在运行时修改AlertDialog的内容

如题,我试过一些方法没实现。

使用的是自定义布局,我用的下面方法:

TextView mjollnir_descr = (TextView) findViewById(R.id.dialog_item_descr);
    mjollnir_descr.setText(R.string.item_detail_clarity_descr);

在代码第二行报出的错误:

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)

ChatGPT尝试为您解答,仅供参考
看起来你在尝试使用自定义布局中的 TextView 控件来更新对话框的内容。


但是,在你尝试使用 findViewById 方法来获取 TextView 控件时,它返回了 null,导致了空指针异常。这可能是因为你在调用 findViewById 方法之前没有设置自定义布局。

要解决此问题,首先确保你已经设置了自定义布局,例如:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
View view = getLayoutInflater().inflate(R.layout.custom_layout, null);
builder.setView(view);

然后,使用 view.findViewById 方法来获取 TextView 控件:

TextView mjollnir_descr = (TextView) view.findViewById(R.id.dialog_item_descr);
mjollnir_descr.setText(R.string.item_detail_clarity_descr);

这应该可以解决问题。