Android 为什么我的自定义警报对话框代码崩溃?
我正在尝试将自定义视图应用于警报对话框。我是这样做的,但是当我试图显示警报框时,会出现nullpointer异常 我创建一个layout.xml,如下所示:Android 为什么我的自定义警报对话框代码崩溃?,android,android-alertdialog,Android,Android Alertdialog,我正在尝试将自定义视图应用于警报对话框。我是这样做的,但是当我试图显示警报框时,会出现nullpointer异常 我创建一个layout.xml,如下所示: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/themescreen" android:gravity="center" android:orientation="horizonta
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/themescreen"
android:gravity="center"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TableLayout
android:padding="45dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioGroup
android:gravity="center">
<TableRow>
<RadioButton
android:id="@+id/rdthemeBlue"
android:button="@drawable/radio_custom">
</RadioButton>
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/bluebackgroundicon"
android:paddingLeft="10dip"
android:paddingBottom="10dip"
></ImageView>
</TableRow>
<TableRow>
<RadioButton
android:id="@+id/rdthemeGolden"
android:button="@drawable/radio_custom"></RadioButton>
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/goldenredbackgroundicon"
android:paddingLeft="10dip"
android:paddingBottom="10dip"
></ImageView>
</TableRow>
<TableRow>
<RadioButton
android:id="@+id/rdthemeBlack"
android:button="@drawable/radio_custom">
</RadioButton>
<TextView
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dip"
android:text="None"
>
</TextView>
</TableRow>
</RadioGroup>
<TableRow
android:paddingTop="25dip"
android:paddingLeft="10dip"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
>
<Button
android:id="@+id/btnthemesave"
android:background="@drawable/btn_custom"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:paddingLeft="10dip"
android:text="Save"
android:textSize="15sp"
></Button>
<Button
android:id="@+id/btnthemecancel"
android:background="@drawable/btn_custom"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:paddingLeft="10dip"
android:text="Cancel"
android:textSize="15sp"
></Button>
</LinearLayout>
</TableRow>
</TableLayout>
</LinearLayout>
什么可能导致空指针异常
public class BranchDialog extends android.app.Dialog
{
ListView list;
Context context;
TextView title;
ProgressBar progress;
public BranchDialog(Context context, String rid, String name)
{
super(context);
setContentView(R.layout.branch_dialog);
setCancelable(true);
setTitle(name);
this.context = context;
title = (TextView)findViewById(R.id.branch_dialog_title);
title.setText("this is my custom title");
show();
}
}
稍作更改后,可能对您有帮助。请尝试使用这些代码创建警报对话框,它对我有用
AlertDialog.Builder alert;
alert = new AlertDialog.Builder(Deal_Purchase_Checkout.this);
alert.setTitle("Enter Your Title");
alert.setView(input);
alert.setPositiveButton("Ok",new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int whichButton){
//Some Code
}
});
alert.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int whichButton) {
dialog.cancel();
}
});
alert.show();
AlertDialog.Builder alert;
alert = new AlertDialog.Builder(Deal_Purchase_Checkout.this);
alert.setTitle("Enter Your Title");
alert.setView(input);
alert.setPositiveButton("Ok",new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int whichButton){
//Some Code
}
});
alert.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int whichButton) {
dialog.cancel();
}
});
alert.show();