我之前看过这个问题,但答案并没有解决我的问题,所以我猜它可能是一个不同的问题。
当我运行它时,我的代码运行良好,但如果我将活动暂停一段时间并稍后再回来 - 它会崩溃。
该活动包含一个片段,该片段本身包含一个片段。崩溃发生在getContext().getString(R.string.vote_for)
上的onViewCreated()内部
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
TextView description = rootView.findViewById(R.id.votes_chart_desc);
description.setText(getArguments().getString("voteItemDesc"));
int votesForNum = getArguments().getInt("voteFor");
int voteAgainstNum = getArguments().getInt("voteAgainst");
int voteAbstainNum = getArguments().getInt("voteAbstain");
PieChart chart = rootView.findViewById(R.id.votes_chart);
ArrayList<PieEntry> entries = new ArrayList<>();
# CRASH happens here, inside the if, when evaluating getString().
if (votesForNum > 0) entries.add(new PieEntry(votesForNum, getContext().getString(R.string.vote_for)));
if (voteAgainstNum > 0) entries.add(new PieEntry(voteAgainstNum, getContext().getString(R.string.vote_against)));
if (voteAbstainNum > 0) entries.add(new PieEntry(voteAbstainNum, getContext().getString(R.string.vote_abstain)));
PieDataSet set = new PieDataSet(entries, "");
set.setColors(ColorTemplate.MATERIAL_COLORS);
PieData data = new PieData(set);
chart.setData(data);
chart.invalidate();
}
FATAL EXCEPTION: main
Process: [myprocess], PID: 11262
java.lang.RuntimeException: Unable to start activity ComponentInfo{...}: android.content.res.Resources$NotFoundException: String resource ID #0x7f0e003b
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2729)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2790)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1505)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:173)
at android.app.ActivityThread.main(ActivityThread.java:6523)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:938)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:828)
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x7f0e003b
at android.content.res.Resources.getText(Resources.java:352)
at android.content.res.Resources.getString(Resources.java:398)
at android.content.Context.getString(Context.java:476)
at [myprocess].Votes.VoteFragment.onViewCreated(VoteFragment.java:65)
...
getString()
接收和ID应该,并且该程序工作,直到我更改为不同的应用程序。rootView.getResources().getString(R.string.vote_for)
getString(R.string.vote_for)
getParentFragment().getActivity().getString(R.string.vote_for)
我在有问题的行上设置了一个断点,但是我在9个应用程序之间切换后调试器断开连接,所以在切换应用程序后我无法回到崩溃线。如果我对活动生命周期的理解是正确的,那么崩溃只发生在onStop()
,onResume()
恢复正常。
我已经尝试过自己解决这个问题,但我失败了。我没有想法。有没有其他人遇到类似的东西?谢谢。
问题源于我有几个string.xml文件,并且由于某种原因,在onRestart()
函数之后语言环境发生了变化。因此,onViewCreated()
引用了一个不同于我期望的xml文件。解决方案是只更改父活动的onResume()
函数中的区域设置。
保持strings.xml文件同步并且不会遗漏任何值也是一种很好的做法。如果我看到英文字符串,我就能更快地识别问题。