将onclicklisternet设置为对话框上的按钮

时间:2019-01-10 10:38:00

标签: android android-studio

因此,我在主要活动的按钮单击上使用了一个对话框,但我需要做的是在该对话框按钮中有2个按钮,我想在两个按钮上都使用setOnClickListner,这样我会收到此错误。

    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_detail);
            getnotes = (EditText) findViewById(R.id.notestext);
            submitbtn = (ImageView) findViewById(R.id.submitbutn);
            submitbtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                openDialog();
            }
        });


    public void openDialog() {
            Button signbtn;
            signbtn = (Button)findViewById(R.id.dialog_ok);
            final Dialog dialog = new Dialog(this); // Context, this, etc.
            dialog.setContentView(R.layout.dialog_demo);
            dialog.setTitle(R.string.dialog_title);
            dialog.show();

        signbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getApplicationContext(), "Notes Have been Signed by the doctor Successfully", Toast.LENGTH_SHORT).show();
            }
        });


XML file is below

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/dialog_info1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginTop="0dp"
        android:padding="10dp"
        android:text="Enter Password to Confirm"
        android:gravity="center"/>

    <EditText
        android:id="@+id/dialog_info"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/dialog_info1"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:padding="10dp"
        android:hint="Enter Password Here"
        android:gravity="center"
        android:inputType="textPassword"
        android:textSize="12sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_below="@id/dialog_info">

        <Button
            android:id="@+id/dialog_cancel"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.50"
            android:background="#e3144e"
            android:text="Cancel"
            android:textColor="#ffffff"/>

        <Button
            android:id="@+id/dialog_ok"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.50"
            android:background="#147fe3"
            android:text="Sign"
            android:textColor="#ffffff"/>
    </LinearLayout>
</RelativeLayout>

我得到的错误是

  

java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'void android.widget.Button.setOnClickListener(android.view.View $ OnClickListener)'           在nl.psdcompany.duonavigationdrawer.example.DetailActivity.openDialog(DetailActivity.java:80)   第80行是openDialog函数中具有setonclicklistner的行

0 个答案:

没有答案