-1

I am implementing a class named MyAdapter which extends BaseAdapter. In the class myAdapter I am implementing a class named AttemptLike which extend AsyncTask. In the class AttemptLike while Implementind doInBackground function I am not able to use finish(); So my app is getting crashed everytime when i invoke the Attemptlike class. So please tell me how to handle the error.

The code goes like this

 class MyAdapter extends BaseAdapter{
 .
 .
 .
  public class Attemptlike extends AsyncTask<String, String, String>{
  {
  protected String doInBackground(String... args) 
  {
  try 
                { 
                    List<NameValuePair> params = new ArrayList<NameValuePair>();
                    params.add(new BasicNameValuePair("somedata1", field1)); 
                    params.add(new BasicNameValuePair("somedata2", field2));
 params.add(new BasicNameValuePair("geteventdata", evnt));
                    Log.d("request!", "starting"); 
                    JSONObject json = jsonParser.makeHttpRequest( LOGIN_URL, "POST", params);
                    // checking log for json response 
                    Log.d("Login attempt", json.toString()); 
                    // success tag for json 
                    success = json.getInt(TAG_SUCCESS); 
                    if (success == 1) 
                    { 
                        Log.d("Successfully Liked!", json.toString());

                        finish();
                        // this finish() method is what i am not able use which is resulting in crashing of app
                    }  
                catch (JSONException e) 
                { 
                    e.printStackTrace();
                } 
                return null; 
                }  

        }

        new AttemptLogin().execute();
   return convertView;
   }

 }

StackTrace

FATAL EXCEPTION: AsyncTask #1
Process: ks.developers.festo, PID: 29568
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:304)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355) 
at java.util.concurrent.FutureTask.setException(FutureTask.java:222) 
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at      java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at  java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method     'java.lang.String org.json.JSONObject.toString()' on a null object reference
at ks.developers.festo.list.adapter.MyAdapter$2$1AttemptLike.doInBackground(MyAdapter.java:237)
at ks.developers.festo.list.adapter.MyAdapter$2$1AttemptLike.doInBackground(MyAdapter.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:292)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
... 4 more
Viral Patel
  • 32,418
  • 18
  • 82
  • 110
Karthik
  • 1,088
  • 6
  • 17

2 Answers2

1

You cannot use finish like that in an AsyncTask. It is a member method of activity.

You might want to try putting it inside runOnUiThread() or passing the contest to the AsyncTask and calling finish() using the context.

Havent personally tried either but i bet on the first one to work.

Update after looking at the stack trace you pasted now:

You actually have a NPE at this line:

Log.d("Login attempt", json.toString()); 

json is null, so you are not getting the data at all. It is never reaching finish() be cause the NPE is thrown much before that in the toString() call when you are trying to log the json. First fix that and see if it works. If still doesnt, try one of the above approaches.

Viral Patel
  • 32,418
  • 18
  • 82
  • 110
  • i have pasted the stack trace here just have a look at it – Karthik Jan 20 '16 at 16:48
  • actually I tried the same work again its working good.And after some time its not working good and is being crashed. – Karthik Jan 20 '16 at 17:18
  • did not understand the above comment. can you explain what worked and then stopped working again and what did you change that make it momentarily work? – Viral Patel Jan 20 '16 at 17:20
  • I havent changed anything i just reinstalled the app and tried agian. It worked good for the first time and later it started to get crashed. – Karthik Jan 20 '16 at 17:26
  • remove the `log.d` and do a null check for `json` before using it. – Viral Patel Jan 20 '16 at 17:27
0

Pass your result to asynctask#onpostexecute and call finish from there. Finish interacts with the ui thread and must be called from that thread. Your asynctask is executed on another thread. On post execute is executed on the calling thread so finish will work.

davehenry
  • 1,026
  • 9
  • 24