-1

亲爱的 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
4

2 回答 2

1

是的,给我们您的异常堆栈跟踪!

没有看到你的堆栈跟踪,我认为那是你的问题:

int position = i.getExtras().getInt("id");

i.getExtras() 返回 null !!!

于 2013-05-26T21:43:03.027 回答
0

您的活动第一次开始时,它不是由意图调用的。因此getIntent()将返回一个NullPointerException. 你可以用任何一个来克服这个问题

if(getIntent()!=null)
Intent i=getIntent();

或者您可以使用该功能onNewIntent()并将当前活动设置为singleTop.

onNewIntent()每次意图调用活动时执行任务。它还确保只有一个活动实例,如果您包含singleTop.

于 2013-05-27T06:58:44.427 回答