2017-05-09 115 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(); 
    } 

,我想用它我onResponse方法内低于

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在控制台下面的错误显示打破了应用

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

我该换什么context

注:其非活性类

+1

如果它是在activity类中,那么使用'ActivityName.this',如果它是非activity类,那么使用'getApplicationContext();' – Piyush

+0

“onResponse”方法是什么? –

+0

@Piyush它的非活动类,并使用getApplicationContext()打破了与应用程序'java.lang.NullPointerException:试图调用虚拟方法'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

服务是一个上下文,所以上下文context = this;在构造函数中执行并传递给该方法。 – Lingeshwaran

0

创建应用类的getter setter方法方面则 这种使用可以在应用

改变这一行

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

你的意思是修改应用程序类?我怎样才能访问它?我是新来的android –