启动新活动时出现空指针异常 [英] null pointer exception when starting new activity
本文介绍了启动新活动时出现空指针异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
好的,当我开始我的第三个活动时,我收到了一个空指针异常.这是 LogCat 消息:
<前>12-28 04:38:00.350:错误/AndroidRuntime(776):java.lang.RuntimeException:无法启动活动组件信息{com.acithium.main/com.acithium.rss.ShowDescription}:java.lang.NullPointerException12-28 04:38:00.350: 错误/AndroidRuntime(776): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2401)12-28 04:38:00.350: 错误/AndroidRuntime(776): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)12-28 04:38:00.350: 错误/AndroidRuntime(776): 在 android.app.ActivityThread.access$2100(ActivityThread.java:116)12-28 04:38:00.350: 错误/AndroidRuntime(776): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)12-28 04:38:00.350: 错误/AndroidRuntime(776): 在 android.os.Handler.dispatchMessage(Handler.java:99)12-28 04:38:00.350: 错误/AndroidRuntime(776): 在 android.os.Looper.loop(Looper.java:123)12-28 04:38:00.350: 错误/AndroidRuntime(776): 在 android.app.ActivityThread.main(ActivityThread.java:4203)12-28 04:38:00.350:错误/AndroidRuntime(776):在java.lang.reflect.Method.invokeNative(本地方法)12-28 04:38:00.350: 错误/AndroidRuntime(776): 在 java.lang.reflect.Method.invoke(Method.java:521)12-28 04:38:00.350: 错误/AndroidRuntime(776): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)12-28 04:38:00.350: 错误/AndroidRuntime(776): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)12-28 04:38:00.350:错误/AndroidRuntime(776):在dalvik.system.NativeStart.main(本地方法)12-28 04:38:00.350:错误/AndroidRuntime(776):由:java.lang.NullPointerException引起12-28 04:38:00.350: 错误/AndroidRuntime(776): 在 com.acithium.rss.ShowDescription.onCreate(ShowDescription.java:48)12-28 04:38:00.350: 错误/AndroidRuntime(776): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)12-28 04:38:00.350: 错误/AndroidRuntime(776): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)12-28 04:38:00.350: 错误/AndroidRuntime(776): ... 11 更多这是我调用活动的代码部分:
public void onItemClick(AdapterView parent, View v, int position, long id){Log.i(tag,"物品被点击![" + feed.getItem(position).getTitle() + "]");Intent itemintent = new Intent(this,com.acithium.rss.ShowDescription.class);//意图 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);}
这里是新的活动类,称为:
公共类 ShowDescription 扩展 Activity{公共无效onCreate(捆绑冰柱){super.onCreate(冰柱);setContentView(R.layout.showdescription);字符串 theStory = null;意图startingIntent = getIntent();if (startingIntent != null){Bundle b = startingIntent.getBundleExtra("android.intent.extra.INTENT");如果 (b == 空){theStory = "坏包?";}别的{theStory = b.getString("title") + "\n\n" + b.getString("description") + "\n\n更多信息:\n" + b.getString("link");}}别的{theStory = "未找到信息.";}TextView db= (TextView) findViewById(R.id.storybox);db.setText(theStory);按钮 backbutton = (Button) findViewById(R.id.back);backbutton.setOnClickListener(new Button.OnClickListener(){public void onClick(View v){结束();}});}}
这是 showdescription.xml 文件:
<文本视图android:layout_width="fill_parent"android:layout_height="wrap_content"机器人:自动链接=所有"android:text="故事在这里......"android:id="@+id/storybox"/><按钮android:layout_height="wrap_content"机器人:文本=返回"android:id="@+id/back"android:layout_width="100px"android:layout_marginLeft="100px"/></LinearLayout></ScrollView></LinearLayout>
解决方案
这是你的线索,来自堆栈跟踪:
12-28 03:47:21.670: ERROR/AndroidRuntime(862): 引起:java.lang.NullPointerException12-28 03:47:21.670: 错误/AndroidRuntime(862): 在 com.acithium.rss.ShowDescription.onCreate(ShowDescription.java:47)
ShowDescription.java 中的第 47 行是什么?
Okay, I'm getting a null pointer exception when I start my third activity. Here is the LogCat message:
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.access$2100(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:
<?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>
解决方案
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?
这篇关于启动新活动时出现空指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文