方向改变后的回调成为空 [英] Callback after orientation change becomes null
问题描述
我有一个 FragmentActivity
两个选项卡这是 ListFragment
秒。每个 ListFragment
有所回调。
一个回调的例子
回调是内部关联的 onAttach(...)的方法
OnStatusUpdateListener mStatusUpdateCallback;公共接口OnStatusUpdateListener {
公共无效onStatusUpdate();
}@覆盖
公共无效onAttach(活动活动){
Log.d(TAGonAttach);
super.onAttach(活动); 尝试{
mStatusUpdateCallback =(OnStatusUpdateListener)活性;
}赶上(抛出ClassCastException E){
抛出新ClassCastException异常(activity.toString()+必须实现OnStatusUpdateListener);
}
}@覆盖
公共无效的onCreate(捆绑savedInstanceState){
Log.d(TAG的onCreate);
super.onCreate(savedInstanceState); setRetainInstance(真);
}
后来,我通过这个回调的正常工作正常。
与 FragmentActivity
沟通在 ListFragment
,我有一个的ImageButton
将调用一个 DialogFragment
其中也有一个回调。此回调中实现我的 ListFragment
,是什么触发回调是的空的
公共无效onStatusOption(){
Log.d(TAGonStatusOption); //更新的东西 //这里是mStatusUpdateCallback是旋转后空
mStatusUpdateCallback.onStatusUpdate();
}
问题是,如果我的应用程序运行时不断旋转手机, mStatusUpdateCallback 变的空的。当然,这意味着我无法执行回调。有谁知道如何解决这一问题?
我已经试过
根据 http://stackoverflow.com/a/6029070/935779 看来,一个新的参考 OnStatusUpdateListener
可能已创建,所以我不能引用旧,但不提供一个解决方案。
我也试着保持状态,按 http://stackoverflow.com/a/6787393/935779 ,但我不能保存到一个回调的引用,据我可以告诉。
我也确实不想做的android:configChanges =方向| keyboardHidden
方法,因为这似乎只是一个黑客和景观我的布局变化。 p>
堆栈跟踪
致命异常:主要
显示java.lang.NullPointerException
在com.blug.blah.Fragment.StatusFragment.onStatusOption(StatusFragment.java:197)
在com.blug.blah.MyActivity.onStatusOption(MyActivity.java:243)
在com.blug.blah.Dialog.StatusOptionDialog $ 1 $ 1.onClick(StatusOptionDialog.java:108)
在com.android.internal.app.AlertController $ ButtonHandler.handleMessage(AlertController.java:158)
在android.os.Handler.dispatchMessage(Handler.java:99)
在android.os.Looper.loop(Looper.java:123)
在android.app.ActivityThread.main(ActivityThread.java:4627)
在java.lang.reflect.Method.invokeNative(本机方法)
在java.lang.reflect.Method.invoke(Method.java:521)
在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868)
在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
在dalvik.system.NativeStart.main(本机方法)
在活动的配置
被破坏并重新创建。
在配置
手机改变的方法 onConfigurationChange
调用。
所以,你可以初始化回调
在 onConfigurationChange
I have have a FragmentActivity
with two tabs which are ListFragment
s. Each ListFragment
has a callback.
An example of a callback
The callback is associated inside of the onAttach(...) method
OnStatusUpdateListener mStatusUpdateCallback;
public interface OnStatusUpdateListener {
public void onStatusUpdate();
}
@Override
public void onAttach(Activity activity) {
Log.d(TAG, "onAttach");
super.onAttach(activity);
try {
mStatusUpdateCallback = (OnStatusUpdateListener)activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement OnStatusUpdateListener");
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "onCreate");
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
Later on, I communicate with the FragmentActivity
by this callback which works fine normally.
Within the ListFragment
, I have an ImageButton
that will call a DialogFragment
which also has a callback. This callback is implemented in my ListFragment
and is what triggers the callback that is null
public void onStatusOption() {
Log.d(TAG, "onStatusOption");
// Update stuff
// Here is where mStatusUpdateCallback is null after rotate
mStatusUpdateCallback.onStatusUpdate();
}
The problem is that if I ever rotate the phone while the application is running, mStatusUpdateCallback becomes null. This of course means I cannot execute the callback. Does anyone know how to fix this?
What I've tried
According to http://stackoverflow.com/a/6029070/935779 it appears that a new reference to OnStatusUpdateListener
may have been created so I cannot reference the old, but doesn't offer a solution.
I've also tried retaining the state as per http://stackoverflow.com/a/6787393/935779, but I can't save a reference to a callback as far as I can tell.
I also would really rather not do the android:configChanges="orientation|keyboardHidden"
method since that just seems like a hack and my layout changes in landscape.
Stacktrace
FATAL EXCEPTION: main
java.lang.NullPointerException
at com.blug.blah.Fragment.StatusFragment.onStatusOption(StatusFragment.java:197)
at com.blug.blah.MyActivity.onStatusOption(MyActivity.java:243)
at com.blug.blah.Dialog.StatusOptionDialog$1$1.onClick(StatusOptionDialog.java:108)
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:158)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
When configuration of the Activity
being destroy and recreates.
When Configuration
of the phone is changed the method onConfigurationChange
called.
So you can initialize your Callbacks
in onConfigurationChange
这篇关于方向改变后的回调成为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!