java 启动新活动时出现空指针异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4543373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
null pointer exception when starting new activity
提问by acithium
Okay, I'm getting a null pointer exception when I start my third activity. Here is the LogCat message:
好的,当我开始第三个活动时,我收到了空指针异常。这是 LogCat 消息:
12-28 04:38:00.350: ERROR/AndroidRuntime(776): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.acithium.main/com.acithium.rss.ShowDescription}: java.lang.NullPointerException 12-28 04:38:00.350: ERROR/AndroidRuntime(776): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2401) 12-28 04:38:00.350: ERROR/AndroidRuntime(776): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417) 12-28 04:38:00.350: ERROR/AndroidRuntime(776): at android.app.ActivityThread.access00(ActivityThread.java:116) 12-28 04:38:00.350: ERROR/AndroidRuntime(776): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794) 12-28 04:38:00.350: ERROR/AndroidRuntime(776): at android.os.Handler.dispatchMessage(Handler.java:99) 12-28 04:38:00.350: ERROR/AndroidRuntime(776): at android.os.Looper.loop(Looper.java:123) 12-28 04:38:00.350: ERROR/AndroidRuntime(776): at android.app.ActivityThread.main(ActivityThread.java:4203) 12-28 04:38:00.350: ERROR/AndroidRuntime(776): at java.lang.reflect.Method.invokeNative(Native Method) 12-28 04:38:00.350: ERROR/AndroidRuntime(776): at java.lang.reflect.Method.invoke(Method.java:521) 12-28 04:38:00.350: ERROR/AndroidRuntime(776): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791) 12-28 04:38:00.350: ERROR/AndroidRuntime(776): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549) 12-28 04:38:00.350: ERROR/AndroidRuntime(776): at dalvik.system.NativeStart.main(Native Method) 12-28 04:38:00.350: ERROR/AndroidRuntime(776): Caused by: java.lang.NullPointerException 12-28 04:38:00.350: ERROR/AndroidRuntime(776): at com.acithium.rss.ShowDescription.onCreate(ShowDescription.java:48) 12-28 04:38:00.350: ERROR/AndroidRuntime(776): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123) 12-28 04:38:00.350: ERROR/AndroidRuntime(776): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364) 12-28 04:38:00.350: ERROR/AndroidRuntime(776): ... 11 more
Here is the section of code where I call the activity:
这是我调用活动的代码部分:
public void onItemClick(AdapterView parent, View v, int position, long id)
{
Log.i(tag,"item clicked! [" + feed.getItem(position).getTitle() + "]");
Intent itemintent = new Intent(this,com.acithium.rss.ShowDescription.class);
//Intent itemintent = new Intent();
//itemintent.setClassName("com.acithium.main", "com.acithium.rss.ShowDescription");
Bundle b = new Bundle();
b.putString("title", feed.getItem(position).getTitle());
b.putString("description", feed.getItem(position).getDescription());
b.putString("link", feed.getItem(position).getLink());
itemintent.putExtra("android.intent.extra.INTENT", b);
startActivityForResult(itemintent,0);
}
And here is new activity class that is called:
这是名为的新活动类:
public class ShowDescription extends Activity
{
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.showdescription);
String theStory = null;
Intent startingIntent = getIntent();
if (startingIntent != null)
{
Bundle b = startingIntent.getBundleExtra("android.intent.extra.INTENT");
if (b == null)
{
theStory = "bad bundle?";
}
else
{
theStory = b.getString("title") + "\n\n" + b.getString("description") + "\n\nMore information:\n" + b.getString("link");
}
}
else
{
theStory = "Information Not Found.";
}
TextView db= (TextView) findViewById(R.id.storybox);
db.setText(theStory);
Button backbutton = (Button) findViewById(R.id.back);
backbutton.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
finish();
}
});
}
}
here is the showdescription.xml file:
这是 showdescription.xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ScrollView android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:autoLink="all"
android:text="story goes here ...."
android:id="@+id/storybox" />
<Button
android:layout_height="wrap_content"
android:text="Back"
android:id="@+id/back"
android:layout_width="100px"
android:layout_marginLeft="100px"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
回答by Vinay Pai
This is your clue, from the stack trace:
这是您的线索,来自堆栈跟踪:
12-28 03:47:21.670: ERROR/AndroidRuntime(862): Caused by: java.lang.NullPointerException
12-28 03:47:21.670: ERROR/AndroidRuntime(862): at com.acithium.rss.ShowDescription.onCreate(ShowDescription.java:47)
What's line 47 in ShowDescription.java?
ShowDescription.java 中的第 47 行是什么?
回答by Vinay Pai
If I understood the code right, you should invoke the findViewById() of the showdescription view that you set for the activity and not of the activity, because both, button and textview, seem to be part of that view:
如果我理解正确的代码,您应该调用为活动设置的 showdescription 视图的 findViewById(),而不是为活动设置的,因为按钮和文本视图似乎都是该视图的一部分:
super.onCreate(icicle);
View showdescription = this.findViewById(R.id.showdescription);
setContentView(showdescription);
// ..
TextView db = (TextView)showdescription.findViewById(R.id.storybox);
db.setText(theStory);
Button backbutton = (Button)showdescription.findViewById(R.id.back);
backbutton.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
finish();
}
});
回答by garima
what is the 47 line in your ShowDescription code? I think you are passing a null cursor.
ShowDescription 代码中的第 47 行是什么?我认为您正在传递一个空游标。