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