onCreate(savedInstanceState)savedInstanceState为NULL

时间:2015-02-24 11:11:58

标签: android oncreate bundles

我的问题是:我是Android编程的新手,我创建了一个新的应用程序,当我按下按钮在下面启动此活动时,它崩溃了。当我调试它时,它说onCreate()有一个空包。我该如何解决?我不了解活动的其他类别所以请使用详细信息,提前感谢!

这是我的代码:

@Override
public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_requests);


        GridView gridview = (GridView) findViewById(R.id.gridview);
        gridview.setAdapter(new ImageAdapter(this));


    gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {


        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            AlertDialog.Builder builder = new AlertDialog.Builder(my_requests.this);
            switch (position){
                case 0: builder.setMessage("You Chose Television, Wait");
                    break;
                case 1: builder.setMessage("You Chose Cookie, Wait");
                    break;
                case 2: builder.setMessage("You Chose Toilet, Wait");
                    break;
                case 3: builder.setMessage("You Chose Toys, Wait");
                    break;
                case 4: builder.setMessage("You Chose Lunch, Wait");
                    break;
                case 5:builder.setMessage("You Chose Water, Wait");
                    break;
            }
            builder.setNeutralButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // User cancelled the dialog
                }
            });
            AlertDialog dialog = builder.create();
            dialog.show();

        }
    });
    super.onSaveInstanceState(savedInstanceState);
}

编辑::以下添加的LOGCAT

02-24 14:53:00.642  17789-17789/com.tiktaktikapps.Gimmie_ D/AndroidRuntime﹕ Shutting down VM
    --------- beginning of crash
02-24 14:53:00.647  17789-17789/com.tiktaktikapps.Gimmie_ E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.tiktaktikapps.Gimmie_, PID: 17789
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tiktaktikapps.Gimmie_/com.tiktaktikapps.Gimmie_.my_requests}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.GridView.setAdapter(android.widget.ListAdapter)' on a null object reference
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2367)
            at android.app.ActivityThread.access$800(ActivityThread.java:148)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5274)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.GridView.setAdapter(android.widget.ListAdapter)' on a null object reference
            at com.tiktaktikapps.Gimmie_.my_requests.onCreate(my_requests.java:49)
            at android.app.Activity.performCreate(Activity.java:5977)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2258)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2367)
            at android.app.ActivityThread.access$800(ActivityThread.java:148)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5274)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704)

2 个答案:

答案 0 :(得分:1)

  

引起:java.lang.NullPointerException:尝试调用虚方法&#39; void android.widget.GridView.setAdapter(android.widget.ListAdapter)&#39;在空对象引用上

您发布的logcat摘录与null Bundle无关,而是表示R.id.gridview

中未找到R.layout.my_requests

检查您的xml文件,查看错误,拼写错误,大写差异,并确保使用您想要的那些。然后清理你的项目(一些工具版本不擅长选择xml更改)并重新部署。

答案 1 :(得分:0)

删除最后一行 -

super.onSaveInstanceState(savedInstanceState);

应该从onSaveInstanceState()调用它。