onCreate(savedInstanceState) savedInstanceState 为 NULL
onCreate(savedInstanceState) savedInstanceState is NULL
我的问题是:我是 android 编程的新手,我创建了一个新应用程序,当我按下按钮启动下面的 activity 时,它崩溃了。当我调试它时,它说 onCreate() 得到了一个空包。我该如何解决?我不明白 activity 的其他 类 所以请使用详细信息,提前致谢!
这是我的代码:
@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.access0(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.access0(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)
删除最后一行-
super.onSaveInstanceState(savedInstanceState);
它应该从 onSavedInstanceState() 调用。
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.GridView.setAdapter(android.widget.ListAdapter)' on a null object reference
您发布的 logcat 摘录与空包无关,而是表明 R.id.gridview
未在 R.layout.my_requests
中找到
检查您的 xml 文件,看是否有错误、拼写错误、大小写差异,并确保您使用的是您想要的文件。然后清理您的项目(某些工具版本不擅长获取 xml 更改)并重新部署。
我的问题是:我是 android 编程的新手,我创建了一个新应用程序,当我按下按钮启动下面的 activity 时,它崩溃了。当我调试它时,它说 onCreate() 得到了一个空包。我该如何解决?我不明白 activity 的其他 类 所以请使用详细信息,提前致谢!
这是我的代码:
@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.access0(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.access0(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)
删除最后一行-
super.onSaveInstanceState(savedInstanceState);
它应该从 onSavedInstanceState() 调用。
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.GridView.setAdapter(android.widget.ListAdapter)' on a null object reference
您发布的 logcat 摘录与空包无关,而是表明 R.id.gridview
未在 R.layout.my_requests
检查您的 xml 文件,看是否有错误、拼写错误、大小写差异,并确保您使用的是您想要的文件。然后清理您的项目(某些工具版本不擅长获取 xml 更改)并重新部署。