2011-07-09 71 views
0

查找按鈕時,這是我的onCreate()空指針異常的對話框

final Dialog dialog = new Dialog(this); 
      dialog.setContentView(R.layout.go_pro); 
      Button free = (Button) dialog.findViewById(R.id.still_free); 
      free.setOnClickListener(new OnClickListener() { //EXCEPTION HERE! 
      @Override 
       public void onClick(View v) { 
        dialog.cancel(); 
       } 
      }); 

這是INT佈局go_pro:

<LinearLayout android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_width="wrap_content"> 
    <Button android:id="@+id/still_free" android:text="Keep the ads" android:layout_width="wrap_content" android:layout_height="wrap_content" ></Button> 
    <Button android:id="@+id/full_version" android:text="Go PRO" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> 
</LinearLayout> 

的logcat:

07-09 16:23:40.102: ERROR/AndroidRuntime(11435): FATAL EXCEPTION: main 
07-09 16:23:40.102: ERROR/AndroidRuntime(11435): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.convert/com.convert.MainWindowYuval}: java.lang.NullPointerException 
07-09 16:23:40.102: ERROR/AndroidRuntime(11435):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 
07-09 16:23:40.102: ERROR/AndroidRuntime(11435):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 
07-09 16:23:40.102: ERROR/AndroidRuntime(11435):  at android.app.ActivityThread.access$2300(ActivityThread.java:125) 
07-09 16:23:40.102: ERROR/AndroidRuntime(11435):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 
07-09 16:23:40.102: ERROR/AndroidRuntime(11435):  at android.os.Handler.dispatchMessage(Handler.java:99) 
07-09 16:23:40.102: ERROR/AndroidRuntime(11435):  at android.os.Looper.loop(Looper.java:123) 
07-09 16:23:40.102: ERROR/AndroidRuntime(11435):  at android.app.ActivityThread.main(ActivityThread.java:4627) 
07-09 16:23:40.102: ERROR/AndroidRuntime(11435):  at java.lang.reflect.Method.invokeNative(Native Method) 
07-09 16:23:40.102: ERROR/AndroidRuntime(11435):  at java.lang.reflect.Method.invoke(Method.java:521) 
07-09 16:23:40.102: ERROR/AndroidRuntime(11435):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858) 
07-09 16:23:40.102: ERROR/AndroidRuntime(11435):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
07-09 16:23:40.102: ERROR/AndroidRuntime(11435):  at dalvik.system.NativeStart.main(Native Method) 
07-09 16:23:40.102: ERROR/AndroidRuntime(11435): Caused by: java.lang.NullPointerException 
**07-09 16:23:40.102: ERROR/AndroidRuntime(11435):  at com.convert.MainWindowYuval.onCreate(MainWindowYuval.java:93)**MY [email protected]#[email protected]#[email protected]#[email protected]#[email protected]# 
07-09 16:23:40.102: ERROR/AndroidRuntime(11435):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
07-09 16:23:40.102: ERROR/AndroidRuntime(11435):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 

編輯:這太奇怪了!我有不同的ID在我的XML兩個相同的按鈕,並在上兩個按鈕創建所有相同,但其中只有一個使一個空指針異常

Button button1 = (Button) dialog.findViewById(R.id.full_version); 
      button1.setOnClickListener(new OnClickListener() { 
      @Override 
       public void onClick(View v) { 
       Intent intent = new Intent(Intent.ACTION_VIEW); 
       intent.setData(Uri.parse("market://details?id=com.paulmaidment.games.flagsoftheworld")); 
       startActivity(intent); 
        dialog.cancel();//TODO OPEN PAID APP HERE!!! 
       } 
      }); 

      Button free = (Button)findViewById(R.id.stay_free); 
      free.setOnClickListener(new OnClickListener() { //EXCEPTION ONLY HERE! 
       @Override 
       public void onClick(View v) { 
        dialog.cancel(); 
       } 
      }); 
+0

是否檢查'dialog.findViewById(R.id.still_free);'將返回somtheing比空不同? – Augusto

+0

確保您清理並重建您的項目。它會重新索引你的ID。 –

回答

1

您在對話框調用findViewById時,它應該被稱爲上活動。更改

 Button free = (Button) dialog.findViewById(R.id.still_free); 

 Button free = (Button) findViewById(R.id.still_free); 

應該修復它。

+0

不是。他在對話框中調用findViewById,因爲他正在對話框中搜索按鈕。所以這段代碼是正確的。 (如果我知道他想要做正確的) – matsjoe

+0

當我們自定義任何dailog,所以我們根據需要使XML,我認爲他是使用XML,這個XML部分有一個按鈕, –

+0

所以先註冊這些按鈕,他正在使用這些代碼, –

0

嘗試移動你的代碼

dialog.setContentView(R.layout.go_pro); 
     Button free = (Button) dialog.findViewById(R.id.still_free); 
     free.setOnClickListener(new OnClickListener() { //EXCEPTION HERE! 
     @Override 
      public void onClick(View v) { 
       dialog.cancel(); 
      } 
     }); 

到對話框類的onCreate方法

0

在XML文件中的ID是still_free並在你的代碼中訪問stay_free ......這就是它

編輯: 嗯,在你的編輯你有stay_free,但在原來的職位,它是still_free,所以我假設你只是改變了ID來測試,如果這造成了?

+0

是的確切.... – Yuval