Squall Leonhart
Squall Leonhart

Reputation: 117

Android: app crashes on starting new activity

Basicaly, I want to start an activity that prints out the input string and an image under.

In activity_display_message.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical" >

      <TextView 
            android:text="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            />

        <ImageView
        android:id="@+id/imageView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/serious" />    


</LinearLayout>

This is the code in OnCreate():

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

    setContentView(R.layout.activity_display_message);  
    Intent intent = getIntent();

    String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

    ImageView image2 = (ImageView) findViewById(R.id.imageView2);


    // Create the text view

    TextView textView = (TextView)findViewById(R.id.textView);
    //textView.setTextSize(20);

    textView.setText(message);
    // Set the text view as the activity layout   


    // Show the Up button in the action bar.
    setupActionBar();
}

Is there anything wrong with this code?

EDIT: Not sure how to get the stack trace, but I just found these lines in Catlog:

07-05 12:26:53.677: E/AppsCustomizePagedView(611): Widget ComponentInfo{com.th.android.widget.gTabsimiClock/com.th.android.widget.gTabsimiClock.GTabsimiClock5x1} can not fit on this device (736, 144)
07-05 12:26:53.692: E/AppsCustomizePagedView(611): Widget ComponentInfo{at.abraxas.powerwidget.free/at.abraxas.powerwidget.free.SettingsAppWidgetProvider_5} can not fit on this device (760, 80)
07-05 12:26:57.958: E/AudioCache(131): Error 1, -1004 occurred
07-05 12:26:58.146: E/AudioCache(131): Error 1, -1004 occurred
07-05 12:26:58.630: E/AudioCache(131): Error 1, -1004 occurred
07-05 12:27:07.833: E/AudioCache(131): Heap size overflow! req size 1052672, max size: 1048576

Is this the stack trace?

07-05 12:41:10.058: E/TAG(22474): STACKTRACE
07-05 12:41:10.058: E/TAG(22474): java.lang.NullPointerException
07-05 12:41:10.058: E/TAG(22474):   at com.example.myfirstapp.DisplayMessageActivity.onCreate(DisplayMessageActivity.java:38)
07-05 12:41:10.058: E/TAG(22474):   at android.app.Activity.performCreate(Activity.java:5104)
07-05 12:41:10.058: E/TAG(22474):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-05 12:41:10.058: E/TAG(22474):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
07-05 12:41:10.058: E/TAG(22474):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-05 12:41:10.058: E/TAG(22474):   at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-05 12:41:10.058: E/TAG(22474):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-05 12:41:10.058: E/TAG(22474):   at android.os.Handler.dispatchMessage(Handler.java:99)
07-05 12:41:10.058: E/TAG(22474):   at android.os.Looper.loop(Looper.java:137)
07-05 12:41:10.058: E/TAG(22474):   at android.app.ActivityThread.main(ActivityThread.java:5041)
07-05 12:41:10.058: E/TAG(22474):   at java.lang.reflect.Method.invokeNative(Native Method)
07-05 12:41:10.058: E/TAG(22474):   at java.lang.reflect.Method.invoke(Method.java:511)
07-05 12:41:10.058: E/TAG(22474):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-05 12:41:10.058: E/TAG(22474):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-05 12:41:10.058: E/TAG(22474):   at dalvik.system.NativeStart.main(Native Method)

Stack trace when crashes at runtime: (the one above was obtained by doing try / catch)

07-05 12:52:24.386: E/AndroidRuntime(25496): FATAL EXCEPTION: main
07-05 12:52:24.386: E/AndroidRuntime(25496): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myfirstapp/com.example.myfirstapp.DisplayMessageActivity}: java.lang.NullPointerException
07-05 12:52:24.386: E/AndroidRuntime(25496):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
07-05 12:52:24.386: E/AndroidRuntime(25496):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-05 12:52:24.386: E/AndroidRuntime(25496):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-05 12:52:24.386: E/AndroidRuntime(25496):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-05 12:52:24.386: E/AndroidRuntime(25496):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-05 12:52:24.386: E/AndroidRuntime(25496):    at android.os.Looper.loop(Looper.java:137)
07-05 12:52:24.386: E/AndroidRuntime(25496):    at android.app.ActivityThread.main(ActivityThread.java:5041)
07-05 12:52:24.386: E/AndroidRuntime(25496):    at java.lang.reflect.Method.invokeNative(Native Method)
07-05 12:52:24.386: E/AndroidRuntime(25496):    at java.lang.reflect.Method.invoke(Method.java:511)
07-05 12:52:24.386: E/AndroidRuntime(25496):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-05 12:52:24.386: E/AndroidRuntime(25496):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-05 12:52:24.386: E/AndroidRuntime(25496):    at dalvik.system.NativeStart.main(Native Method)
07-05 12:52:24.386: E/AndroidRuntime(25496): Caused by: java.lang.NullPointerException
07-05 12:52:24.386: E/AndroidRuntime(25496):    at com.example.myfirstapp.DisplayMessageActivity.onCreate(DisplayMessageActivity.java:38)
07-05 12:52:24.386: E/AndroidRuntime(25496):    at android.app.Activity.performCreate(Activity.java:5104)
07-05 12:52:24.386: E/AndroidRuntime(25496):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-05 12:52:24.386: E/AndroidRuntime(25496):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
07-05 12:52:24.386: E/AndroidRuntime(25496):    ... 11 more

message is not null. When I change OnCreate to below, the message is printed: (but of course, not the image)

// Create the text view

//TextView textView = (TextView)findViewById(R.id.textView);
TextView textView = new TextView(this);
textView.setTextSize(20);

textView.setText(message);
setContentView(textView);

ANSWER: Found the mistake: android:text="@+id/textView" . Should be android:id, not android:text ...

But thank you all for your help. You guys have made the android programming experience for beginners like me a lot better.

Upvotes: 3

Views: 24920

Answers (4)

Raghunandan
Raghunandan

Reputation: 133560

You need to set the content to the activity first.

You can findViewById of the current view hierarchy set to the activity. Since you have not set the content to the activity you will get NullPointerException if you try to initialize views.

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message); 
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
ImageView image2 = (ImageView) findViewById(R.id.imageView2);
TextView textView = (TextView)findViewById(R.id.textView);
textView.setTextSize(20);
textView.setText(message);
....
}

Edit:

As Squonk commented setText(CharSequence text) checks for null

From

http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.2.2_r1/android/widget/TextView.java#TextView.setText%28java.lang.CharSequence%2Candroid.widget.TextView.BufferType%29

The implementation looks like

3561    private void setText(CharSequence text, BufferType type,
3562                         boolean notifyBefore, int oldlen) {
3563        if (text == null) { // checking for null
3564            text = ""; 
3565        }   
        ..... 

So line 38 is textView.setText(message) which causes NPE. So the textView is not initialized which causes NPE.

Further to the update and Squonk comment

textview can be null bcoz of specifying the wrong (or an invalid) R.id OR in some cases if the R.java file is corrupt. In the latter case, using Project -> Clean should fix things.

Also this

android:text="@+id/textView" // this is actually setting text to textview.

You were missing

android:id="@+id/textView" // textview id attribute missing

SO when you try to initialize

TextView textView = (TextView)findViewById(R.id.textView);
// there is no textview with id  textView which caused NPE

Upvotes: 6

Niraj Adhikari
Niraj Adhikari

Reputation: 1728

Did you declare your activity in the manifest file? please check that too.

Upvotes: 4

Mufazzal
Mufazzal

Reputation: 883

Put setContentView(R.layout.activity_display_message); line at the top of onCreate method.

    @Override
    public void onCreate() {
    super.onCreate();
    setContentView(R.layout.activity_display_message); 

    ......
    rest code

    }

Upvotes: 0

Permita
Permita

Reputation: 5493

setContentView should be called before creating Imageview object.

Upvotes: 1

Related Questions