Я пытаюсь создать собственный класс диалога с настраиваемым макетом, но по неизвестным причинам я получаю исключение нулевого указателя при попытке получить доступ к кнопкам макета. Вот XML для диалога:Кнопка класса пользовательского диалогового окна Android NPE
<?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, и как лучше всего это исправить? Спасибо за любой вклад, который у вас есть.
убедитесь, что разрешение не равно null, прежде чем пытаться добавить к нему слушателя – Stultuske