简体   繁体   English

Android自定义对话框类按钮NPE

[英]Android Custom Dialog class button NPE

I am trying to create a custom dialog class with a custom layout, but, for reasons unknown to be, I am getting a null pointer exception when trying to access the buttons for the layout. 我正在尝试创建具有自定义布局的自定义对话框类,但是由于未知的原因,在尝试访问布局按钮时出现了空指针异常。 Here is the XML for the 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>

Here is the code for the class: 这是该类的代码:

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();
}

}

I instantiate the dialog with this code: 我用以下代码实例化对话框:

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();

The stack trace indicates the NPE occurs on this line: 堆栈跟踪表明NPE发生在此行上:

enable.setOnClickListener(new OnClickListener() {

My question for the experts is... why is this code causing a NPE, and what is the best way to fix it? 我对专家的问题是...为什么此代码会导致NPE,什么是修复它的最佳方法? Thank you for any input you have. 感谢您的任何投入。

According to documentation of findViewById() method: 根据findViewById()方法的文档

Finds a child view with the given identifier. 查找具有给定标识符的子视图。 Returns null if the specified child view does not exist or the dialog has not yet been fully created (for example, via show() or create()). 如果指定的子视图不存在或尚未完全创建对话框(例如,通过show()或create()),则返回null。

In your code you call findViewById() before show() - that's why Button enable is null. 在您的代码中,在show()之前调用findViewById()-这就是Button enable为null的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM