gpt4 book ai didi

Android AlertDialog异常 "Resources$NotFoundException"

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:14:24 25 4
gpt4 key购买 nike

尝试在 Android 中显示对话框时出现异常。使用以下代码从 FragmentActivity 调用我的 AlertDialog:

public static void displayShare(){
// show share options
CharSequence selections[] = new CharSequence[] {"Email", "SMS", "Tweet", "Phone Call", "Cancel"};
final AlertDialog.Builder builder = new AlertDialog.Builder(CommonVariables.mContext);
builder.setTitle("Share your location via...");
builder.setItems(selections, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch(which){
case 0: // Email
callEmailMethod();
break;
case 1: // SMS
callSMSMethod();
break;
case 2: // Tweet
callTwitterMethod();
break;
case 3: // Phone Call
callNumberMethod();
break;
case 4:
dialog.cancel();
break;
}
}
});
builder.show();
}

在该行收到以下错误:builder.show();

    FATAL EXCEPTION: main
Process: com.au.ewn.melbwater, PID: 2839
android.content.res.Resources$NotFoundException: Resource ID #0x0
at android.content.res.Resources.getValue(Resources.java:1351)
at android.content.res.Resources.loadXmlResourceParser(Resources.java:2774)
at android.content.res.Resources.getLayout(Resources.java:1165)
at android.view.LayoutInflater.inflate(LayoutInflater.java:421)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at android.support.v7.app.AlertController$AlertParams.createListView(AlertController.java:879)
at android.support.v7.app.AlertController$AlertParams.apply(AlertController.java:856)
at android.support.v7.app.AlertDialog$Builder.create(AlertDialog.java:899)
at android.support.v7.app.AlertDialog$Builder.show(AlertDialog.java:917)
at com.au.ewn.activities.MainFragment.displayShare(MainFragment.java:1081)
at com.au.ewn.activities.HelpMeScreen$2.onClick(HelpMeScreen.java:257)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

我已经尝试了所有方法(似乎除了正确的解决方案)。感谢任何帮助,谢谢。

注意:CommonVariables.mContext 是 FragmentActivity 的上下文,不为空:CommonVariables.mContext = FragmentAct.this;

最佳答案

问题是我的项目缺少 AlertDialog 的样式资源:

在 styles.xml 中放这个:

<style name="MyDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert" />

在您创建警报对话框的代码中放置以下内容:

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.MyDialogTheme);

感谢@Fraranc 在这篇文章中的回答:Resources$NotFoundException: Resource ID #0x0 in AlertDialog

关于Android AlertDialog异常 "Resources$NotFoundException",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38341207/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com