2011-09-06 71 views
0

我有打電話從次活動的對話與此代碼的第一個活動:安卓:對話和NullPointerException異常

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); 
      } 

第二項活動,popup_segnala有這樣的代碼:

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 correttamente. 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; 
} 
} 

爲什麼? ? :-( 我有這樣的錯誤日誌:

09-06 19:44:35.507:ERROR/AndroidRuntime(1657):09-06 19:44:35.507: ERROR/AndroidRuntime(1657):致命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:錯誤/ AndroidRuntime(1657):在 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:錯誤/AndroidRuntime(1657):在 android.app.Activity.showDialog 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:錯誤/ AndroidRuntime(1657):在 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( (3857)09-06 19:44:35.507:ERROR/AndroidRuntime(1657):at java.lang.reflect.Method.invokeNative(Native Method)09-06 19:44:35.507:ERR OR/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):在 dalvik.system.NativeStart.main(本機方法)

+0

有人幫我嗎? – Mimmo

+0

您應該閱讀開發者網站上發佈的教程。很明顯,你錯過了android框架的基本原理。你不應該在擴展'Activity'的類上調用'new'。 – Falmarri

回答

0

你爲什麼要創建一個擴展活動類popup_segnala?你必須在你的主要活動中移動onCreateDialog(int id)代碼,你只需調用showDialog(OK_INSERT)。

此外onCreateDialog必須返回一個對話框對象。

相關問題