简体   繁体   中英

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. When I debugged it it said onCreate() got a null bundle. 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

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

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

Check your xml files, for errors, typos, capitalization differences, and make sure that you are using the ones you meant to. Then clean your project (some tool versions aren't good at picking up xml changes) and re-deploy.

remove last line-

super.onSaveInstanceState(savedInstanceState);

it should be called from onSavedInstanceState().

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM