2012-02-28 186 views
0

在我的自定義「警報對話框」我有一個兩難的問題,我已經在xml上聲明的按鈕上設置onclick。自定義AlertDialog.Builder添加自定義按鈕事件點擊

此代碼給了我已經證實,有一個實際的alertDialog在此之後被實施

close.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       alertDialog.dismiss(); 
      } 
     }); 

LayoutInflates被點擊的ListView的項目後一個空指針異常。

我的問題是「你如何設置自定義xml onClickListeners自定義AlertDialog上的按鈕?」

LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE); 
    View layout = inflater.inflate(R.layout.custom_dialog, (ViewGroup) findViewById(R.id.layout_root)); 

    TextView text = (TextView) layout.findViewById(R.id.data1); 
    ImageView image = (ImageView) layout.findViewById(R.id.dialog_image); 
    image.setImageResource(R.drawable.temp_book); 
    Button summary, confirm, close; 
    summary = (Button)findViewById(R.id.Summary); 
    confirm = (Button)findViewById(R.id.confirmCD); 
    close = (Button)findViewById(R.id.closeCD); 
    builder = new AlertDialog.Builder(this); 
    builder.setView(layout); 

    close.setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      alertDialog.dismiss(); 
     } 
    }); 

    alertDialog = builder.create(); 

    alertDialog.show(); 

日誌錯誤

02-28 15:13:28.385: E/AndroidRuntime(6763): FATAL EXCEPTION: main 

02-28 15:13:28.385: E/AndroidRuntime(6763): java.lang.NullPointerException 

02-28 15:13:28.385: E/AndroidRuntime(6763):  at test.list.A_Test4Activity.onListItemClick(A_Test4Activity.java:532) 

02-28 15:13:28.385: E/AndroidRuntime(6763):  at android.app.ListActivity$2.onItemClick(ListActivity.java:319) 

02-28 15:13:28.385: E/AndroidRuntime(6763):  at android.widget.AdapterView.performItemClick(AdapterView.java:292) 

02-28 15:13:28.385: E/AndroidRuntime(6763):  at android.widget.AbsListView.performItemClick(AbsListView.java:1058) 

02-28 15:13:28.385: E/AndroidRuntime(6763):  at android.widget.AbsListView$PerformClick.run(AbsListView.java:2514) 

02-28 15:13:28.385: E/AndroidRuntime(6763):  at android.widget.AbsListView$PerformClick.run(AbsListView.java:2514) 

02-28 15:13:28.385: E/AndroidRuntime(6763):  at android.widget.AbsListView$1.run(AbsListView.java:3168) 

02-28 15:13:28.385: E/AndroidRuntime(6763):  at android.os.Handler.handleCallback(Handler.java:605) 

02-28 15:13:28.385: E/AndroidRuntime(6763):  at android.os.Handler.dispatchMessage(Handler.java:92) 

02-28 15:13:28.385: E/AndroidRuntime(6763):  at android.os.Looper.loop(Looper.java:137) 

02-28 15:13:28.385: E/AndroidRuntime(6763):  at android.app.ActivityThread.main(ActivityThread.java:4340) 

02-28 15:13:28.385: E/AndroidRuntime(6763):  at java.lang.reflect.Method.invokeNative(Native Method) 

02-28 15:13:28.385: E/AndroidRuntime(6763):  at java.lang.reflect.Method.invoke(Method.java:511) 

02-28 15:13:28.385: E/AndroidRuntime(6763):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 

02-28 15:13:28.385: E/AndroidRuntime(6763):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 

02-28 15:13:28.385: E/AndroidRuntime(6763):  at dalvik.system.NativeStart.main(Native Method) 
+0

您膨脹的'Dialog'自定義佈局中的close按鈕? – Luksprog 2012-02-28 20:32:01

+0

AlertDialog.Builder builder = null; &AlertDialog alertDialog = null; 全球宣佈。 – wesdfgfgd 2012-02-28 20:32:20

+0

它膨脹在一個選定項目的列表視圖點擊。 – wesdfgfgd 2012-02-28 20:33:05

回答

3

沒有看到任何蹤跡與例外,我會說,你不看,你應該尋找Button密切。如果您將AlertDialog的佈局設置爲您膨脹的自定義佈局或構建的代碼View,那麼您必須對執行findViewById(R.id.Id_of_button),您將其設置爲AlertDialog內容。

編輯:

接近Buttonnull(和投擲NullPointerException調用它的方法時),因爲你必須尋找在佈局中Button以前膨脹並用作AlertDialog內容:

close = (Button) layout.findViewById(R.id.closeCD); 

不要忘了總結確認Buttons如果他們也在膨脹的佈局。

+0

我已經這麼做了。爲了避免混淆,我將發佈我當前使用的代碼以及作爲日誌錯誤。 – wesdfgfgd 2012-02-28 20:25:07