2017-05-09 6 views
-1

は、ここに私のダイアログコードonResponseメソッド内でダイアログを作成する方法は?

public void registrationSuccess(final Context context, String warning, String message) { 
     alert = new AlertDialog.Builder(context); 
     alert.setTitle(warning); 
     alert.setMessage(message) 
       .setCancelable(false) 
       .setPositiveButton("OK", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         Intent i = new Intent(context, loginActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
         context.startActivity(i); 
        } 
       }); 
     AlertDialog alertDialog = alert.create(); 
     alert.show(); 
    } 

であると私は

client.newCall(request).enqueue(new Callback() { 
     @Override 
     public void onFailure(Call call, IOException e) { 
      System.out.println("Failure!!"); 
     } 

     @Override 
     public void onResponse(Call call, Response response) throws IOException { 
      if(response.isSuccessful()) { 
       registrationSuccess(getApplicationContext(), getResources().getString(R.string.congrats), getResources().getString(R.string.successfull_registration)); 
      } else { 
       System.out.println("FAILED"); 

      } 
     } 

    }); 

getApplicationContextの下に私のonResponseメソッド内でそれを使用したい、次のエラーがコンソールに表示してアプリを壊す

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference 

contextの代わりをどうすればいいですか?

注:その非アクティビティクラス

+1

もしそれがアクティビティクラスにあれば 'ActivityName.this'を使い、それが非アクティビティクラスなら' getApplicationContext(); 'を使います – Piyush

+0

"onResponse"メソッドは何ですか? –

+0

@Piyushその非アクティビティクラスを使用して、getApplicationContext()を使用すると、java.lang.NullPointerExceptionでアプリケーションが中断されます:nullメソッドを参照しようとしました。 'android.content.Context android.content.Context.getApplicationContext()'コンソール上でエラーが発生しました –

答えて

3

new My_Non_ActivityClass(MainActivity.this); 

今、あなたのクラスでは、このようなコンストラクタを作成し、コンテキストを取得:アクティビティクラスからは、あなたがあなたのクラスを初期化するとき、次のようにコンテキストを渡す

Context context; 
My_Non_ActivityClass(Context c){ 
    context = c; 
} 

今、あなたは、コンテキストを持っているところはどこでも使用します

public void registrationSuccess(context, String warning, String message) { 
    alert = new AlertDialog.Builder(context); 
    alert.setTitle(warning); 
    alert.setMessage(message) 
      .setCancelable(false) 
      .setPositiveButton("OK", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        Intent i = new Intent(context, loginActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
        context.startActivity(i); 
       } 
      }); 
    AlertDialog alertDialog = alert.create(); 
    alert.show(); 
} 

コンテキストを渡す必要はありません。クラス内のグローバル変数であるため、コンテキストにアクセスする必要はありません。

注:サービスには、独自のコンテキストがあります。ちょうどthisを使用してください。

+0

サービスはコンテキストなので、コンテキストはthis =それをコンストラクタで行い、そのメソッドに渡します。 – Lingeshwaran

0

アプリケーション

変更このライン

registrationSuccess(null, getResources().getString(R.string.congrats), getResources().getString(R.string.successfull_registration)); 

で渡ってコンテキストを取得することができ、この使用して、コンテキスト のアプリケーションクラスのゲッターセッターメソッドを作成します。この

registrationSuccess(ApplicationControler.getContext(), getResources().getString(R.string.congrats), getResources().getString(R.string.successfull_registration)); 
+0

あなたはアプリケーションクラスを変更することを意味しますか?どうすればそれにアクセスできますか?私はアンドロイドの新人です –

関連する問題