I have a Menu button called Information and when I touch it, I want a new activity called "InformationActivity" to launch. I've verified the code, but nothing seems to work. I get a NullPointer Exception and a FATAL EXCEPTION: Main. Why is this happening? Check out the code to see where it fails. Thanks.
Here's everything related to the menu;
@Override
public boolean onCreateOptionsMenu(Menu menu){
menu.add(1,INFORMATION,1,"Information");
Log.w("I AM HERE ?!", "YES01");
return true;
}
public boolean onOptionsItemSelected (MenuItem item){
switch (item.getItemId()){
case INFORMATION:
Log.w("I AM HERE ?!", "YES02");
Toast.makeText(getApplicationContext(), "This works - tested.", Toast.LENGTH_LONG).show();
Intent infoIntent = new Intent(this,InformationActivity.class);
Log.w("I AM HERE ?!", infoIntent.toString());
//This log shows as "package name/.InformationActivity" -WHY?! is this the correct format? startActivity(infoIntent); //Fails here. return true;
default:
return super.onOptionsItemSelected(item);
}
And here's the relevant code in AndroidManifest.xml;
<activity
android:name="com.example.pingtest.InformationActivity"
android:label="@string/title_activity_information" >
</activity>
And here's the activity_information.xml file. Note that I am using linear layout in the Java code of the InformationActivity class. However, it doesn't show up here. And I don't need it as I use Java to add a new TextView there. Hope that makes sense.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".InformationActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RelativeLayout>
Here's the Logcat output
11-29 16:55:04.223: I/ViewRootImpl(2740): ViewRoot's KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_MENU, scanCode=139, metaState=0, flags=0x48, repeatCount=0, eventTime=60889321, downTime=60889321, deviceId=7, source=0x101 } to com.android.internal.policy.impl.PhoneWindow$DecorView{4286a2b8 V.E..... R....... 0,0-1080,1920}
11-29 16:55:04.293: I/ViewRootImpl(2740): ViewRoot's KeyEvent { action=ACTION_UP, keyCode=KEYCODE_MENU, scanCode=139, metaState=0, flags=0x48, repeatCount=0, eventTime=60889391, downTime=60889321, deviceId=7, source=0x101 } to com.android.internal.policy.impl.PhoneWindow$DecorView{4286a2b8 V.E..... R....... 0,0-1080,1920}
11-29 16:55:04.873: I/ViewRootImpl(2740): ViewRoot's Touch Event : Touch Down
11-29 16:55:04.933: I/ViewRootImpl(2740): ViewRoot's Touch Event : Touch UP
11-29 16:55:05.003: W/I AM HERE ?!(2740): YES02
11-29 16:55:05.023: W/I AM HERE ?!(2740): Intent { cmp=com.example.pingtest/.InformationActivity }
11-29 16:55:05.023: I/ActivityManager(2740): Timeline: Activity_launch_request id:com.example.pingtest time:60890123
11-29 16:55:05.093: D/AndroidRuntime(2740): Shutting down VM
11-29 16:55:05.093: W/dalvikvm(2740): threadid=1: thread exiting with uncaught exception (group=0x41822e48)
11-29 16:55:05.103: E/AndroidRuntime(2740): FATAL EXCEPTION: main
11-29 16:55:05.103: E/AndroidRuntime(2740): Process: com.example.pingtest, PID: 2740
11-29 16:55:05.103: E/AndroidRuntime(2740): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.pingtest/com.example.pingtest.InformationActivity}: java.lang.NullPointerException
11-29 16:55:05.103: E/AndroidRuntime(2740): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2202)
11-29 16:55:05.103: E/AndroidRuntime(2740): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2252)
11-29 16:55:05.103: E/AndroidRuntime(2740): at android.app.ActivityThread.access$800(ActivityThread.java:139)
11-29 16:55:05.103: E/AndroidRuntime(2740): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1200)
11-29 16:55:05.103: E/AndroidRuntime(2740): at android.os.Handler.dispatchMessage(Handler.java:102)
11-29 16:55:05.103: E/AndroidRuntime(2740): at android.os.Looper.loop(Looper.java:136)
11-29 16:55:05.103: E/AndroidRuntime(2740): at android.app.ActivityThread.main(ActivityThread.java:5103)
11-29 16:55:05.103: E/AndroidRuntime(2740): at java.lang.reflect.Method.invokeNative(Native Method)
11-29 16:55:05.103: E/AndroidRuntime(2740): at java.lang.reflect.Method.invoke(Method.java:515)
11-29 16:55:05.103: E/AndroidRuntime(2740): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
11-29 16:55:05.103: E/AndroidRuntime(2740): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)
11-29 16:55:05.103: E/AndroidRuntime(2740): at dalvik.system.NativeStart.main(Native Method)
11-29 16:55:05.103: E/AndroidRuntime(2740): Caused by: java.lang.NullPointerException
11-29 16:55:05.103: E/AndroidRuntime(2740): at com.example.pingtest.InformationActivity.onCreate(InformationActivity.java:24)
11-29 16:55:05.103: E/AndroidRuntime(2740): at android.app.Activity.performCreate(Activity.java:5275)
11-29 16:55:05.103: E/AndroidRuntime(2740): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-29 16:55:05.103: E/AndroidRuntime(2740): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2166)
11-29 16:55:05.103: E/AndroidRuntime(2740): ... 11 more
This is the InformationActivity.java file. I don't see any errors here.
public class InformationActivity extends Activity {
LinearLayout layout2;
TextView label2;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//readTask = new ReadTask();
Typeface type = Typeface.createFromAsset(getAssets(),"fonts/41370_WINDSORCONDENSED.TTF");
layout2=new LinearLayout(this);
label2.setText("Text here\n");
label2.setTextColor(Color.rgb(0,0,0));
label2.setBackgroundColor(Color.rgb(255,194,75));
label2.setTypeface(type);
setContentView(R.layout.activity_information);
}