Android Custom Dialog类按钮NPE

时间:2016-02-21 17:26:22

标签: java android dialog

我正在尝试使用自定义布局创建自定义对话框类,但是,由于未知原因,我在尝试访问布局按钮时遇到空指针异常。以下是对话框的XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/enableTopRelativeLayout"
android:layout_width="460dp"
android:layout_height="wrap_content"
android:background="@drawable/dialog_background"
android:paddingBottom="15dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingTop="15dp" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:src="@drawable/ic_launcher" />

<RelativeLayout
    android:id="@+id/enableInnerRelativeLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/imageView1" >

    <TextView
        android:id="@+id/enableMessage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerInParent="false"
        android:paddingBottom="10dp"
        android:text="Start service?"
        android:textAppearance="?android:attr/textAppearanceMedium" />

     <TextView
        android:id="@+id/enableStrut"
        android:layout_width="25dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/enableMessage"
        android:layout_centerHorizontal="true"
        android:text=" " />

    <Button
        android:id="@+id/noenable"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/enableMessage"
        android:layout_toLeftOf="@id/enableStrut"
        android:background="@drawable/button_background"
        android:text="No"
        android:textColorLink="@color/color2"
        android:textStyle="bold" />

    <Button
        android:id="@+id/enable"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/enableMessage"
        android:layout_toRightOf="@id/enableStrut"
        android:background="@drawable/button_background"
        android:text="Yes"
        android:textColorLink="@color/color2"
        android:textStyle="bold" />

</RelativeLayout>

以下是该类的代码:

public class DialogStartService extends Dialog implements android.view.View.OnClickListener{
public Button yes;
public Button no;
public TextView tv;

public DialogStartService(Context context) {
    super(context);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.start_service_layout);

    getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

    tv = (TextView) findViewById(R.id.enableMessage);

    yes = (Button) findViewById(R.id.enable);
    no = (Button) findViewById(R.id.noenable);

    yes.setOnClickListener(this);
    no.setOnClickListener(this);

}

@Override
public void onClick(View v) {       

    dismiss();
}

}

我使用以下代码实例化对话框:

DialogStartService enableDialog = new DialogStartService(this);

    Button enable = (Button) enableDialog.findViewById(R.id.enable);
    enable.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // Do stuff

        }

    });

    Button noenable = (Button) enableDialog.findViewById(R.id.noenable);
    noenable.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // Do stuff
        }

    });


    enableDialog.show();

堆栈跟踪表明此行发生了NPE:

enable.setOnClickListener(new OnClickListener() {

我对专家的问题是......为什么这段代码导致了NPE,修复它的最佳方法是什么?感谢您提供任何意见。

1 个答案:

答案 0 :(得分:2)

根据findViewById()方法的documentation

  

查找具有给定标识符的子视图。如果指定的子视图不存在或者尚未完全创建对话框(例如,通过show()或create()),则返回null。

在你的代码中,你在show()之前调用findViewById() - 这就是为什么Button enable为null。

相关问题