-1

我正在为我的 android 类开发一个项目。I have three options listed on my MainActivity, and when one is selected, the user goes to one of three activities: a) an activity to enter new student info, b) an activity which views all students on record, or c) an activity它允许跟踪目标学生的行为。当我选择用于输入学生信息的单选按钮并单击“选择”按钮时,一切正常,即您被发送到正确的活动。However, when either of the other two options are selected, I get a null pointer exception. 我的清单中声明了所有三项活动。

这是我的课程代码:

    public void onButtonClicked (View v){
    RadioButton rdTrackBehaviour = (RadioButton)findViewById(R.id.radioOption2);
    RadioButton rdEnterStudent = (RadioButton)findViewById(R.id.radioOption1);
    RadioButton rdCurrentStudents = (RadioButton)findViewById(R.id.radioOption3);

            if(rdEnterStudent.isChecked()){
                Intent enterIntent = new Intent(MainActivity.this, EnterStudentActivity.class);
                startActivity(enterIntent);
            }
            if(rdTrackBehaviour.isChecked()){
                Intent onTaskIntent = new Intent(MainActivity.this, ActivityOnTaskBehaviour.class);
                MainActivity.this.startActivity(onTaskIntent);
            }
            else{
                if(rdCurrentStudents.isChecked()){
                    Intent currentStudentIntent = new Intent(MainActivity.this, StudentList.class);
                    startActivity(currentStudtenIntent);
                }
            }
}

还有我清单中的一个片段:

    <application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".EnterStudentActivity"
        android:label="@string/title_activity_enter_student" >
    </activity>
    <activity
        android:name=".ActivityOnTaskBehaviour"
        android:label="@string/title_activity_activity_on_task_behaviour" >
    </activity>
    <activity
        android:name=".StudentList"
        android:label="@string/title_activity_student_list" >
    </activity>
    <activity
        android:name=".Results"
        android:label="@string/title_activity_results" >
    </activity>
</application>

还有我的堆栈跟踪:

11-25 19:47:39.735: E/AndroidRuntime(332): FATAL EXCEPTION: main
11-25 19:47:39.735: E/AndroidRuntime(332): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.behaviourtracker/com.example.behaviourtracker.ActivityOnTaskBehaviour}: java.lang.NullPointerException
11-25 19:47:39.735: E/AndroidRuntime(332):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
11-25 19:47:39.735: E/AndroidRuntime(332):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
11-25 19:47:39.735: E/AndroidRuntime(332):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
11-25 19:47:39.735: E/AndroidRuntime(332):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
11-25 19:47:39.735: E/AndroidRuntime(332):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-25 19:47:39.735: E/AndroidRuntime(332):  at android.os.Looper.loop(Looper.java:123)
11-25 19:47:39.735: E/AndroidRuntime(332):  at android.app.ActivityThread.main(ActivityThread.java:3683)
11-25 19:47:39.735: E/AndroidRuntime(332):  at java.lang.reflect.Method.invokeNative(Native Method)
11-25 19:47:39.735: E/AndroidRuntime(332):  at java.lang.reflect.Method.invoke(Method.java:507)
11-25 19:47:39.735: E/AndroidRuntime(332):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-25 19:47:39.735: E/AndroidRuntime(332):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-25 19:47:39.735: E/AndroidRuntime(332):  at dalvik.system.NativeStart.main(Native Method)
11-25 19:47:39.735: E/AndroidRuntime(332): Caused by: java.lang.NullPointerException
11-25 19:47:39.735: E/AndroidRuntime(332):  at com.example.behaviourtracker.ActivityOnTaskBehaviour.onCreate(ActivityOnTaskBehaviour.java:19)
11-25 19:47:39.735: E/AndroidRuntime(332):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-25 19:47:39.735: E/AndroidRuntime(332):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
11-25 19:47:39.735: E/AndroidRuntime(332):  ... 11 more

我是个菜鸟,完全被难住了。已经在谷歌上四处寻找一两个小时,每个人似乎一直在说这与未在清单中注册的活动有关。我是不是做错了,还是有其他问题?另外,我扔了一个try catch块

            if(rdTrackBehaviour.isChecked()){
                Intent onTaskIntent = new Intent(MainActivity.this, ActivityOnTaskBehaviour.class);
                MainActivity.this.startActivity(onTaskIntent);
            }

使用 log.d,但 logcat 中没有任何内容....

4

2 回答 2

3

检查第 19 行ActivityOnTaskBehaviour.java。您很可能试图在该行的空对象上调用实例方法。

于 2012-11-26T02:43:03.710 回答
1
11-25 19:47:39.735: E/AndroidRuntime(332):  at com.example.behaviourtracker.ActivityOnTaskBehaviour.onCreate(ActivityOnTaskBehaviour.java:19)

您的问题在 ActivityOnTaskBehaviour.java 的第 19 行。

于 2012-11-26T02:42:16.263 回答