Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can i to get the result of OnPostExecute() to main activity because AsyncTask is a separate class?
    primarykey
    data
    text
    <p>I read and apply something from this link:<a href="https://stackoverflow.com/questions/12575068/how-to-get-the-result-of-onpostexecute-to-main-activity-because-asynctask-is-a">How to get the result of OnPostExecute() to main activity because AsyncTask is a separate class?</a> but I get an error NullPointerException onPostExecute on the line delegate.processFinish(result); What is the problem in my code? Here is the code:</p> <pre><code>public class MainActivity extends Activity implements AsyncResponse{ ProductConnect asyncTask =new ProductConnect(); public void processFinish(String output){ //this you will received result fired from async class of onPostExecute(result) method. Log.v(TAG, output); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); asyncTask.delegate = this; setContentView(R.layout.activity_main); Button b = (Button) findViewById(R.id.button1); final Intent i=new Intent(MainActivity.this, second.class); b.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub new ProductConnect().execute(true); startActivity(i); //startActivity(new Intent(MainActivity.this, second.class)); } }); } // START DATABASE CONNECTION class ProductConnect extends AsyncTask&lt;Boolean, String, String&gt; { public AsyncResponse delegate=null; private Activity activity; public void MyAsyncTask(Activity activity) { this.activity = activity; } @Override protected String doInBackground(Boolean... params) { String result = null; StringBuilder sb = new StringBuilder(); try { // http post HttpClient httpclient = new DefaultHttpClient(); HttpGet httppost = new HttpGet("http://192.168.2.245/getProducts.php"); HttpResponse response = httpclient.execute(httppost); if (response.getStatusLine().getStatusCode() != 200) { Log.d("MyApp", "Server encountered an error"); } BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF8")); sb = new StringBuilder(); sb.append(reader.readLine() + "\n"); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } result = sb.toString(); Log.d("test", result); } catch (Exception e) { Log.e("log_tag", "Error converting result " + e.toString()); } return result; } @Override protected void onPostExecute(String result) { try { JSONArray jArray = new JSONArray(result); JSONObject json_data; for (int i = 0; i &lt; jArray.length(); i++) { json_data = jArray.getJSONObject(i); t = json_data.getString("name"); delegate.processFinish(result); } } catch (JSONException e1) { e1.printStackTrace(); } catch (ParseException e1) { e1.printStackTrace(); } super.onPostExecute(result); } protected void onPreExecute() { super.onPreExecute(); ProgressDialog pd = new ProgressDialog(MainActivity.this); pd.setTitle("Lütfen Bekleyiniz"); pd.setMessage("Authenticating.."); pd.show(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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