Android : dialog and NullPointerException
I have the first activity that call a dialog from second activity with this code :
public class negozi extends ListActivity implements dati_punti{
private Context context;
private popup_segnala segnala;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.negozi);
context = this;
visual_pop(context);
}
public void visual_pop(Context context){
segnala = new popup_segnala(context);
segnala.showDialog(popup_segnala.OK_INSERT);
}
the second activity, popup_segnala have this code :
public class popup_segnala extends Activity {
public static final int OK_INSERT = 1;
private static Context context;
public popup_segnala(Context context) {
this.context = context;
}
@Override
protected Dialog onCreateDialog(int id) {
switch(id){
case OK_INSERT :
Builder build_insert = createDialog(context,"Dati registrati correttame开发者_StackOverflownte. Grazie","Alert");
build_insert.setPositiveButton("Ok", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
return build_insert.show();
}
return null;
}
}
Why ??? :-( I have this error log:
09-06 19:44:35.507: ERROR/AndroidRuntime(1657): 09-06 19:44:35.507: ERROR/AndroidRuntime(1657): FATAL EXCEPTION: main 09-06 19:44:35.507: ERROR/AndroidRuntime(1657): java.lang.NullPointerException 09-06 19:44:35.507: ERROR/AndroidRuntime(1657): at android.app.Activity.getVolumeControlStream(Activity.java:3703) 09-06 19:44:35.507: ERROR/AndroidRuntime(1657): at android.app.Dialog.setOwnerActivity(Dialog.java:188) 09-06 19:44:35.507: ERROR/AndroidRuntime(1657): at android.app.Activity.onPrepareDialog(Activity.java:2491) 09-06 19:44:35.507: ERROR/AndroidRuntime(1657): at android.app.Activity.onPrepareDialog(Activity.java:2515) 09-06 19:44:35.507: ERROR/AndroidRuntime(1657): at android.app.Activity.showDialog(Activity.java:2565) 09-06 19:44:35.507: ERROR/AndroidRuntime(1657): at android.app.Activity.showDialog(Activity.java:2524) 09-06 19:44:35.507: ERROR/AndroidRuntime(1657): at it.android.infoconsumatori.negozi.onOptionsItemSelected(negozi.java:65) 09-06 19:44:35.507: ERROR/AndroidRuntime(1657): at android.app.Activity.onMenuItemSelected(Activity.java:2205) 09-06 19:44:35.507: ERROR/AndroidRuntime(1657): at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:774) 09-06 19:44:35.507: ERROR/AndroidRuntime(1657): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:143) 09-06 19:44:35.507: ERROR/AndroidRuntime(1657): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:855) 09-06 19:44:35.507: ERROR/AndroidRuntime(1657): at com.android.internal.view.menu.IconMenuView.invokeItem(IconMenuView.java:532) 09-06 19:44:35.507: ERROR/AndroidRuntime(1657): at com.android.internal.view.menu.IconMenuItemView.performClick(IconMenuItemView.java:122) 09-06 19:44:35.507: ERROR/AndroidRuntime(1657): at android.view.View$PerformClick.run(View.java:9152) 09-06 19:44:35.507: ERROR/AndroidRuntime(1657): at android.os.Handler.handleCallback(Handler.java:587) 09-06 19:44:35.507: ERROR/AndroidRuntime(1657): at android.os.Handler.dispatchMessage(Handler.java:92) 09-06 19:44:35.507: ERROR/AndroidRuntime(1657): at android.os.Looper.loop(Looper.java:123) 09-06 19:44:35.507: ERROR/AndroidRuntime(1657): at android.app.ActivityThread.main(ActivityThread.java:3687) 09-06 19:44:35.507: ERROR/AndroidRuntime(1657): at java.lang.reflect.Method.invokeNative(Native Method) 09-06 19:44:35.507: ERROR/AndroidRuntime(1657): at java.lang.reflect.Method.invoke(Method.java:507) 09-06 19:44:35.507: ERROR/AndroidRuntime(1657): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842) 09-06 19:44:35.507: ERROR/AndroidRuntime(1657): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 09-06 19:44:35.507: ERROR/AndroidRuntime(1657): at dalvik.system.NativeStart.main(Native Method)
Why do you create the class popup_segnala that extends Activity? You have to move onCreateDialog(int id) code in your main activity where you will simply call showDialog(OK_INSERT).
Moreover onCreateDialog must return a dialog object.
精彩评论