Reputation: 62464
When I comment out calc1.setOnClickListener(buttonListener);
this runs fine, when I allow it to run, it crashes my script. How can I figure out what's going on? Logcat shows a bunch of red, but I'm not sure how to read it...
package com.example.contactwidget;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ContactWidget extends Activity {
private static final int HELLO_ID = 1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button calc1 = (Button) findViewById(R.id.calc_button_1);
OnClickListener buttonListener = new View.OnClickListener() {
public void onClick(View v) {
}
};
calc1.setOnClickListener(buttonListener);
setContentView(R.layout.main);
}
}
update
Stack trace...
E/AndroidRuntime( 339): FATAL EXCEPTION: main
E/AndroidRuntime( 339): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.contactwidget/com.example.contactwidget.ContactWidget}: java.lang.NullPointerException
E/AndroidRuntime( 339): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
E/AndroidRuntime( 339): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
E/AndroidRuntime( 339): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
E/AndroidRuntime( 339): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
E/AndroidRuntime( 339): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 339): at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 339): at android.app.ActivityThread.main(ActivityThread.java:4627)
E/AndroidRuntime( 339): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 339): at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime( 339): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
E/AndroidRuntime( 339): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
E/AndroidRuntime( 339): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 339): Caused by: java.lang.NullPointerException
E/AndroidRuntime( 339): at com.example.contactwidget.ContactWidget.onCreate(ContactWidget.java:41)
E/AndroidRuntime( 339): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
E/AndroidRuntime( 339): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
E/AndroidRuntime( 339): ... 11 more
Upvotes: 1
Views: 9236
Reputation:
ccheneson's answer is likely correct for your specific case.
According to your stack trace you're running in to a NullPointerException. A great way to make it easier to track these down is to add a Java exception breakpoint for all uncaught NullPointerExceptions. (I also suggest breaking on ArrayIndexOutOfBoundsException, IllegalArgumentException, and IllegalStateException).
To do this go to the Debug perspective (Window -> Open Perspective -> Debug or Other and then Debug), find the Breakpoints pane (it is by default combined with the Variables pane, top right) and then click the J! icon. Next time you run in to this exception you'll be able to see exactly where it is happening, instead of having to sift through the RuntimeException output.
Upvotes: 3
Reputation: 49410
One thing is that you call setContentView(R.layout.main);
at the end.
You have to set the content view as above before referencing any views in a xml layout
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button calc1 = (Button) findViewById(R.id.calc_button_1);
OnClickListener buttonListener = new View.OnClickListener() {
public void onClick(View v) {
}
};
calc1.setOnClickListener(buttonListener);
}
Since you try to find a view by id without specifying any layout before (with setContentView(R.layout.main);
), it returns a null object of calc1
.
Upvotes: 1
Reputation: 385
Someone else probably knows how to fix this, but I'm too new to know - I can help with debugging, though.
If you want to debug through LogCat, I suggest creating a filter. You can do so by clicking the green + in the LogCat view and entering what rules you want to filter by. What I do is have all my log statements under a certain flag [ Log.d(DEBUGTAG, message); ] so that I set my filter to just look for those tags and not have anything else show up.
If you want to debug by going step-by-step through the app, you can set a breakpoint by double-clicking the bar to the left of the line number for where you want it to stop - a small blue dot should show up. Then when you run the program using the Debug button, it will run up until it hits that breakpoint. You can now use the F6 key to step to the next line. F5 will step into the function. You can also look at the current values of objects, among other things.
Hope this helps - as I said, I'm pretty new at this too. A lot of learning-as-I-go.
Upvotes: 2