使用startActivity时出错

时间:2012-03-06 21:17:06

标签: android

我有以下代码在点击时崩溃。当startActivity(...)被注释掉时,它不会崩溃(但不起作用)。但活动是空的!我对将要发生的事情毫无头绪。取出Bundle不起作用。

有人有什么想法吗?

在RSSReader.java中

 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,ShowDescription.class);

     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());
     b.putString("pubdate", feed.getItem(position).getPubDate());

     itemintent.putExtra("android.intent.extra.INTENT", b);

     startActivity(itemintent);
 }

ShowDescription.java:

import android.app.Activity;
public class ShowDescription extends Activity 
{
}

3 个答案:

答案 0 :(得分:1)

我认为您在活动中至少需要以下内容:

protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);


     }

答案 1 :(得分:0)

确保已将ShowDescription活动添加到清单文件。

答案 2 :(得分:0)

您必须在清单中声明第二个活动,如下所示:

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" 
    android:theme="@android:style/Theme.Light">
    <activity
        android:label="@string/app_name"
        android:name=".MainActivity" >
        <intent-filter >
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:label="@string/app_name"
        android:name=".ShowDescription" >
    </activity>
</application>

并确保它是一个有效的活动,如Sid所说:

import android.app.Activity;
public class ShowDescription extends Activity 
{
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //grab your Bundle stuff if you want to handle it that way:
        String title = savedInstanceState.getString("title");
        //etc.

        //Inflate/create your layouts here and set the contentview.
        setContentView(showDescLayout);
    }
}