简体   繁体   English

onCreate(savedInstanceState)savedInstanceState为NULL

[英]onCreate(savedInstanceState) savedInstanceState is NULL

My issue is this: I'm new to android programming and I have created a new app, when i press the button to launch this activity below, it crashes. 我的问题是这样的:我是android编程的新手,我创建了一个新的应用程序,当我按下下面的按钮启动此活动时,它崩溃了。 When I debugged it it said onCreate() got a null bundle. 当我调试它时,它说onCreate()得到了一个空包。 How do I fix this? 我该如何解决? I don't understand the other classes of the activity so please use detailed info, thanks in advance! 我不了解该活动的其他类别,因此请使用详细信息,谢谢!

Here is my code: 这是我的代码:

@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);
}

EDIT:: ADDED LOGCAT BELOW 编辑::在下面添加了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)

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.GridView.setAdapter(android.widget.ListAdapter)' on a null object reference 原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'void android.widget.GridView.setAdapter(android.widget.ListAdapter)'

Your posted logcat excerpt has nothing to do with a null Bundle, but rather indicates that R.id.gridview is not being found in R.layout.my_requests 您发布的logcat摘录与null捆绑包无关,而是指示在R.layout.my_requests中找不到R.id.gridview

Check your xml files, for errors, typos, capitalization differences, and make sure that you are using the ones you meant to. 检查您的xml文件,查看是否有错误,错别字,大写字母差异,并确保使用了您想要的文件。 Then clean your project (some tool versions aren't good at picking up xml changes) and re-deploy. 然后清理您的项目(某些工具版本不擅长进行xml更改)并重新部署。

remove last line- 删除最后一行

super.onSaveInstanceState(savedInstanceState);

it should be called from onSavedInstanceState(). 应该从onSavedInstanceState()调用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 savedInstanceState在onCreate中始终为null值 - savedInstanceState is always null value in onCreate 在onCreate上调用了onSaveInstanceState,但saveInstanceState为null - onSaveInstanceState called but savedInstanceState is null in onCreate onCreate(Bundle savedInstanceState)始终为null - onCreate(Bundle savedInstanceState) in always null 捆绑的saveInstanceState在onCreate()上为null,尽管saveInstanceState已经设置了数据 - Bundle savedInstanceState is null on onCreate() although savedInstanceState is already set data 调用了onSaveInstanceState,但是onCreate方法中的savedInstanceState抛出null - onSaveInstanceState is called, but savedInstanceState in onCreate method throws null Android:Activity onCreate savedInstanceState始终为null - Android: Activity onCreate savedInstanceState allways is null Android-片段onCreate()和onCreateView()中捆绑的saveInstanceState为null - Android - Bundle savedInstanceState is null in Fragment onCreate() and onCreateView() SavedInstanceState为null - SavedInstanceState is null super.onCreate(savedInstanceState); - super.onCreate(savedInstanceState); 在onCreate中检查savedInstanceState是否为null是判断设备是否已旋转的好方法? - Is checking savedInstanceState for null in onCreate a good way to tell if the device was rotated?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM