开发者

Android java.land.nullpointer exception or source not found at onClick(View v)

I am trying to run a simple Android tutorial. I have written a few already, and have come to expect the source not found error to be a bit misleading, so I ignore it, but this is a stack trace and some code. It appears to be some null pointer, but I can't seem to work it out.

package com.rlee.randomquotes;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class RandomQuotes extends Activity {
    DBAdapter db = new DBAdapter(this);
    EditText Quote;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        // Capture our button from layout
        Button setButton = (Button)findViewById(R.id.go);
        Button getButton = (Button)findViewById(R.id.genRan);
        // Register the onClick listener with the implementation above
        setButton.setOnClickListener(mAddListener);
        getButton.setOnClickListener(mAddListener);
    }

    // Create an anonymous implementation of OnClickListener.

    private OnClickListener mAddListener = new OnClickListener()
    {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
        }
    };

}

and the error

08-03 21:08:18.325: ERROR/jdwp(2383): Failed sending reply to debugger: Broken pipe
08-03 21:08:37.795: ERROR/AndroidRuntime(2383): FATAL EXCEPTION: main
08-03 21:08:37.795: ERROR/AndroidRuntime(2383): java.lang.RuntimeException: Unable to start     activity ComponentInfo{com.rlee.randomquotes/com.rlee.randomquotes.RandomQuotes}:  java.lang.NullPointerException
08-03 21:08:37.795: ERROR/AndroidRuntime(2383):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
08-03 21:08:37.795: ERROR/AndroidRuntime(2383):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-03 21:08:37.795: ERROR/AndroidRuntime(2383):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-03 21:08:37.795: ERROR/AndroidRuntime(2383):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-03 21:08:37.795: ERROR/AndroidRuntime(2383):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-03 21:08:37.795: ERROR/AndroidRuntime(2383):     at android.os.Looper.loop(Looper.java:130)
08-03 21:08:37.795: ERROR/AndroidRuntime(2383):     at android.app.ActivityThread.main(ActivityThread.java:3683)
08-03 21:08:37.795: ERROR/AndroidRuntime(2383):     at java.lang.reflect.Method.invokeNative(Native Method)
08-03 21:08:37.795: ERROR/AndroidRuntime(2383):     at java.lang.reflect.Method.invoke(Method.java:507)
08-03 21:08:37.795: ERROR/AndroidRuntime(2383):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-03 21:08:37.795: ERROR/AndroidRuntime(2383):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-03 21:08:37.795: ERROR/AndroidRuntime(2383):     at dalvik.system.NativeStart.main(Native Method)
08-03 21:08:37.795: ERROR/AndroidRuntime(2383): Caused by: java.lang.NullPointerException
08-03 21:08:37.795: ERROR/AndroidRuntime(2383):     at com.rlee.randomquotes.RandomQuotes.onCreate(RandomQuotes.java:23)
08-03 21:08:37.795: ERROR/AndroidRunti开发者_如何学JAVAme(2383):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-03 21:08:37.795: ERROR/AndroidRuntime(2383):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-03 21:08:37.795: ERROR/AndroidRuntime(2383):     ... 11 more
08-03 21:17:59.915: ERROR/power(852): Failed setting last user activity: g_error=0

Is this the correct stack trace from LogCat? And where is the error?

the main.xml, and string.xml below are where the buttons are initialized. Am I correct in saying this. Hopefully this html will show.

<!-- language-all: lang-html --> <br/><pre><Button
android:id="@+id/go"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/press"
/>
<Button
android:id="@+id/genRan"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/genRan"
/></pre> <br/><pre>  <string name="press">Press Me!</string>
<string name="genRan">Generate Random Quote!</string> 


Be sure that your get and set buttons are not null. Are you sure that those are the ids of your buttons? If those are the ids try to Clean your project, maybe even manually delete the R.java generated file, because sometimes it gets messed up.


Yes, that is the correct StackTrace from the Logcat. The most useful information is found in these lines

08-03 21:08:37.795: ERROR/AndroidRuntime(2383): Caused by: java.lang.NullPointerException
08-03 21:08:37.795: ERROR/AndroidRuntime(2383):     at com.rlee.randomquotes.RandomQuotes.onCreate(RandomQuotes.java:23)
08-03 21:08:37.795: ERROR/AndroidRuntime(2383):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-03 21:08:37.795: ERROR/AndroidRuntime(2383):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-03 21:08:37.795: ERROR/AndroidRuntime(2383):     ... 11 more

The Logcat is telling you that you have a NullPointerExcepotion on line 23 of your RandomQuotes class. This is line 23 from your code

setButton.setOnClickListener(mAddListener);

Since you are getting a NullPointerException, setButton must be null at this point, meaning it wasn't properly initialized by Button setButton = (Button)findViewById(R.id.go);.

Check your main.xml file to make sure that you have declared a button with the attribute android:id="@+id/go".


I notice u have a DBAdapter being initialized in your class. I faced similar null pointer exception issues a few days back when using database adapters.

Basically, the issue may be that the Database is either being closed somewhere in the Activity Lifecycle or isn't being opened properly - BEFORE your DBAdapter tries to access the database in this activity.

So my suggestion is - comment out any database close() calls in your application and check that the database is opened open & initialized before the DBAdapter tries to access it and try running again.

In my case, the issue was that in the Activity.OnStop() function in my calling activity, I had put in a call to close() the database. Now, OnStop() is called whenever an activity is hidden by a sub-activity, so when the DBAdapter in my sub-activity tried to access the database, it hit an error & raised null pointer exception

Here's the Activity Lifecycle -http://developer.android.com/guide/topics/fundamentals/activities.html#Lifecycle

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜