1

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
2
  • "Logcat shows a bunch of red, but I'm not sure how to read it..." post the stack trace here and we can help you read it. Commented Dec 24, 2010 at 20:04
  • Why would you -1 this? Epic fail Commented Dec 24, 2010 at 20:51

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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.

3 Comments

The thing is, I'm not logging anything. Is that a problem? I guess I just assumed it would throw a parse error or something simpler like in PHP, but that's definitely not the case.
Your code is pre-compiled, unlike PHP which is interpreted, so if you got it to the point that it runs then there isn't a problem parsing your code. It's throwing an exception that will show up as a stack trace in LogCat. You just need to take the time to read that stack trace and it should tell you the exact line number that is causing the problem.
All the line numbers are not in my code except for 1.... I hate to just ignore the others... it looks like calc1 can't have .setOnClickListener() executed on it, if not then how do I add a listener for it?
1

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 .

2 Comments

@ccheneson and how can you figure out the location of the error from this stacktrace? If im making XML layouts, the stacktraces seem useless to me.
@sydd : the stacktrace will just give you where the NPE occurred. From there, you have to go up and check if you have done everything correctly such as missing/wrong place of setContentView, if you re using findViewById from the correct context...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.