【发布时间】:2010-12-28 03:01:05
【问题描述】:
好的,当我开始我的第三个活动时,我遇到了一个空指针异常。这是 LogCat 消息:
12-28 04:38:00.350: 错误/AndroidRuntime(776): java.lang.RuntimeException: 无法启动活动 ComponentInfo{com.acithium.main/com.acithium.rss.ShowDescription}: java.lang.NullPointerException 12-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(Native Method) 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,"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);
}
这是一个新的活动类,被称为:
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();
}
});
}
}
这里是 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>
【问题讨论】:
-
好吧,我找到了解决方法。最初我将这些类放在一个单独的包中,但我将它们全部移到同一个包中,并删除了与该包关联的未使用的 R 文件。一旦我这样做了,一切都开始工作了。我不会称之为“解决”,而是称之为解决方法。
-
啊!这就解释了!如果它在不同的包中,那么您所指的“R”最终不是您需要的 R!嗯,第二个想法可能不完全正确......那将无法编译。
-
是的,我也不明白。当它们在 2 秒包中时,我为 R 文件添加了导入语句,但我仍然得到空指针。我想我现在就这样吧。谢谢
标签: java android nullpointerexception