类中不能使用Toast函数?

时间:2018-09-30 08:53:56

标签: java android

我尝试通过使用Toast函数显示消息,以不同的格式提供了许多参数,例如:getBaseContext()getContext(),。getApplication()Context() 但是最后还是有错误,他没有显示信息

//我的logCat:

AndroidRuntime: FATAL EXCEPTION: main
                                                                                 Process: com.example.android.login, PID: 9911
                                                                                 java.lang.NullPointerException                                                                          at com.example.android.login.retrieveUserLogin$1.onResponse(retrieveUserLogin.java:76)                                                                      at retrofit.ExecutorCallAdapterFactory$ExecutorCallback$1.run(ExecutorCallAdapterFactory.java:86                                                                      at android.os.Handler.handleCallback(Handler.java:733)
                                                                      at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                     at android.os.Looper.loop(Looper.java:136)
                                                                                     at android.app.ActivityThread.main(ActivityThread.java:5001)
                                                                                     at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                     at java.lang.reflect.Method.invoke(Method.java:515)
                                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
                                                                                     at dalvik.system.NativeStart.main(Native Method)

我的课:

public class retrieveUserLogin {

    public Activity activity;
    private ProgressDialog progressDialog;
    private HttpApi api;
    public int value_array;
    private List<Users> user;
    private String UserAccount_;
    private Call<HttpApi.HttpBinResponse> call;
    private Call<List<Users>> getUsersCall;
    private List<Integer>collection;
 //   private Context context;
      Context mContext;
    public retrieveUserLogin(final String t1 ,final String t2){

      //  context = context;
        api = HttpApi.getInstance();
        api.addHeader("Authorization","MyT23");
        getUsersCall = api.getService().getAllChatRooms();
        getUsersCall.enqueue(new retrofit.Callback<List<Users>>(){
            String UserAccuent;
            @Override
            public void onResponse(retrofit.Response<List<Users>>response, Retrofit retrofit){

                collection = new ArrayList<>();
                user = response.body();

                String [] arrayString = new String[user.size()];
                String [] arrayString2 = new String[user.size()];

                int[] arrayInt = new int[user.size()];

                for(int i=0; i<response.body().size(); i++){
                    arrayString[i] = user.get(i).user_name;
                    arrayString2[i] = user.get(i).email;
                    arrayInt[i] =  user.get(i).password;

                    if(Arrays.asList(arrayString2).contains(t1)){
                        UserAccuent =   arrayString[i];
                        break;
                    }
                }

                boolean Check = Arrays.asList(arrayString2).contains(t1);
                boolean Check2 = contains(arrayInt , Integer.parseInt(t2));

                if(Check == true && Check2 == true){

               Toast.makeText(activity.getApplication(),"تم تسجيل الدخول يا......" +UserAccuent.toString() ,Toast.LENGTH_LONG).show();
                   // Toast.makeText(getContext(), "sdfasd"+5, Toast.LENGTH_LONG).show();

                }else{
                 Toast.makeText(activity.getBaseContext(),"خطأ فى اسم البريد او كلمة السر" ,Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void onFailure(Throwable t){
                progressDialog.dismiss();
                // Toast.makeText(getContext(),"XXX" ,Toast.LENGTH_LONG).show();
            }});
    }


    public static boolean contains(int[] arr,int item) {
        for (int n : arr){
            if (item == n){
                return true;
            }
        }
        return false;
    }



    // End ;
}

5 个答案:

答案 0 :(得分:0)

在您的非活动班级中

public class NonActivity {

public void showToast(Context context, String message){
    Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
 }
}

答案 1 :(得分:0)

//通过活动致电

retrieveUserLogin("username","userpassword",Activity.this)

//更改构造函数

  public retrieveUserLogin(final String t1 ,final String t2, final Context context){
         mContext = context;  
        Toast.makeText(mContext ,"خطأ فى اسم البريد او كلمة السر" ,Toast.LENGTH_LONG).show();}

尝试一下

答案 2 :(得分:0)

将承包商从public retrieveUserLogin(final String t1 ,final String t2){更改为public retrieveUserLogin(Context context, final String t1 ,final String t2){
并分配mContext=context,然后将您的活动实例传递给它。

答案 3 :(得分:0)

请勿在字段中存储活动和上下文。而是将它们作为参数传递给方法。

public void retrieveUserLogin(
    final Context context, 
    final String t1 ,final String t2) {
    // ...
    // then change your toasts to
    Toast.makeText(context, "...", Toast.LENGTH_LONG).show();
}

问题可能出在您没有为activity分配值,而是通过将上下文作为参数来解决该问题。这样,只有在有上下文可提供时,您才能调用该方法。

答案 4 :(得分:0)

由于活动为空,Toast不显示任何消息:

Toast.makeText(activity.getApplication(),"تم تسجيل الدخول يا......" +UserAccuent.toString() ,Toast.LENGTH_LONG).show();

这也是为什么您在LogCat中看到NullPointerException的原因。

您需要初始化retrieveUserLogin类的活动:

public Activity activity;

在类构造函数内部,可能您需要通过再添加一个参数来稍微更改构造函数:

public retrieveUserLogin(Activity activity, final String t1 ,final String t2){
     // init activity here
     this.activity = activity;
     // your code
}