I am working on a project for my android class. 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 which allows tracking of a target students’ behaviour.
When I select the radio button for entering student info and click the ‘select’ button, all goes as it should, i.e. you are sent to the proper activity. However, when either of the other two options are selected, I get a null pointer exception. I have all three activities declared in my manifest.
Here is my class code:
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);
}
}
}
And a snippet from my manifest:
<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>
And my stackTrace:
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
I’m a total rookie, and completely stumped. Have been looking around for an hour or two on the googles, and everyone seems to keep saying that it has to do with the activity not being registered in the manifest. Have I done that incorrectly, or is there another problem?
Also, I threw a try catch block around
if(rdTrackBehaviour.isChecked()){
Intent onTaskIntent = new Intent(MainActivity.this, ActivityOnTaskBehaviour.class);
MainActivity.this.startActivity(onTaskIntent);
}
With a log.d, but there was nothing in logcat….
Check line 19 of
ActivityOnTaskBehaviour.java. You’re most likely trying to call an instance method on a null object on that line.