在Android应用程序中处理新活动时出现NullPointerException

时间:2016-03-01 15:00:47

标签: java android nullpointerexception

我的代码如下,我认为它与我的if / else语句有关。基本上我想确保在打开下一页之前,组中至少有一个单选按钮被检查。一旦这样做,我将对其进行设置,以便如果未检查组中的一个单选按钮,则它将显示错误消息。

    public void onClickListenerButton() {
    radioGroup_gender = (RadioGroup) findViewById(R.id.radioGroup_gender);
    radioGroup_course = (RadioGroup) findViewById(R.id.radioGroup_course);
    button_splits = (Button)findViewById(R.id.button_splits);

    button_splits.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent("com.idealsplit.admin.idealsplit.ResultsActivity");
                    int selectedID_gender = radioGroup_gender.getCheckedRadioButtonId();
                    int selectedID_course = radioGroup_course.getCheckedRadioButtonId();
                    radioButton_gender = (RadioButton) findViewById(selectedID_gender);
                    radioButton_course = (RadioButton) findViewById(selectedID_course);

                    CharSequence gender, courseType, age, event, time;

                    if (selectedID_gender == -1) {
                        //Throw Error
                        isNull = true;
                    } else if (selectedID_course == -1) {
                        //Throw Error
                        isNull = true;
                    } else {
                        courseType = radioButton_course.getText();
                        gender = radioButton_gender.getText();
                        startActivity(intent);

                    }
                }

            }
    );
}

这是logcat:

03-01 08:52:22.060 19234-19234/com.idealsplit.admin.idealsplit E/AndroidRuntime: FATAL EXCEPTION: main
                                                                             Process: com.idealsplit.admin.idealsplit, PID: 19234
                                                                             java.lang.RuntimeException: Unable to start activity ComponentInfo{com.idealsplit.admin.idealsplit/com.idealsplit.admin.idealsplit.ResultsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2358)
                                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2420)
                                                                                 at android.app.ActivityThread.access$900(ActivityThread.java:154)
                                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                 at android.os.Looper.loop(Looper.java:135)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5294)
                                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                                 at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
                                                                              Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                                                 at com.idealsplit.admin.idealsplit.ResultsActivity.onCreate(ResultsActivity.java:96)
                                                                                 at android.app.Activity.performCreate(Activity.java:5990)
                                                                                 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
                                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311)
                                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2420) 
                                                                                 at android.app.ActivityThread.access$900(ActivityThread.java:154) 
                                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321) 
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                 at android.os.Looper.loop(Looper.java:135) 
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5294) 
                                                                                 at java.lang.reflect.Method.invoke(Native Method) 
                                                                                 at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904) 
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699) 

感谢您对此提供的任何帮助,如果需要更多信息,请告诉我们!

**如果有帮助我正在关注这个youtube教程:https://www.youtube.com/watch?v=3f0NAn5xFy4

1 个答案:

答案 0 :(得分:3)

看起来button_splits为null,因此(Button)findViewById(R.id.button_splits);返回null。

确保视图R.id.button_splits存在。 附加到此活动的视图必须包含一个ID为“button_splits”的按钮。

What is a Null Pointer Exception

你也可以通过在添加监听器之前确保按钮不为空来停止这种NPE,但是如果按钮为null则不会添加监听器。

if (button_splits != null) {
    button_splits.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent("com.idealsplit.admin.idealsplit.ResultsActivity");
                    int selectedID_gender = radioGroup_gender.getCheckedRadioButtonId();
                    int selectedID_course = radioGroup_course.getCheckedRadioButtonId();
                    radioButton_gender = (RadioButton) findViewById(selectedID_gender);
                    radioButton_course = (RadioButton) findViewById(selectedID_course);

                    CharSequence gender, courseType, age, event, time;

                    if (selectedID_gender == -1) {
                        //Throw Error
                        isNull = true;
                    } else if (selectedID_course == -1) {
                        //Throw Error
                        isNull = true;
                    } else {
                        courseType = radioButton_course.getText();
                        gender = radioButton_gender.getText();
                        startActivity(new Intent(getApplicationContext(), ResultsActivity.class));
                        startActivity(intent);

                    }
                }

            }
    );
}