亲爱的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]&#39;,并将其置于当前的顶部。我怀疑这里给出了NullPointerException,因此我将if语句放在行周围,但没有效果。由于过去的活动显示内容,因此mCatIds [position]&#39;可能有价值。问题可能在其他地方吗?还是我错过了重要的事情?
我期待着你的回复。
编辑:这是堆栈跟踪,抱歉没有直接发布:
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
答案 0 :(得分:1)
是的,请告诉我们您的异常堆栈跟踪!
如果没有看到你的堆栈跟踪,我认为这就是你的问题:
int position = i.getExtras().getInt("id");
i.getExtras()返回null !!!
答案 1 :(得分:0)
您的活动第一次开始时,它不会被意图调用。因此getIntent()
将返回NullPointerException
。你可以用
if(getIntent()!=null)
Intent i=getIntent();
或者您可以使用函数onNewIntent()
并将当前活动设置为singleTop
。
onNewIntent()
执行任务。如果您包含singleTop
,它还会确保只有一个活动实例。