1

Problem

I've seen this question posted before, but the answers did not solve my problem, so I'm guessing it may be a different problem.

My code works well when I run it, but if I leave the activity on for a while and come back to it later - it crashes.

The activity contains a fragment, which itself contains a fragment. The crash occurs inside onViewCreated() on getContext().getString(R.string.vote_for)

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) - Cleaning the project did not help
  2. This is not similar, but similar exception - getString() receives and ID as it should, and the program works until I change to a different app.
  3. Not similar to my issue, OP had their layout removed somehow. My layout exists, and works until you switch apps.
  4. Duplicate of 2, irrelevant.
  5. Duplicate of 2, irrelevant.
  6. Changing the crashing line to rootView.getResources().getString(R.string.vote_for)
  7. Changing the crashing line to getString(R.string.vote_for)
  8. Changing the crashing line to getParentFragment().getActivity().getString(R.string.vote_for)

Debugger info

I put a breakpoint on the problematic line, but the debugger disconnects after I switch between 9 apps, so I cannot come back to the crashing line after switching between apps. If my understanding of the activity lifecycle is correct, the crash happens only onStop(), onResume() resumes as normal.

I've tried resolving this issue on my own, but I failed. I'm out of ideas. Has anyone else encountered something similar? Thank you.

Alex Osheter
  • 589
  • 5
  • 22
  • 1st clean project that is not work than try that getContext().getResources().getString(R.string.vote_for) or getActivity().getResources().getString(R.string.vote_for) – Engr Waseem Arain Sep 17 '18 at 12:53
  • It didn't work. – Alex Osheter Sep 17 '18 at 12:59
  • 1
    It's not able to find one of the strings (`android.content.res.Resources$NotFoundException`). Make sure all those strings exists, clean your project and then retry. – Mudassir Sep 17 '18 at 13:02
  • Read my post, I did all of those things. The app works until I reload it. – Alex Osheter Sep 17 '18 at 13:03
  • 2
    Its clearly an issue of missing resource. What's there in strings.xml at `vote_******` strings..? – Mudassir Sep 17 '18 at 13:07
  • Check that https://stackoverflow.com/questions/33338785/how-to-link-a-string-property-from-string-xml-in-android-fragment – Engr Waseem Arain Sep 17 '18 at 13:11
  • @AnCoder Wow, this comment really helped. I have two strings.xml files, one in English, the other in Hebrew. The string was missing from the english xml file. I added the string to the english strings file, but now the string is in English even if the entire activity's locale is set to iw/he. – Alex Osheter Sep 17 '18 at 13:14
  • Make sure those strings are present in all string resource files (strings.xml). – Mudassir Sep 17 '18 at 13:19

1 Answers1

0

The problem stemmed from the fact that I had several string.xml files, and for some reason the locale changed after the onRestart() function. So as a result, onViewCreated() was referencing a different xml file than I was expecting. The solution was to just change locale in the onResume() function of the parent activity.

It's also good practice to keep the strings.xml files synced and not miss any values. Had I seen the strings in English, I would have been able to identify the problem much quicker.

Alex Osheter
  • 589
  • 5
  • 22