问题:我需要将callback
从fragment
传递给acitvity
。不幸的是,我得到了堆栈跟踪:
为什么我会收到错误?我发现了一些类似问题的帖子。不幸的是,我仍然不明白为什么我会遇到这个问题。
FATAL EXCEPTION: main
Process:
com.stack.overflow, PID: 26190
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.stack.overflow/com.stack.overflow.main.MainActivity}: java.lang.ClassCastException: com.stack.overflow.main.MainActivity cannot be cast tocom.stack.overflow.ReviewMaschineFragment$Callbacks
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.ClassCastException:com.stack.overflow.main.MainActivity cannot be cast to com.stack.overflow.reviewmaschine.ReviewMaschineFragment$Callbacks
at com.stack.overflow.reviewmaschine.ReviewMaschineFragment.onAttach(ReviewMaschineFragment.java:101)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1039)
at android.support.v4.app.BackStackRecord.setLastIn(BackStackRecord.java:795)
at android.support.v4.app.BackStackRecord.calculateFragments(BackStackRecord.java:818)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:676)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1671)
at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:388)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:619)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1236)
at android.app.Activity.performStart(Activity.java:6006)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2288)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
我的fragment
课程:
public class ReviewMaschineFragment extends Fragment {
(...)
public interface Callbacks {
void onUserChangedData();
}
private Callbacks mCallbacks;
(...)
@Override
public void onAttach(Context context) {
super.onAttach(context);
mCallbacks = (Callbacks) context;
EventBus.getDefault().register(this);
}
@Override
public void onDetach() {
super.onDetach();
mCallbacks = null;
EventBus.getDefault().unregister(this);
}
}
我的activity
课程:
public class ReviewActivity extends AppCompatActivity implements ReviewMaschineFragment.Callbacks{
(...)
@Override
public void onUserChangedData() {
// Do something.
}
(...)
}
答案 0 :(得分:4)
您的MainActivity
正在使用ReviewMaschineFragment
但未实施Callbacks
。
仅当活动实施onAttach
时,才更改Callbacks
方法以分配回调:
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof Callbacks) {
mCallbacks = (Callbacks) context;
}
EventBus.getDefault().register(this);
}
现在,当您使用mCallbacks
时,先检查它是否为空
if (mCallbacks != null) {
mCallbacks.onUserChangedData();
}