Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to make Asynctask postExecute send variable back to main class
    text
    copied!<p>I have my class called Main.java which runs the UI. I have another class called VerifyCreds.java which extends AsyncTask. VerifyCreds whole purpose is to run calls to my server on another thread so as to not lockup my UI. I don't have it sublcassed in Main.java because I will have a number of other classes that will be making calls to the server and I would like to have them all use the VerifyCreds class to do it.</p> <p>What I need to be able to do is pass data back to Main.java from VerifyCreds.java. but getting a error with the below code.</p> <p>Any help is appreciated! Thanks!</p> <p>Here is my Main.java code</p> <pre><code> package com.coolprograms.zeal; &lt;&lt;&lt;imports removed for brevity&gt;&gt; public class Main extends Activity { // Global VAR's String TAG = "ZEAL"; static Boolean authCode; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.login); //Grab elements final EditText userName = (EditText)findViewById(R.id.usernameBOX); final EditText userPWD = (EditText)findViewById(R.id.passwordBOX); final Button loginBTN = (Button)findViewById(R.id.loginBTTN); //Button listener loginBTN.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Get username and password and add to array ArrayList&lt;String&gt; creds = new ArrayList&lt;String&gt;(); creds.add(userName.getText().toString()); creds.add(userPWD.getText().toString()); // Call method that will execute AsyncTask to get creds from server verify(creds); } }); } private void verify (ArrayList&lt;String&gt; creds) { // Start AsyncTask to get creds new VerifyCredentials(this).execute(creds); } public interface credsAuth { public void authorizedCreds(Boolean authCode); } public void verifiedCreds(Context ctx, Boolean serverAuth) { if(serverAuth = true) { // Move to next screen if we are authorized Intent i = new Intent(ctx, Items.class); startActivity(i); } } } </code></pre> <p>Here is the VerifyCreds.java code</p> <pre><code>package com.coolprograms.zeal; &lt;&lt;&lt;imports removed for brevity&gt;&gt; public class VerifyCredentials extends AsyncTask&lt;ArrayList&lt;String&gt;, Void, Boolean&gt; { private Context ctx; ProgressDialog dialog; public VerifyCredentials(Context applicationContext) { // TODO Auto-generated constructor stub ctx = applicationContext; dialog = new ProgressDialog(applicationContext); } @Override protected void onPreExecute() { dialog.setTitle("Please wait"); dialog.setMessage("Verifying username and password..."); dialog.show(); } @Override protected Boolean doInBackground(ArrayList&lt;String&gt;...creds) { //To return Boolean serverAnwser = false; //Get the creds String userID = creds[0].get(0).toString(); Log.i("ZEAL", "Creds[0]: " + creds[0].get(0).toString()); String userPWD = creds[0].get(1).toString(); Log.i("ZEAL", "Creds[1]: " + creds[0].get(1).toString()); //Get creds from server try { String serverANW = null; URL getCreds = new URL("http://XXX.XXX.XXX.XXX/api/api.php?method=getCreds&amp;id=" + userID + "&amp;pwd=" + userPWD); Log.i("ZEAL", "Webservice URL: " + getCreds.toString()); URLConnection tc = getCreds.openConnection(); BufferedReader in = new BufferedReader(new InputStreamReader( tc.getInputStream())); // Get JSON Object JSONArray jArray; jArray = new JSONArray(in.readLine()); //Log json object returned Log.i("ZEAL", jArray.toString()); //Check server response //for (int i = 0; i &lt; jArray.length(); i++) //{ JSONObject e = jArray.getJSONObject(0); String s = e.getString("RETURN"); JSONObject jObject = new JSONObject(s); serverANW = jObject.getString("RESULT"); Log.i("ZEAL", "API Result: " + serverANW); //} if(serverANW.equalsIgnoreCase("True")) { serverAnwser = true; Log.i("ZEAL", "ServerANW = " + serverANW.toString()); Log.i("ZEAL", "Setting server anwser to true"); } else { serverAnwser = false; Log.i("ZEAL", "ServerANW = " + serverANW.toString()); Log.i("ZEAL", "Setting server anwser to false - " + serverAnwser.toString()); } } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } return serverAnwser; } @Override protected void onPostExecute(final Boolean authCode) { dialog.dismiss(); // Log what the functions is doing Log.d("ZEAL","Server Response: " + authCode.toString()); Toast.makeText(ctx, "Server says: " + authCode.toString(), Toast.LENGTH_LONG).show(); Main m = new Main(); m.verifiedCreds(m, authCode); } } </code></pre> <p>ERROR:</p> <pre><code>05-23 15:18:57.190: E/AndroidRuntime(25475): FATAL EXCEPTION: main 05-23 15:18:57.190: E/AndroidRuntime(25475): java.lang.NullPointerException 05-23 15:18:57.190: E/AndroidRuntime(25475): at android.content.ContextWrapper.getPackageName(ContextWrapper.java:127) 05-23 15:18:57.190: E/AndroidRuntime(25475): at android.content.ComponentName.&lt;init&gt;(ComponentName.java:75) 05-23 15:18:57.190: E/AndroidRuntime(25475): at android.content.Intent.&lt;init&gt;(Intent.java:3174) 05-23 15:18:57.190: E/AndroidRuntime(25475): at com.coolprograms.zeal.Main.verifiedCreds(Main.java:85) 05-23 15:18:57.190: E/AndroidRuntime(25475): at com.coolprograms.zeal.VerifyCredentials.onPostExecute(VerifyCredentials.java:117) 05-23 15:18:57.190: E/AndroidRuntime(25475): at com.coolprograms.zeal.VerifyCredentials.onPostExecute(VerifyCredentials.java:1) 05-23 15:18:57.190: E/AndroidRuntime(25475): at android.os.AsyncTask.finish(AsyncTask.java:602) 05-23 15:18:57.190: E/AndroidRuntime(25475): at android.os.AsyncTask.access$600(AsyncTask.java:156) 05-23 15:18:57.190: E/AndroidRuntime(25475): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615) 05-23 15:18:57.190: E/AndroidRuntime(25475): at android.os.Handler.dispatchMessage(Handler.java:99) 05-23 15:18:57.190: E/AndroidRuntime(25475): at android.os.Looper.loop(Looper.java:137) 05-23 15:18:57.190: E/AndroidRuntime(25475): at android.app.ActivityThread.main(ActivityThread.java:4424) 05-23 15:18:57.190: E/AndroidRuntime(25475): at java.lang.reflect.Method.invokeNative(Native Method) 05-23 15:18:57.190: E/AndroidRuntime(25475): at java.lang.reflect.Method.invoke(Method.java:511) 05-23 15:18:57.190: E/AndroidRuntime(25475): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 05-23 15:18:57.190: E/AndroidRuntime(25475): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 05-23 15:18:57.190: E/AndroidRuntime(25475): at dalvik.system.NativeStart.main(Native Method) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload