java - NullpointerException 尝试调用虚拟方法 'void android.widget.Button.setOnClickListener

标签 java android nullpointerexception

我想在我的代码中使用对话框做一些事情,但我对此有疑问。我的问题出现在这里:

btnDialogCancel.setOnClickListener(new View.OnClickListener() 

有人可以帮我吗?非常感谢

这是我的 MainActivity 类:

public class MainActivity extends AppCompatActivity {

private ProgressBar prgChronemeter;
private TextView txtShowSecond;
private Handler handler = new Handler();

private Button btnSelectMin;
private Button btnDialogSelect;
private Button btnDialogCancel;
private  EditText etMinute;
private  Dialog dialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);



    prgChronometer = (ProgressBar) findViewById(R.id.prgChronometer);
    txtShowSecond = (TextView) findViewById(R.id.txtShowSecond);

    btnSelectMin = (Button) findViewById(R.id.btnSelectMin);

    btnSelectMin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            customDialogCalistir();
        }
    });
}

private void customDialogCalistir(){
    dialog = new Dialog(this);
    dialog.setContentView(R.layout.dialog);
    btnDialogCancel = (Button) findViewById(R.id.btnDialogCancel);
    btnDialogSelect =(Button) findViewById(R.id.btnDialogSelect);
    etMinute = (EditText) findViewById(R.id.etMinute);

    btnDialogCancel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();
        }
    });
    btnDialogSelect.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            dialog.dismiss();
        }
    });
    dialog.show();
}
}

这是我的dialog.xml 文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <Button
        android:id="@+id/btnDialogSelect"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="16dp"
        android:text="Seç"
        android:layout_marginStart="42dp"
        android:layout_marginTop="155dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="10"
        android:layout_centerHorizontal="true"
        android:id="@+id/etMinute"
        android:layout_marginTop="50dp"
         />

    <Button
        android:id="@+id/btnDialogCancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"

        android:layout_marginEnd="12dp"
         />

</RelativeLayout>

最佳答案

private void customDialogCalistir(){
    dialog = new Dialog(this);
    dialog.setContentView(R.layout.dialog);
    btnDialogCancel = (Button) dialog.findViewById(R.id.btnDialogCancel);
    btnDialogSelect =(Button) dialog.findViewById(R.id.btnDialogSelect);
    etMinute = (EditText) dialog.findViewById(R.id.etMinute);

    btnDialogCancel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();
        }
    });
    btnDialogSelect.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            dialog.dismiss();
        }
    });
    dialog.show();
}

使用它可以解决您的问题。

关于java - NullpointerException 尝试调用虚拟方法 'void android.widget.Button.setOnClickListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41143437/

相关文章:

java - 一个微服务如何使用另一个微服务的安全配置

java - 我可以在 SLF4J Simple 的 props 文件中指定环境变量吗?

java - 位图而不是 View 上的波纹动画

android - 自动播放视频( ListView )

android - 我的 customarayadapter 似乎传递了错误的变量

java - 什么是NullPointerException,我该如何解决?

java线程和jtabbedpane

java - 如何动态改变线条的颜色?

java - 声明 Java 对象数组的数组时遇到问题

android - 尝试打开 Android XML 文件时,如何修复 Eclipse 中出现的 nullpointerexception?