getResources()上的IllegalStatexception片段[英] Fragment IllegalStatexception on getResources()

本文是小编为大家收集整理的关于getResources()上的IllegalStatexception片段的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我在SubCategoryFragment内有这条线:

s.group_name = SubCategoryFragment.this.getResources().getString(R.string.everything_else); // line 315 in stacktrace

我的理解是,通过添加subcategoryfragment.

这是ListFragment.当有人单击此片段中的列表项目并转到另一个时,就会发生这种情况.然后命中后按钮并返回SubCategoryFragment. onActivityCreated()上面的这条线最终居住.

我在与此相关的内部不做任何事情.

java.lang.IllegalStateException: 
  at android.support.v4.app.Fragment.getResources (Fragment.java:608)
  at ---.---.com.--.SubCategoryFragment.buildSubcategories (SubCategoryFragment.java:315)
  at ---.---.com.---.SubCategoryFragment$2.onResponse (SubCategoryFragment.java:287)
  at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run (ExecutorCallAdapterFactory.java:70)
  at android.os.Handler.handleCallback (Handler.java:751)
  at android.os.Handler.dispatchMessage (Handler.java:95)
  at android.os.Looper.loop (Looper.java:154)
  at android.app.ActivityThread.main (ActivityThread.java:6776)
  at java.lang.reflect.Method.invoke (Native Method)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:1520)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1410)

推荐答案

Fragment.getResources()是一个便利调用,可以从当前附加到Activity的任何Activity中获取Resources对象.如果您的片段没有附在任何活动上,它将抛出IllegalStateException.

呼叫SubCategoryFragment.this.getResources() vs Just getResources()不会有任何区别.

这个问题有两种可能的解决方案.首先是将呼叫移至一个保证您的片段将附加到某些活动的地方.另一个是从片段以外的某个地方获得一个Resources对象(因此片段的活动).如果您可以访问其他Context对象(也许通过View可以调用View.getContext()),则可以致电Context.getResources().

本文地址:https://www.itbaoku.cn/post/2091074.html

问题描述

I have this line inside SubCategoryFragment:

s.group_name = SubCategoryFragment.this.getResources().getString(R.string.everything_else); // line 315 in stacktrace

My understanding was that by adding SubCategoryFragment.this to this would prevent the IllegalStatexception.

This is a ListFragment. This occurs when someone clicks on a list item in this fragment, and goes to another; then hits the back button and returns to SubCategoryFragment. onActivityCreated() where this line above ultimately resides.

I don't do anything inside onDetach() related to this.

java.lang.IllegalStateException: 
  at android.support.v4.app.Fragment.getResources (Fragment.java:608)
  at ---.---.com.--.SubCategoryFragment.buildSubcategories (SubCategoryFragment.java:315)
  at ---.---.com.---.SubCategoryFragment$2.onResponse (SubCategoryFragment.java:287)
  at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run (ExecutorCallAdapterFactory.java:70)
  at android.os.Handler.handleCallback (Handler.java:751)
  at android.os.Handler.dispatchMessage (Handler.java:95)
  at android.os.Looper.loop (Looper.java:154)
  at android.app.ActivityThread.main (ActivityThread.java:6776)
  at java.lang.reflect.Method.invoke (Native Method)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:1520)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1410)

推荐答案

Fragment.getResources() is a convenience call that fetches the Resources object from whatever Activity your fragment is currently attached to. If your fragment isn't attached to any activity, it will throw IllegalStateException.

Calling SubCategoryFragment.this.getResources() vs just getResources() won't make any difference.

There are two possible solutions to this problem. The first is to move the call to a place where you are guaranteed that your fragment will be attached to some activity. The other is to get a Resources object from somewhere other than the fragment (and therefore the fragment's activity). If you have access to some other Context object (perhaps by having a View you can call View.getContext() on), you can call Context.getResources() instead.