I have implement task by interface and callback function. When the user got account wrong, the error message comes up and there are no errors at that point. However, the app stops unfortunately when the user login successfully.
Java:
public interface AsyncResponse {
void processFinish();
}
// start this activity from MainActivity when the button is pressed
public class login extends AppCompatActivity implements AsyncResponse{
...
BackgroundTask backgroundTask = new BackgroundTask(this);
backgroundTask.delegate = this;
backgroundTask.execute("login",user_ac_s,user_pw_s);
public void processFinish(){
if(!MainActivity.user_ID.equals("0"))
finish();
}
}
public class BackgroundTask extends AsyncTask<String, Void, String>{
...
public AsyncResponse delegate = null;
...
protected void onPostExecute(String result) {
super.onPreExecute();
if(task.equals("get_userID")){
MainActivity.user_ID = result;
delegate.processFinish();
}
if(task.equals("register") || task.equals("login")){
Toast.makeText(context,result,Toast.LENGTH_LONG).show();
if(result.contains("success")){
BackgroundTask get_userID_task = new BackgroundTask(context);
get_userID_task.execute("get_userID",user_ac);
}
}
}
}
Error: --------- beginning of crash
08-04 05:53:11.807 2667-2667/com.example.kwei.minigame1 E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.kwei.minigame1, PID: 2667 java.lang.NullPointerException: Attempt to invoke interface method 'void com.example.kwei.minigame1.AsyncResponse.processFinish()' on a null object reference at com.example.kwei.minigame1.BackgroundTask.onPostExecute(BackgroundTask.java:132) at com.example.kwei.minigame1.BackgroundTask.onPostExecute(BackgroundTask.java:26) at android.os.AsyncTask.finish(AsyncTask.java:636) at android.os.AsyncTask.access$500(AsyncTask.java:177) at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:653) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5254) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)