2012-08-11 156 views
0

我測試人在回答我的問題代碼:對話框顯示問題

remove line between custom option menu items

回答與此相關的PIC:

enter image description here

我想火對話框時新聞反饋通過將此代碼添加到WidgetMenu類,但它給了我強制關閉,我添加的代碼:

public void onClick(View arg0) {  
     final Dialog dialog = new Dialog(context); 
     dialog.setContentView(R.layout.custom_dialog); 
     dialog.setTitle("Title..."); 

     // set the custom dialog components - text, image and button 
     TextView text = (TextView) dialog.findViewById 
          (R.id.dialog_text); 
     text.setText("Places contact me"); 

     ImageView image = (ImageView) dialog.findViewById(R.id.image); 
     image.setImageResource(R.drawable.ic_launcher); 

    Button dialogButton = (Button) dialog.findViewById(R.id.dialog_Button); 
     // if button is clicked, close the custom dialog 
     dialogButton.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       dialog.dismiss(); 
      } 
     }); 

     dialog.show(); 
     } 
    }); 

我的logcat:

W/KeyCharacterMap(303): No keyboard for id 0 
    W/KeyCharacterMap(303): Using default keymap: /system/usr/keychars/qwerty.kcm.bin 
    D/dalvikvm(303): GC_EXTERNAL_ALLOC freed 932 objects/65328 bytes in 90ms 
    D/AndroidRuntime(303): Shutting down VM 
    W/dalvikvm(303): threadid=1: thread exiting with uncaught exception (group=0x4001d800) 
    E/AndroidRuntime(303): FATAL EXCEPTION: main 
    E/AndroidRuntime(303): java.lang.NullPointerException 
    E/AndroidRuntime(303): at android.app.Dialog.<init> (Dialog.java:141) 
    E/AndroidRuntime(303): at android.app.Dialog.<init> (Dialog.java:123) 
    E/AndroidRuntime(303): at com.tsn.dr.WidgetMenu$Ui$2.onClick (WidgetMenu.java:99) 
    E/AndroidRuntime(303): at android.view.View.performClick (View.java:2408) 
    E/AndroidRuntime(303): at android.view.View$PerformClick.run (View.java:8816) 
    E/AndroidRuntime(303): at android.os.Handler.handleCallback (Handler.java:587) 
    E/AndroidRuntime(303): at android.os.Handler.dispatchMessage (Handler.java:92) 
    E/AndroidRuntime(303): at android.os.Looper.loop(Looper.java:123) 
    E/AndroidRuntime(303): at android.app.ActivityThread.main (ActivityThread.java:4627) 
    E/AndroidRuntime(303): at java.lang.reflect.Method.invokeNative (Native Method) 
    E/AndroidRuntime(303): at java.lang.reflect.Method.invoke (Method.java:521) 
    E/AndroidRuntime(303): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
    E/AndroidRuntime(303): at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:626) 
    E/AndroidRuntime(303): at dalvik.system.NativeStart.main(Native Method) 

任何意見可以理解,謝謝。

回答

1

可以試試這個:

final Dialog dialog = new Dialog(YourActivity.this); 
+0

後者是你應該在這裏使用的。 – FoamyGuy 2012-08-11 02:58:50

0

Dialog構造函數失敗,出現NullPointerException。我能想到的唯一原因是你傳入的context參數確實是null

你怎麼初始化context

+0

我改變這樣的:最終的對話的對話=新的對話框(的getContext()); – 2012-08-11 01:32:28

+0

但強制再次關閉 – 2012-08-11 01:33:26

+0

嘗試簡單地通過「this」。如果仍然失敗,請檢查堆棧跟蹤是否仍然相同或其他現在失敗? – digitalbreed 2012-08-11 01:34:47