【问题标题】:null pointer exception when starting new activity开始新活动时出现空指针异常
【发布时间】: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


【解决方案1】:

这是你的线索,来自堆栈跟踪:

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)

ShowDescription.java 中的第 47 行是什么?

【讨论】:

  • //第 47 行是这两行之间的空白行。 db.setText(theStory); //空行Button backbutton = (Button) findViewById(R.id.back);
  • acithium:这没有任何意义。收到该错误后,您必须编辑源代码。尝试重新构建并再次运行它,这样我们就可以准确地判断它在哪一行。
  • 好的,现在它说 db.setText(theStory);是导致问题的线路。
  • 我同意 atk。从查看代码来看,似乎没有任何路径可以导致 theStory 在那时为空,所以它必须是 db.您能否再次检查 TextView 的 ID 是否正确(或发布您的 layout/showdescription.xml 文件)
  • 已发布,希望这能解决这个问题。
【解决方案2】:

如果我对代码的理解正确,您应该调用您为活动而不是活动设置的 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();
    }
});

【讨论】:

    【解决方案3】:

    您的 ShowDescription 代码中的第 47 行是什么?我认为您正在传递一个空光标。

    【讨论】:

    • 我发布了新的 logcat,显示它现在位于第 48 行。那行代码是:db.setText(theStory);
    猜你喜欢
    • 1970-01-01
    • 2017-07-09
    • 2016-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-12
    • 1970-01-01
    • 2011-04-07
    相关资源
    最近更新 更多