回调:java.lang.ClassCastException

时间:2016-06-22 12:59:18

标签: android android-activity callback fragment

问题:我需要将callbackfragment传递给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. 
    }

    (...)

}

1 个答案:

答案 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();
}