我刚刚在我的 Android 应用程序中的一个 fragment 中实现了一个 AlertDialog
,它导致我的应用程序在显示时崩溃。
对于为什么会这样有什么想法吗?
对话框
void addSiteOption() {
String[] options = {"Auto", "Manual"};
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity().getApplicationContext());
builder.setTitle("Add");
builder.setMessage("Auto add - download. \n Manually add - no internet connection.");
builder.setItems(options, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int selectionIndex) {
switch (selectionIndex)
{
case 0:
break;
case 1:
break;
}
}
});
builder.show();
}
错误:
E/AndroidRuntime: FATAL EXCEPTION: main
android.content.res.Resources$NotFoundException: Resource ID #0x0
最佳答案
您正在此处获取应用程序上下文
,但您需要获取调用 Activity 的上下文
。因此请更改您的代码
从此:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity().getApplicationContext());
对此:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
关于java - AlertDialog 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60394009/