无法在对话框中设置按钮的监听器?

时间:2013-02-17 12:58:26

标签: android

我在对话框上按钮的事件监听器有问题。 我有一个quickAction,当我单击菜单快速操作上的项目时,我想显示一个对话框, 我想设置clickListener for Buton YES和NO。请帮我! 我的代码: 在函数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;              
            }

        }
}

当我点击菜单快速操作项目时,我打电话提及Play()

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

        dialog1.show();
}

但是当我运行app不工作时,我在文件布局中检查了widget的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

1 个答案:

答案 0 :(得分:0)

由于您在btnDialogYes

之后致电dialog1.setContentView(R.layout.custom_dialog);btnDialogYes = (Button)dialog1.findViewById(R.id.bmessageDialogYes);未引用您的按钮

在创建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);