我使用奥托1.3.3 ,当我恢复的应用程序有时我得到一个抛出:IllegalArgumentException
与以下堆栈跟踪:
I am using Otto 1.3.3 and when I resume my application sometimes I get an IllegalArgumentException
with the following stacktrace:
Caused by: java.lang.IllegalArgumentException: Producer method for type class
com.couchsurfing.mobile.ui.setup
.SessionProviderFragment$SessionConnectionStateChangeEvent found on
type class com.couchsurfing.mobile.ui.setup.SessionProviderFragment,
but already registered by type class
com.couchsurfing.mobile.ui.setup.SessionProviderFragment.
at com.squareup.otto.Bus.register(Bus.java:194)
at com.couchsurfing.mobile.ui.BaseRetainedFragment
.onCreate(BaseRetainedFragment.java:20)
的 SessionProviderFragment
的它的实例保留,请找扩展的类如下:
The SessionProviderFragment
has its instance retained, please find below the extended class:
public abstract class BaseRetainedFragment extends SherlockFragment {
@Inject
Bus bus;
@Override
public void onCreate(final Bundle state) {
super.onCreate(state);
((CouchsurfingApplication) getActivity().getApplication()).inject(this);
setRetainInstance(true);
bus.register(this);
}
@Override
public void onDestroy() {
super.onDestroy();
bus.unregister(this);
bus = null;
}
}
我都尝试使用 bus.register(本)
在 onAttach()
或的onCreate()
,这并没有改变的问题。
I tried both using bus.register(this)
in onAttach()
or onCreate()
, that didn't change the issue.
我使用一个保留片段每个活动保存一个HTTP会话请求的状态。我的问题是,我并没有实例化我的保留碎片的正确方法。
I am using one "Retained Fragment" per activity to save the state of an HTTP session request. My issue was that I didn't instantiate my "Retained Fragment" the proper way.
在我在的onCreate():
Before I had in onCreate():
if (savedInstanceState == null) {
sessionProviderFragment = new SessionProviderFragment();
getSupportFragmentManager().beginTransaction().add(sessionProviderFragment,
SessionProviderFragment.TAG).commit();
}
显然高于code可以创建多个退出活性更高版本重新打开它时, SessionProviderFragment
。它的接缝正确的做法是:
Apparently the code above could create several SessionProviderFragment
when quitting the activity is reopening it later.
It seams that the correct way is :
sessionProviderFragment = (SessionProviderFragment) getSupportFragmentManager()
.findFragmentByTag(SessionProviderFragment.TAG);
// If not retained (or first time running), we need to create it.
if (sessionProviderFragment == null) {
sessionProviderFragment = new SessionProviderFragment();
getSupportFragmentManager().beginTransaction().add(sessionProviderFragment,
SessionProviderFragment.TAG).commit();
}
if (savedInstanceState == null) {
initUiFragment();
}
我也感动总线注册/注销在onResume /的onPause我BaseFragment,以确保我将永远有一个 SessionProviderFragment
一次在公共汽车上注册。
I also moved the bus register/unregister in onResume/onPause in my BaseFragment to be sure that I will always have one SessionProviderFragment
registered on the bus at a time.