Snackbar 显示空指针异常

Snackbar is showing Null pointerException

Snackbar 显示空指针异常 fragment.I 已在 Onfailure 中实现 Snackbar error.Snackbar 显示空指针异常 fragment.I 已在 Onfailure [=19] 中实现 Snackbar =] 显示空指针异常 fragment.I 已在 Onfailure 错误中实现 Snackbar。

public class SignInOtpFragment 扩展片段 {

@BindView(R.id.tv_generate_otp)
TextView tvGenerateOtp;
@BindView(R.id.et_mobile)
TextInputEditText etMobile;
@BindView(R.id.constraintLayout)
ConstraintLayout mConstraintLayout;

String MobilePattern = "[0-9]{10}";

@Override
public View onCreateView(LayoutInflater inflater,
                         ViewGroup container,
                         Bundle savedInstanceState) {
    View fragmentView = inflater.inflate(R.layout.generate_otp, container, false);
    ButterKnife.bind(this, fragmentView);
    getActivity().setTitle("Sign In");

    return fragmentView;
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
}

@OnClick(R.id.tv_generate_otp)
public void onGenerateOtpClick() {
    validateAndLogin();
}

private void validateAndLogin() {
    if (etMobile.getText().toString().equalsIgnoreCase(null) || etMobile.getText().toString() == "") {
        etMobile.setError("Please Enter a valid mobile number");
    } else if (!etMobile.getText().toString().matches(MobilePattern)) {
        etMobile.setError("Please Enter a valid 10 digit number");
    } else {
        registerUser(etMobile.getText().toString());
    }
}

private void registerUser(final String mobileNum) {
    Log.e("--UserTYPE", AppConstants.USER_TYPE);
    Log.e("--MobileNumber", mobileNum);
    ApiClient.makeAPI().postRegister(mobileNum,
            AppConstants.USER_TYPE).enqueue(new Callback<Register>()

    {
        @Override
        public void onResponse(Call<Register> call, Response<Register> response) {
            ApiClient.printCall(call, response, "Generate OTP");
            String status = response.body().getStatus();               

//                if (status.equalsIgnoreCase("1")) {
            SharedPrefsUtils.setStringPreference(getActivity(), AppConstants.BASE_USER, response.body().getUserid());
            Bundle bundle = new Bundle();
            bundle.putString(AppConstants.KEY_MOBILE_NUM, mobileNum);
            Fragment fragment = new SignInVerifyOtpFragment();
            fragment.setArguments(bundle);
            //Move to VerifyOtp Fragment
            if (getActivity() instanceof BaseActivity) {
                (((BaseActivity) getActivity())).replaceFragment(R.id.container, fragment);
            }
            //  etMobile.setText("");

        }

        @Override
        public void onFailure(Call<Register> call, Throwable t) {
            Log.d("error", call.toString());
            Log.d("--Throwable", t.toString());
            // Fabric crashlytics issue.
            Snackbar snackbar = Snackbar
                    .make(mConstraintLayout, "Error occured!", Snackbar.LENGTH_LONG);
            snackbar.show();
        }
    });
}

我的 logcat 显示:

Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.view.ViewGroup.getContext()' on a null object reference
       at android.support.design.widget.Snackbar.make(Snackbar.java:136)
       at com.menu28.menu28.view.fragment.SignInOtpFragment.onFailure(SignInOtpFragment.java:105)
       at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall.run(ExecutorCallAdapterFactory.java:77)
       at android.os.Handler.handleCallback(Handler.java:815)
       at android.os.Handler.dispatchMessage(Handler.java:104)
       at android.os.Looper.loop(Looper.java:194)
       at android.app.ActivityThread.main(ActivityThread.java:5651)
       at java.lang.reflect.Method.invoke(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:372)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)

小吃店选择以下方法

public static void showMessage(Activity activity) {
    Snackbar snackbar = Snackbar.make(activity.findViewById(android.R.id.content),
            activity.getString(R.string.your_msg_string), Snackbar.LENGTH_LONG);
    TextView textView = (TextView) snackbar.getView().findViewById(android.support.design.R.id.snackbar_text);
    textView.setTextColor(Color.RED);
    snackbar.show();
}

因为你在我认为的片段中

Snackbar snackbar = Snackbar
                    .make(getView(), "Error occured!", Snackbar.LENGTH_LONG);
            snackbar.show();

应该适合你,问题是你传递给 snackbar 的视图是空的。