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
- This question seemed similar (resource exists, but error) - Cleaning the project did not help
- 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. - Not similar to my issue, OP had their layout removed somehow. My layout exists, and works until you switch apps.
- Duplicate of 2, irrelevant.
- Duplicate of 2, irrelevant.
- Changing the crashing line to
rootView.getResources().getString(R.string.vote_for)
- Changing the crashing line to
getString(R.string.vote_for)
- 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.