仅在api 19中单击按钮时发生IllegalStateException

时间:2019-08-14 07:31:10

标签: java android onclick illegalstateexception

我仅在android 4.4 kitKat中单击IllegalStateException,我已经在andorid 9.0,android 8.0,android 7.0和android 5.1上进行过尝试,并且在这些设备上都可以正常工作,但错误仅在安卓奇巧。我不确定我的代码有什么问题,请帮忙,我的应用程序正在生产中。

日志是由Firebase testLab生成的,因为我没有4.4设备:


    at androidx.appcompat.app.AppCompatViewInflater$a.onClick(:2)
       FATAL EXCEPTION: ControllerMessenger
       Process: in.co.poket_play, PID: 31314
       java.lang.IllegalStateException: Could not execute method for android:onClick
           at androidx.appcompat.app.AppCompatViewInflater$a.onClick(:2)
           at android.view.View.performClick(View.java:4626)
           at android.view.View$PerformClick.run(View.java:19293)
           at android.os.Handler.handleCallback(Handler.java:733)
           at android.os.Handler.dispatchMessage(Handler.java:95)
           at androidx.test.espresso.base.Interrogator.a(Interrogator.java:19)
           at androidx.test.espresso.base.UiControllerImpl.a(UiControllerImpl.java:166)
           at androidx.test.espresso.base.UiControllerImpl.a(UiControllerImpl.java:158)
           at androidx.test.espresso.base.UiControllerImpl.a(UiControllerImpl.java:34)
           at androidx.test.espresso.action.MotionEvents.a(MotionEvents.java:77)
           at androidx.test.espresso.action.MotionEvents.a(MotionEvents.java:52)
           at androidx.test.espresso.action.Tap.c(Tap.java:8)
           at androidx.test.espresso.action.Tap.b(Tap.java:18)
           at androidx.test.espresso.action.Tap$1.a(Tap.java:3)
           at androidx.test.espresso.action.GeneralClickAction.perform(GeneralClickAction.java:20)
           at androidx.test.espresso.ViewInteraction$SingleExecutionViewAction.perform(ViewInteraction.java:9)
           at androidx.test.espresso.ViewInteraction.a(ViewInteraction.java:79)
           at androidx.test.espresso.ViewInteraction.a(ViewInteraction.java:96)
           at androidx.test.espresso.ViewInteraction$1.call(ViewInteraction.java:3)
           at java.util.concurrent.FutureTask.run(FutureTask.java:237)
           at android.os.Handler.handleCallback(Handler.java:733)
           at android.os.Handler.dispatchMessage(Handler.java:95)
           at android.os.Looper.loop(Looper.java:157)
           at android.app.ActivityThread.main(ActivityThread.java:5293)
           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:1265)
           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
           at dalvik.system.NativeStart.main(Native Method)
       Caused by: java.lang.reflect.InvocationTargetException
           at java.lang.reflect.Method.invokeNative(Native Method)
           at java.lang.reflect.Method.invoke(Method.java:515)
           ... 29 more
       Caused by: java.lang.NullPointerException: println needs a message
           at android.util.Log.println_native(Native Method)
           at android.util.Log.e(Log.java:298)
           at in.co.poket_play.login_and_register.UserProfileActivity.saveData(:4)

创建时:

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_profile);

        profileImage  = findViewById(R.id.profileImage);
        fieldUsername = findViewById(R.id.textName);
        fieldAge = findViewById(R.id.textAge);
        fieldEmail = findViewById(R.id.textEmail);
        sexGroup = findViewById(R.id.genderGroup);
        maleBtn = findViewById(R.id.genderMale);
        femaleBtn = findViewById(R.id.genderFemale);

        mAuth = FirebaseAuth.getInstance();
        functions = new FirebaseFunctions(this);

        setProfileData();


    }

这是OnClick方法:

public void saveData(View view){

    try {
        boolean isChanged = true;
        RadioButton radioSexButton = findViewById(sexGroup.getCheckedRadioButtonId());
        String newUsername = fieldUsername.getText().toString().trim();
        String newAge = fieldAge.getText().toString().trim();
        String newGender = radioSexButton.getText().toString().trim();



    if(newUsername.length() > 0){
        if(!newUsername.equals(username)) {
            functions.updateUsername(newUsername);
            isChanged = true;
        }
    }else {
        fieldUsername.setError("Invalid username");
        isChanged = false;
    }


    if(newAge.length() > 0){
        if(!newAge.equals(String.valueOf(age))) {
            functions.updateAge(Integer.parseInt(newAge));
            isChanged = true;
        }
    }else {
        fieldAge.setError("Invalid Age");
        isChanged = false;
    }


    if(!newGender.equalsIgnoreCase(gender)){
        functions.updateGender(newGender);
        isChanged = true;
    }


    if(isChanged){
        Toast.makeText(this, "Details Saved", Toast.LENGTH_SHORT).show();
        finish();
    }

    }catch (Exception e){
        Log.e("ERROR",e.getLocalizedMessage());
    }

}

这是setProfileData方法:

private void setProfileData(){

        FirebaseFirestore db = FirebaseFirestore.getInstance();
        DocumentReference docRef = db.collection(getString(R.string.db_users)).document(Objects.requireNonNull(mAuth.getCurrentUser()).getUid());
        listener = docRef.addSnapshotListener(new EventListener<DocumentSnapshot>() {
            @Override
            public void onEvent(@Nullable DocumentSnapshot documentSnapshot, @Nullable FirebaseFirestoreException e) {

                if (documentSnapshot != null) {
                    User user = documentSnapshot.toObject(User.class);

                    if (user != null) {

                        username = user.getUsername();
                        email = user.getEmail();
                        age = user.getAge();
                        gender = user.getGender();

                        Glide.with(getApplicationContext())
                                .load(user.getProfile_image_url())
                                .placeholder(R.drawable.com_facebook_profile_picture_blank_square)
                                .into(profileImage);

                        fieldUsername.setText(username);
                        fieldEmail.setText(email);
                        fieldAge.setText(String.valueOf(age));

                        if (gender.equalsIgnoreCase(getString(R.string.textMale))) {
                            maleBtn.setChecked(true);
                        } else if (gender.equalsIgnoreCase(getString(R.string.textFemale))) {
                            femaleBtn.setChecked(true);
                        }

                    }
                }
            }
        });

    }

2 个答案:

答案 0 :(得分:1)

问题可能不在您正在寻找的地方。您应该查看每个“ Causeed By”语句。

Caused by: java.lang.NullPointerException: println needs a message
        at android.util.Log.println_native(Native Method)
        at android.util.Log.e(Log.java:298)
        at in.co.poket_play.login_and_register.UserProfileActivity.saveData(:4)

您应该查看此软件包- in.co.poket_play.login_and_register.UserProfileActivity.saveData 并检查您的saveData方法是否存在此异常。-“ NullPointerException”

答案 1 :(得分:0)

实际上,我试图获取已检查单选按钮的Id,因此当未选中任何单选按钮时,由于按钮ID不匹配,它引发了异常。所以我通过一个简单的if检查解决了它。但我仍然不知道,它在其他Android版本上如何工作。