2016-08-04 7 views
0

私はタスクとインターフェイスをコールバック関数で実装しています。ユーザーが間違ってアカウントを取得すると、エラーメッセージが表示され、その時点でエラーはありません。しかし、ユーザーが正常にログインするとアプリは残念ながら停止します。アンドロイド - asynctaskが終了した後でのみ現在のアクティビティを終了します

の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); 
      } 
     } 
    } 
} 

エラー:

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)

+0

「残念なことに停止」は、ログキャッチがあることを意味します。質問にそれを含めてください –

+0

あなたの_delegate_はnullです – Piyush

+0

しかし私はbackgroundTask.delegate = this; in login.class –

答えて

1

あなた

AsyncResponse delegate = null;

delegateがnullのクラッシュの ---------始まり。あなたはそれを初期化していません!

編集:

コンストラクタ

public AsyncResponse delegate; 

    public BackgroundTask(AsyncResponse delegate){ 
    this.delegate = delagate; 
    } 

今すぐ

BackgroundTask backgroundTask = new BackgroundTask(new AsyncResponse()......); 
    backgroundTask.delegate = this;// Comment this 
    backgroundTask.execute("login",user_ac_s,user_pw_s); 

thisキーワードがあなたのケースでは、アクティビティのコンテキストを定義しますが、実際にあなたが取得する必要があるとあなたの非同期タスクを実行してくださいAsyncResponse

+0

class loginでbackgroundTask.delegate = thisを実行するとどうなりますか? –

+0

_this_は何を意味していますか?非同期コンストラクタで代理人を渡す必要があります – Piyush

+0

backgroundTask.delegate = thisはオブジェクトbackgroundTaskの変数delegateを変更しますか? –

関連する問題