Java 无法在类内使用Toast函数?

Java 无法在类内使用Toast函数?,java,android,Java,Android,我尝试以不同的格式使用Toast函数来显示消息,我提供了许多参数,如:getBaseContext、getContext、.getApplication、Context 但最终还是有错误,他没有成功地传达信息 //我的日志: AndroidRuntime: FATAL EXCEPTION: main Process: com.examp

我尝试以不同的格式使用Toast函数来显示消息,我提供了许多参数,如:getBaseContext、getContext、.getApplication、Context 但最终还是有错误,他没有成功地传达信息

//我的日志:

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

在你的非活动课上

public class NonActivity {

public void showToast(Context context, String message){
    Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
 }
}
//活动电话

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

尝试此操作

将构造函数从public retrieveUserLoginfinal字符串t1、final字符串t2{更改为public retrieveUserLoginContext、final字符串t1、final字符串t2{ 并指定mContext=context,然后将活动实例传递给它。

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

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

问题可能是您没有为“活动”指定值,但将上下文作为参数传递可以解决该问题。这样,您只能在有上下文的情况下调用该方法。

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
}

此类中没有上下文。必须使用类构造函数发送上下文。欢迎使用SO。粘贴的logcat没有帮助。请将其添加为崩溃开始的完整logcat。