0

我的问题是:我是 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)
4

2 回答 2

1

原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法 'void android.widget.GridView.setAdapter(android.widget.ListAdapter)'

您发布的 logcat 摘录与 null Bundle 无关,而是表明R.id.gridviewR.layout.my_requests

检查您的 xml 文件是否有错误、拼写错误、大小写差异,并确保您使用的是您想要使用的文件。然后清理您的项目(某些工具版本不擅长获取 xml 更改)并重新部署。

于 2015-02-24T13:35:46.120 回答
0

删除最后一行-

super.onSaveInstanceState(savedInstanceState);

它应该从 onSavedInstanceState() 调用。

于 2015-02-24T11:17:25.280 回答