-3

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);
    }
Chisko
  • 3,092
  • 6
  • 27
  • 45
Zac1
  • 208
  • 7
  • 34

1 Answers1

1

label2.setText is called before you have obtained a reference to label2. First, set the activity layout by calling setContentView(R.layout.activity_information);. Then use findViewById to obtain a reference to the appropriate TextView.

Edit: full fix:

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_information);

    //readTask = new ReadTask();
    Typeface type = Typeface.createFromAsset(getAssets(),"fonts/41370_WINDSORCONDENSED.TTF");

    label2 = (TextView) findViewById(R.id.label_2_id_here);
    label2.setText("Text here\n");
    label2.setTextColor(Color.rgb(0,0,0));
    label2.setBackgroundColor(Color.rgb(255,194,75));
    label2.setTypeface(type);
}
stkent
  • 19,772
  • 14
  • 85
  • 111
  • Hi, I am using a LinearLayout in my MainActivity too. I have a "label1" TextView there. I did not have to reference it to an ID there. That's why you see my relativelayout file (activity_information.xml) without any entries. I guess I don't understand how it works in the MainActivity without referencing, but would fail here. WHERE should I find my label_2_id?! – Zac1 Nov 29 '14 at 22:15
  • I would have to see the code for MainActivity and its corresponding layout to comment more, but that sounds like it may belong in a separate question. – stkent Nov 29 '14 at 22:16
  • Additionally, the LinearLayout you instantiate inside the `onCreate` of InformationActivity is not actually being used anywhere - it's not causing problems, but it's also not doing any good. – stkent Nov 29 '14 at 22:18
  • You added the id to the TextView defined in the activity_information.xml layout file, correct? Also, remove the LinearLayout instantiation from your InformationActivity (see my edited answer). – stkent Nov 29 '14 at 22:30
  • Ah, throwing the setContentView(R.layout.activity_information); at the beginning fixed it. More explanation please? WHy does it have to be at the top? – Zac1 Nov 29 '14 at 22:33
  • Calling [`setContentView`](http://developer.android.com/reference/android/app/Activity.html#setContentView%28android.view.View%29) tells the Activity which layout file should be used to construct its layout. Before you call that, the Activity has no idea which view(s) it will be displaying... which means you also can't search for (using `findViewById`) or interact with those views. – stkent Nov 29 '14 at 22:35