我想在运行时更改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)
答案 0 :(得分: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
为空。
因为你没有从对话框
的inflatted视图(即你的customView)中获取它即
试
TextView mjollnir_descr = (TextView)yourview. findViewById(R.id.dialog_item_descr);
这里
yourview
是您的TextView所在的对话框的inflatted视图