字符串onStart()的android.content.res.Resources $ NotFoundException

问题描述 投票:0回答:1

Problem

我之前看过这个问题,但答案并没有解决我的问题,所以我猜它可能是一个不同的问题。

当我运行它时,我的代码运行良好,但如果我将活动暂停一段时间并稍后再回来 - 它会崩溃。

该活动包含一个片段,该片段本身包含一个片段。崩溃发生在getContext().getString(R.string.vote_for)上的onViewCreated()内部

Code

@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();
}

Stacktrace

 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)
      ...

Things I've tried / questions I've searched

  1. This question seemed similar (resource exists, but error) - 清理项目没有帮助
  2. This is not similar, but similar exception - getString()接收和ID应该,并且该程序工作,直到我更改为不同的应用程序。
  3. Not similar to my issue,OP以某种方式删除了他们的布局。我的布局存在,直到你切换应用程序。
  4. Duplicate of 2,无关紧要。
  5. Duplicate of 2,无关紧要。
  6. 将崩溃线更改为rootView.getResources().getString(R.string.vote_for)
  7. 将崩溃线更改为getString(R.string.vote_for)
  8. 将崩溃线更改为getParentFragment().getActivity().getString(R.string.vote_for)

Debugger info

我在有问题的行上设置了一个断点,但是我在9个应用程序之间切换后调试器断开连接,所以在切换应用程序后我无法回到崩溃线。如果我对活动生命周期的理解是正确的,那么崩溃只发生在onStop()onResume()恢复正常。

我已经尝试过自己解决这个问题,但我失败了。我没有想法。有没有其他人遇到类似的东西?谢谢。

android android-fragments activity-lifecycle
1个回答
0
投票

问题源于我有几个string.xml文件,并且由于某种原因,在onRestart()函数之后语言环境发生了变化。因此,onViewCreated()引用了一个不同于我期望的xml文件。解决方案是只更改父活动的onResume()函数中的区域设置。

保持strings.xml文件同步并且不会遗漏任何值也是一种很好的做法。如果我看到英文字符串,我就能更快地识别问题。

© www.soinside.com 2019 - 2024. All rights reserved.