亲爱的 JAVA 程序员,
打开我的一项活动时,我在 onCreate 方法中遇到了 NullPointerException。这就是我的 onCreate 方法的样子:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
overridePendingTransition(R.anim.push_left_in, R.anim.fadeout);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.category_page);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar);
// get intent data
Intent i = getIntent();
// Selected category id
int position = i.getExtras().getInt("id");
Categories categories = new Categories();
Categories.CategoriesFromDatabase textAdapter1 = categories.new CategoriesFromDatabase(this);
TextView pageTitle = (TextView) findViewById(R.id.category_page_title);
if (textAdapter1.mCatIds[position] == null){
pageTitle.setText("error parsing pagename");
} else {
pageTitle.setText(textAdapter1.mCatIds[position]);
}
ListView pageContent = (ListView) findViewById(R.id.category_page_content);
pageContent.setAdapter(new LinksPerCategory(this));
pageContent.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
// Sending image id to FullScreenActivity
Intent i = new Intent(getApplicationContext(), viewPageItem.class);
// passing array index
i.putExtra("id", position);
startActivity(i);
}
});
}
此活动的主要目的是从过去的活动中获取类别的名称“mCatIds[position]”并将其放在当前活动的顶部。我怀疑这里给出了 NullPointerException,因此我将 if 语句放在了行周围,但没有任何效果。由于过去的活动显示内容,'mCatIds[position]' 可能有一个值。问题可能出在其他地方吗?还是我错过了一些重要的事情?
我期待着您的回复。
编辑:这是堆栈跟踪,很抱歉没有直接发布:
05-27 08:37:41.845: E/AndroidRuntime(31413): FATAL EXCEPTION: main
05-27 08:37:41.845: E/AndroidRuntime(31413): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.abnwiki/com.example.abnwiki.viewCategoryPage}: java.lang.NullPointerException
05-27 08:37:41.845: E/AndroidRuntime(31413): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1996)
05-27 08:37:41.845: E/AndroidRuntime(31413): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2023)
05-27 08:37:41.845: E/AndroidRuntime(31413): at android.app.ActivityThread.access$600(ActivityThread.java:127)
05-27 08:37:41.845: E/AndroidRuntime(31413): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1174)
05-27 08:37:41.845: E/AndroidRuntime(31413): at android.os.Handler.dispatchMessage(Handler.java:99)
05-27 08:37:41.845: E/AndroidRuntime(31413): at android.os.Looper.loop(Looper.java:137)
05-27 08:37:41.845: E/AndroidRuntime(31413): at android.app.ActivityThread.main(ActivityThread.java:4503)
05-27 08:37:41.845: E/AndroidRuntime(31413): at java.lang.reflect.Method.invokeNative(Native Method)
05-27 08:37:41.845: E/AndroidRuntime(31413): at java.lang.reflect.Method.invoke(Method.java:511)
05-27 08:37:41.845: E/AndroidRuntime(31413): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
05-27 08:37:41.845: E/AndroidRuntime(31413): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
05-27 08:37:41.845: E/AndroidRuntime(31413): at dalvik.system.NativeStart.main(Native Method)
05-27 08:37:41.845: E/AndroidRuntime(31413): Caused by: java.lang.NullPointerException
05-27 08:37:41.845: E/AndroidRuntime(31413): at com.example.abnwiki.viewCategoryPage.onCreate(viewCategoryPage.java:38)
05-27 08:37:41.845: E/AndroidRuntime(31413): at android.app.Activity.performCreate(Activity.java:4479)
05-27 08:37:41.845: E/AndroidRuntime(31413): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050)
05-27 08:37:41.845: E/AndroidRuntime(31413): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1960)
05-27 08:37:41.845: E/AndroidRuntime(31413): ... 11 more