Why is my custom alert dialog code crashing?

吃可爱长大的小学妹 提交于 2019-12-25 07:47:52

问题


I'm trying to apply custom view to alert dialog. I'm doing like this, but there is nullpointer exception when I try to show the alert box.

I create a layout.xml like so:

<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>

and applying to alertbox through this code.

 LayoutInflater factory = LayoutInflater.from(BackupRestoreActivityContext);  
  final View textEntryView = factory.inflate(R.layout.layout,null); 
    AlertDialog.Builder alert = new AlertDialog.Builder(BackupRestoreActivityContext);          
           alert.setTitle("Configuration Setting"); 
          alert.setView(textEntryView);   
      alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() 
          {     
              public void onClick(DialogInterface dialog, int whichButton) {  

                    return;          
                  }      
              });      
          alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() 
          {     
              public void onClick(DialogInterface dialog, int which) {   
                  // TODO Auto-generated method stub       
                  return;      
                  }  
              });     
          alert.show();     

What could be causing the null pointer exception?


回答1:


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

}

may be help full for you after a bit changing.




回答2:


Try with these code for Alert Dialog its working for me

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


来源:https://stackoverflow.com/questions/5297642/why-is-my-custom-alert-dialog-code-crashing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!