setContentView()上的NullPointerException但它存在

时间:2014-10-10 23:12:59

标签: android android-activity nullpointerexception setcontentview

我有一个扩展到BaseAdapter的适配器类,它通过intent启动另一个活动。另一项活动给了我错误:

10-10 23:58:57.171: E/AndroidRuntime(3651): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.migdinny.passman/com.migdinny.passman.DataActivity}: java.lang.NullPointerException

...

10-10 23:58:57.171: E/AndroidRuntime(3651): Caused by: java.lang.NullPointerException
10-10 23:58:57.171: E/AndroidRuntime(3651):     at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:77)
10-10 23:58:57.171: E/AndroidRuntime(3651):     at com.migdinny.passman.DataActivity.onCreate(DataActivity.java:20)

以下是代码:

启动新活动的代码(这是在新听众中)

            @Override
        public void onClick(View view) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(context, DataActivity.class);
            intent.putExtra("category", category);
            context.startActivity(intent);
        }

此代码是上述代码启动的新活动

public class DataActivity extends ActionBarActivity {

String category;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); // i've tried this line on all places and it doesnt fix the problem, after the setcontentview, before, etc
    Intent intent = getIntent(); // this works

    category = intent.getExtras().getString("category"); // this exists 

    setContentView(R.layout.activity_data); // THIS IS THE LINE 20 OF DATA ACTIVITY and of course that the activity_data layout exists. the eclipse does not warn me anything.

}
........

编辑:

这是activity_data.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.migdinny.passman.DataActivity" >

<TextView
    android:id="@+id/test"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
/>
</RelativeLayout>

onClick上使用的类别变量:

final String category = list.get(position).getCatname(); // on the beginning of getView method and i think the error is not here

1 个答案:

答案 0 :(得分:0)

你有自定义主题吗?我记得在某个地方读过一个与问题有关并且与思想有关的人,如果你试图删除它以查看它是否相关(值得尝试)