Android java.land.nullpointer 异常或在 onClick(View v) 处找不到源
我正在尝试运行一个简单的 Android 教程。我已经写了一些,并且预计源未找到错误会有点误导,所以我忽略它,但这是堆栈跟踪和一些代码。它似乎是一些空指针,但我似乎无法解决它。
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
}
};
这
和错误
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/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
08-03 21:17:59.915: ERROR/power(852): Failed setting last user activity: g_error=0
是来自 LogCat 的正确堆栈跟踪吗?错误在哪里?
下面的 main.xml 和 string.xml 是初始化按钮的地方。我这样说对吗?希望这个 html 能够显示。
<!-- 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>
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/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
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
确保您的获取和设置按钮不为空。你确定这些是你的按钮的 id 吗?如果这些 ID 尝试
清理
您的项目,甚至可能手动删除R.java
生成的文件,因为有时它会变得混乱。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 theR.java
generated file, because sometimes it gets messed up.是的,这是来自 Logcat 的正确 StackTrace。最有用的信息可以在这些行中找到
Logcat 告诉您,您的 RandomQuotes 类的第 23 行有一个 NullPointerExcepotion。这是代码中的第 23 行,
由于您收到 NullPointerException,此时
setButton
必须为 null,这意味着它没有通过Button setButton = (Button)findViewById(R. id.go);
。检查您的 main.xml 文件,确保您已声明具有属性
android:id="@+id/go"
的按钮。Yes, that is the correct StackTrace from the Logcat. The most useful information is found in these lines
The Logcat is telling you that you have a NullPointerExcepotion on line 23 of your RandomQuotes class. This is line 23 from your code
Since you are getting a NullPointerException,
setButton
must be null at this point, meaning it wasn't properly initialized byButton 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"
.我注意到你的类中有一个正在初始化的 DBAdapter。几天前,我在使用数据库适配器时遇到了类似的空指针异常问题。
基本上,问题可能是数据库在活动生命周期中的某个位置被关闭,或者没有正确打开 - 在 DBAdapter 尝试访问此活动中的数据库之前。
所以我的建议是 - 注释掉应用程序中的所有数据库 close() 调用,并检查数据库是否已打开并打开。在 DBAdapter 尝试访问它之前初始化并再次尝试运行。
就我而言,问题是在我的调用活动的 Activity.OnStop() 函数中,我调用了 close() 数据库。现在,只要子活动隐藏了某个活动,就会调用 OnStop() ,因此当我的子活动中的 DBAdapter 尝试访问数据库时,它会遇到错误并显示错误。引发空指针异常
这是活动生命周期 - http://developer.android.com/guide/topics/fundamentals/activities.html#Lifecycle
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