Snackbar显示Null pointerException

时间:2017-03-06 06:16:20

标签: android nullpointerexception butterknife android-snackbar

Snackbar在fragment中显示空指针异常。我在Onfailure中实现了Snackbar error.Snackbar在fragment中显示空指针异常。我已经在Onfailure中实现了Snackbar error.Snackbar在片段中显示空指针异常。我已经在Onfailure错误中实现了Snackbar。

公共类SignInOtpFragment扩展了Fragment {

@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$1.onFailure(SignInOtpFragment.java:105)
       at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$2.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)

2 个答案:

答案 0 :(得分:1)

为小吃店选择以下方法

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

答案 1 :(得分:1)

当你在片段中我认为

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

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