0

我想在运行时更改 AlertDialog 中的文本,但它还没有真正起作用。

由于我使用的是自定义布局,我认为我可以这样做,

AlertDialog.Builder dialog_item_detail = new AlertDialog.Builder(this);   

LayoutInflater inflate_dialog = getLayoutInflater();
TextView mjollnir_descr = (TextView)findViewById(R.id.dialog_item_descr);
mjollnir_descr.setText(R.string.item_detail_mjollnir_descr);        

dialog_item_detail.setIcon(R.drawable.item_clarity);
dialog_item_detail.setView(inflate_dialog.inflate(R.layout.item_dialog, null));     
dialog_item_detail.create();

dialog_item_detail.setTitle(R.string.dialog_item_detail_title_clarity);   
//dialog_item_detail.setCancelable(true);   
dialog_item_detail.setNeutralButton(android.R.string.ok, new DialogInterface.OnClickListener() {  
   public void onClick(DialogInterface dialog, int whichButton){               
   }   
   });   

dialog_item_detail.show(); 

但如上所述,它不起作用。我总是在上面代码的第二行收到这个错误:

03-31 19:45:25.261: E/AndroidRuntime(781): FATAL EXCEPTION: main
03-31 19:45:25.261: E/AndroidRuntime(781): java.lang.IllegalStateException: Could not execute method of the activity
03-31 19:45:25.261: E/AndroidRuntime(781): Caused by: java.lang.NullPointerException
03-31 19:45:25.261: E/AndroidRuntime(781):  at com.myapp.ItemsOverview.show_details_mjollnir(ItemsOverview.java:81)
4

1 回答 1

1

编辑:

AlertDialog.Builder dialog_item_detail = new AlertDialog.Builder(this);   

    LayoutInflater inflate_dialog = getLayoutInflater();
View v=inflate_dialog.inflate(R.layout.item_dialog, null);
    TextView mjollnir_descr = (TextView)v.findViewById(R.id.dialog_item_descr);
    mjollnir_descr.setText(R.string.item_detail_mjollnir_descr);        

    dialog_item_detail.setIcon(R.drawable.item_clarity);
    dialog_item_detail.setView(v);     
    dialog_item_detail.create();

    dialog_item_detail.setTitle(R.string.dialog_item_detail_title_clarity);   
    //dialog_item_detail.setCancelable(true);   
    dialog_item_detail.setNeutralButton(android.R.string.ok, new DialogInterface.OnClickListener() {  
       public void onClick(DialogInterface dialog, int whichButton){               
       }   
       });   
dialog_item_detail.show();

你会得到NullPointerException,因为你TextView是空的。

因为您没有从对话框的膨胀视图(即您的 customView)中获取它

IE

尝试

TextView mjollnir_descr = (TextView)yourview. findViewById(R.id.dialog_item_descr);

这里

yourview是 TextView 所在对话框的膨胀视图

于 2013-03-31T20:05:52.770 回答