Opened 6 weeks ago

Closed 6 weeks ago

#2631 closed defect (fixed)

java.lang.IllegalStateException: at android.support.v4.app.Fragment.requireContext

Reported by: Meeh Owned by: Meeh
Priority: minor Milestone: 0.9.43
Component: apps/android Version: 0.9.42
Keywords: Cc:
Parent Tickets: Sensitive: no

Description

java.lang.IllegalStateException: 
  at android.support.v4.app.Fragment.requireContext (Fragment.java:696)
  at android.support.v4.app.Fragment.getResources (Fragment.java:760)
  at net.i2p.android.router.MainFragment$5.onClick (MainFragment.java:586)
  at android.support.v7.app.AlertController$AlertParams$3.onItemClick (AlertController.java:1068)
  at android.widget.AdapterView.performItemClick (AdapterView.java:318)
  at android.widget.AbsListView.performItemClick (AbsListView.java:1223)
  at android.widget.AbsListView$PerformClick.run (AbsListView.java:3199)
  at android.widget.AbsListView$3.run (AbsListView.java:4302)
  at android.os.Handler.handleCallback (Handler.java:790)
  at android.os.Handler.dispatchMessage (Handler.java:99)
  at android.os.Looper.loop (Looper.java:164)
  at android.app.ActivityThread.main (ActivityThread.java:6651)
  at java.lang.reflect.Method.invoke (Native Method)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:438)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:810)

Subtickets

Change History (5)

comment:1 Changed 6 weeks ago by idk

I found this Stack Overflow question that looks like a very similar problem. https://stackoverflow.com/questions/41922616/android-button-illegalstateexception-with-onclickmainactivity. I'm looking to see if this applies to us and whether we can use the same solution.

comment:2 Changed 6 weeks ago by zzz

I don't think the above-linked SO page is relevant. The exception is being thrown in Fragment.getResources(), via our onClick() method. Javadoc says it returns Activity.getResources() which is from ContextThemeWrapper?. As there's no associated message, may take a tour through the source or more googling to find out why.

comment:3 Changed 6 weeks ago by zzz

https://stackoverflow.com/questions/51421865/java-lang-illegalstateexception-fragment-not-attached-to-a-context
https://medium.com/@shafran/fragment-getcontext-vs-requirecontext-ffc9157d6bbe

We have lots of getResources() calls in fragments. The issue is apparently that the fragment got 'detached' and we need to 'reattach' it? Not clear how to detect detachment and/or reattach.

comment:4 Changed 6 weeks ago by zzz

fragment lifecycle:
https://developer.android.com/guide/components/fragments
onAttach() is called before onStart() which calls checkDialog() so the problem is it got detached.

How about this which would get the resources in advance? (untested)

--- app/src/main/java/net/i2p/android/router/MainFragment.java	b6d53a9da7a552964fc256d20d5eaa623c5a06a9
+++ app/src/main/java/net/i2p/android/router/MainFragment.java	052b5ea292c3d1a899db5424e3cca9491f6c342d
@@ -578,12 +578,13 @@ public class MainFragment extends I2PFra
         );
         if (language == null) {
             AlertDialog.Builder b = new AlertDialog.Builder(getActivity());
+            final String languages[] = getResources().getStringArray(R.array.languages);
             b.setTitle(R.string.choose_language)
                     .setItems(R.array.language_names, new DialogInterface.OnClickListener() {
                         @Override
                         public void onClick(DialogInterface dialog, int which) {
                             // Save the language choice
-                            String language = getResources().getStringArray(R.array.languages)[which];
+                            String language = languages[which];
                             PreferenceManager.getDefaultSharedPreferences(getActivity())
                                     .edit()
                                     .putString(getString(R.string.PREF_LANGUAGE), language)

comment:5 Changed 6 weeks ago by zzz

Milestone: undecided0.9.43
Resolution: fixed
Status: newclosed
Note: See TracTickets for help on using tickets.