0

我对对话框上的按钮的事件侦听器有问题。我有一个快速操作,当我单击菜单快速操作上的项目时,我想显示一个对话框,并且我想将 clickListener 设置为按钮 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();
}

但是当我运行应用程序不起作用时,我在文件布局中检查了小部件的 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
4

1 回答 1

0

它接缝btnDialogYes不引用您的按钮,因为您dialog1.setContentView(R.layout.custom_dialog);btnDialogYes = (Button)dialog1.findViewById(R.id.bmessageDialogYes);

setContentView在 Dialog 实例创建后尝试调用方法。例如:

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);
于 2013-02-17T13:09:43.537 回答