我有一个扩展为 BaseAdapter 的适配器类,它通过意图启动另一个活动。另一个活动给了我错误:
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