2013-02-17 108 views
0

我在對話框上的按鈕的事件偵聽器有問題。 我有一個quickAction,當我點擊菜單快速動作項目時,我想顯示一個對話框, ,我想爲buton YES和NO設置clickListener。請幫幫我! 我的代碼: 函數的onCreate(),我宣佈無法設置對話框上的按鈕監聽器?

dialog1 = new Dialog(this,R.style.FullHeightDialog);  
     btnDialogYes = (Button)dialog1.findViewById(R.id.bmessageDialogYes); 
     btnDialogNo = (Button)dialog1.findViewById(R.id.bmessageDialogNo); 
     message = (TextView)dialog1.findViewById(R.id.tvmessagedialogtext); 
     dialog1.setContentView(R.layout.custom_dialog); 
     dialog1.setCancelable(true); 

class MyClass implements OnClickListener{ 

} 

和我重寫

onClick(){ 
     switch(v.getId()){ 
      case R.id.bmessageDialogYes:{ 
       board.setCurrentPiece(Board.WHITE); 
       threadComputer = new myThread(doBackgroundThreadProcessing); 
       threadComputer.start(); 
       dialog1.dismiss(); 
       break;    
      } 
      case R.id.bmessageDialogNo:{ 
       board.setCurrentPiece(Board.BLACK); 
       threadComputer = new myThread(doBackgroundThreadProcessing); 
       threadComputer.start(); 
       dialog1.dismiss(); 
       break;    
      } 

     } 
} 

當我點擊菜單quickaction的項目,我打電話提播放()

Play(){ 
    message.setText("YES to AI first play!");  
     btnDialogYes.setOnClickListener(this); // error here 
     btnDialogNo.setOnClickListener(this); 

     dialog1.show(); 
} 

但是當我運行的應用程序無法正常工作,和我有檢查控件的ID文件佈局:(

my layout 
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/layout_root" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#ffffff" 
    android:orientation="vertical" > 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="10dp" 
     android:orientation="horizontal" > 

     <!-- This is for the info image --> 
     <ImageView 
      android:id="@+id/imageView1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="20dp" 
      android:src="@android:drawable/ic_menu_info_details" /> 

     <!-- Title --> 
     <TextView 
      android:id="@+id/tvmessagedialogtitle" 
      android:layout_width="184dp" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="10dp" 
      android:layout_marginTop="4dip" 
      android:layout_weight="0.01" 
      android:padding="8dp" 
      android:text="Message" 
      android:textColor="#343434" 
      android:textSize="16dip" 
      android:textStyle="normal" 
      android:typeface="normal" /> 
    </LinearLayout> 

    <!-- Underline with this bluish color --> 
    <View 
     android:layout_width="fill_parent" 
     android:layout_height="2dip" 
     android:background="#09adb9" /> 

    <!-- The message --> 
    <TextView 
     android:id="@+id/tvmessagedialogtext" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="10dp" 
     android:layout_marginBottom="10dp" 
     android:layout_marginTop="4dip" 
     android:gravity="left" 
     android:padding="8dp" 
     android:text="Message" 
     android:textColor="#343434" 
     android:textSize="15dip" 
     android:textStyle="normal" 
     android:typeface="normal" /> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="10dp" 
     android:orientation="horizontal" > 

     <!-- Yes button --> 
     <Button 

      android:id="@+id/bmessageDialogYes" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="4dip" 
      android:layout_marginLeft="60dp" 
      android:background="#ffffff" 
      android:padding="8dp" 
      android:text="Yes" 
      android:textColor="#343434" 
      android:textSize="13dip" 
      android:textStyle="normal" 
      android:typeface="normal" /> 

     <!-- No Button --> 

     <Button 
      android:id="@+id/bmessageDialogNo" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="4dip" 
      android:layout_marginLeft="80dp" 
      android:background="#ffffff" 
      android:padding="8dp" 
      android:text="No" 
      android:textColor="#343434" 
      android:textSize="13dip" 
      android:textStyle="normal" 
      android:typeface="normal" /> 
    </LinearLayout> 

</LinearLayout> 

logcat 
02-17 19:34:38.760: E/AndroidRuntime(374): FATAL EXCEPTION: main 
02-17 19:34:38.760: E/AndroidRuntime(374): java.lang.NullPointerException 
02-17 19:34:38.760: E/AndroidRuntime(374): at my.quoc.caro_final.ComputerActivity.Play(ComputerActivity.java:254) 
02-17 19:34:38.760: E/AndroidRuntime(374): at my.quoc.caro_final.ComputerActivity.access$4(ComputerActivity.java:219) 
02-17 19:34:38.760: E/AndroidRuntime(374): at my.quoc.caro_final.ComputerActivity$2.onItemClick(ComputerActivity.java:68) 
02-17 19:34:38.760: E/AndroidRuntime(374): at my.quoc.caro_final.QuickAction$2.onClick(QuickAction.java:169) 
02-17 19:34:38.760: E/AndroidRuntime(374): at android.view.View.performClick(View.java:2485) 
02-17 19:34:38.760: E/AndroidRuntime(374): at android.view.View$PerformClick.run(View.java:9080) 
02-17 19:34:38.760: E/AndroidRuntime(374): at android.os.Handler.handleCallback(Handler.java:587) 
02-17 19:34:38.760: E/AndroidRuntime(374): at android.os.Handler.dispatchMessage(Handler.java:92) 
02-17 19:34:38.760: E/AndroidRuntime(374): at android.os.Looper.loop(Looper.java:123) 
02-17 19:34:38.760: E/AndroidRuntime(374): at android.app.ActivityThread.main(ActivityThread.java:3683) 
02-17 19:34:38.760: E/AndroidRuntime(374): at java.lang.reflect.Method.invokeNative(Native Method) 
02-17 19:34:38.760: E/AndroidRuntime(374): at java.lang.reflect.Method.invoke(Method.java:507) 
02-17 19:34:38.760: E/AndroidRuntime(374): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
02-17 19:34:38.760: E/AndroidRuntime(374): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
02-17 19:34:38.760: E/AndroidRuntime(374): at dalvik.system.NativeStart.main(Native Method) 
02-17 19:34:40.700: I/Process(374): Sending signal. PID: 374 SIG: 9 

回答

0

它的接縫btnDialogYes沒有引用您的按鈕,因爲你叫btnDialogYes = (Button)dialog1.findViewById(R.id.bmessageDialogYes);

dialog1.setContentView(R.layout.custom_dialog);嘗試在創建Dialog實例後調用setContentView方法。 例如:

dialog1 = new Dialog(this,R.style.FullHeightDialog); 
dialog1.setContentView(R.layout.custom_dialog); 

btnDialogYes = (Button)dialog1.findViewById(R.id.bmessageDialogYes); 
btnDialogNo = (Button)dialog1.findViewById(R.id.bmessageDialogNo); 
message = (TextView)dialog1.findViewById(R.id.tvmessagedialogtext); 

dialog1.setCancelable(true); 
+0

它工作的很好!非常感謝你,我明白我的問題!再次感謝你! – 2013-02-17 13:21:07

相關問題