将片段切换到容器时出现 ClassCastException

ClassCastException when switching Fragments into container

我的应用有一个 Activity,其布局包含 3 个 Fragment。左侧和中间片段是列表,其中右侧片段是详细信息。

我创建了一个名为 onArticleSelected 的方法,无论用户是否长按左侧列表,它都会传递一个布尔值。如果是长按,片段 CareAwayDatesFragment 将加载,而如果是短按,片段 CarerAwayListFragment 将加载。

我在 OnPrepareOptionsMenu 上收到 ClassCastException。如果首先加载 Fragment CareAwayDatesFragment 然后我短按左侧列表,我得到这个异常。

我希望根据加载的片段在菜单中加载不同的选项。

有谁知道我为什么会收到此异常?

提前致谢。

@Override
    public void onArticleSelected(String carerId, String _carerFirstName, String _carerLastName, String carerTelno, boolean longClick) {

        Log.e(TAG, "onArticleSelected CarerAwayActivity");

        if(longClick == true){
        Log.e(TAG, "longClick = true");


                this.carerID = carerId;
                this.carerFirstName = _carerFirstName;
                this.carerLastName = _carerLastName;


                View carerAwayDateTimeFragment = findViewById(R.id.carerawaydatetimefragment_container);

                if (carerAwayDateTimeFragment != null ) {



                Fragment newFragment = new CarerAwayDateTimeFragment();


                FragmentTransaction transaction = getFragmentManager().beginTransaction();

                // Replace whatever is in the fragment_container view with this fragment,
                // and add the transaction to the back stack
                transaction.replace(R.id.carerawaydatetimefragment_container, newFragment);
                //transaction.addToBackStack(null);

                // Commit the transaction
                transaction.commit();


                }else{

                    Intent intent = new Intent(this, DisplayCarerAwayDateTimeFragmentActivity.class);
                    intent.putExtra("carerid", carerId);
                    intent.putExtra("carerfirstname", carerFirstName);
                    intent.putExtra("carerlastname", carerLastName);

                    startActivity(intent);


                }

        }else{
            Log.e(TAG, "longClick = false");    

            this.carerID = carerId;
            this.carerFirstName = _carerFirstName;
            this.carerLastName = _carerLastName;


            View carerAwayDateTimeFragment = findViewById(R.id.carerawaydatetimefragment_container);

            if (carerAwayDateTimeFragment != null ) {

                DateTime dt = new DateTime();
                DateTimeFormatter fmt = DateTimeFormat.forPattern("d-MMM-Y");
                String from = fmt.print(dt);
                String to = fmt.print(dt);

                String[] params = new String[] { from, to, carerId};
                AsyncGetCarerAwayDates agcad = new AsyncGetCarerAwayDates();
                agcad.execute(params);

            }else{

//              Intent intent = new Intent(this, DisplayCarerListFragmentActivity.class);
//              intent.putExtra("carerid", carerId);
//              intent.putExtra("carerfirstname", carerFirstName);
//              intent.putExtra("carerlastname", carerLastName);
//              
//              startActivity(intent);


            }
        }

    }//end of onArticleSelected

.

@Override
     public boolean onPrepareOptionsMenu(Menu menu) {
        super.onPrepareOptionsMenu(menu);

        CarerListFragment carerListFragment = (CarerListFragment) getFragmentManager().findFragmentById(R.id.carerlist);
        CarerAwayListFragment carerAwayListFragment = (CarerAwayListFragment) getFragmentManager().findFragmentById(R.id.carerawaydatetimefragment_container);

        if(carerListFragment != null && carerAwayListFragment != null){

             menu.getItem(0).setVisible(false);
             menu.getItem(1).setVisible(false);
             menu.getItem(2).setVisible(false);
             menu.getItem(3).setVisible(false);
             menu.getItem(4).setVisible(true);
             menu.getItem(5).setVisible(false);
             menu.getItem(6).setVisible(false);



        }else{

             menu.getItem(0).setVisible(false);
             menu.getItem(1).setVisible(false);
             menu.getItem(2).setVisible(false);
             menu.getItem(3).setVisible(false);
             menu.getItem(4).setVisible(false);
             menu.getItem(5).setVisible(false);
             menu.getItem(6).setVisible(false);

        }

.

07-27 15:45:54.389: E/AndroidRuntime(15580): FATAL EXCEPTION: main
07-27 15:45:54.389: E/AndroidRuntime(15580): Process: com.carefreegroup.rr3.carefreeoncall, PID: 15580
07-27 15:45:54.389: E/AndroidRuntime(15580): java.lang.ClassCastException: com.carefreegroup.rr3.carefreeoncall.CarerAwayDateTimeFragment cannot be cast to com.carefreegroup.rr3.carefreeoncall.CarerAwayListFragment
07-27 15:45:54.389: E/AndroidRuntime(15580):    at com.carefreegroup.rr3.carefreeoncall.CarerAwayActivity.onPrepareOptionsMenu(CarerAwayActivity.java:514)
07-27 15:45:54.389: E/AndroidRuntime(15580):    at android.app.Activity.onPreparePanel(Activity.java:2643)
07-27 15:45:54.389: E/AndroidRuntime(15580):    at android.support.v4.app.FragmentActivity.onPrepareOptionsPanel(FragmentActivity.java:469)
07-27 15:45:54.389: E/AndroidRuntime(15580):    at android.support.v7.app.ActionBarActivity.superOnPrepareOptionsPanel(ActionBarActivity.java:284)
07-27 15:45:54.389: E/AndroidRuntime(15580):    at android.support.v7.app.ActionBarActivityDelegate.onPrepareOptionsPanel(ActionBarActivityDelegate.java:204)
07-27 15:45:54.389: E/AndroidRuntime(15580):    at android.support.v7.app.ActionBarActivity.onPrepareOptionsPanel(ActionBarActivity.java:256)
07-27 15:45:54.389: E/AndroidRuntime(15580):    at android.support.v4.app.FragmentActivity.onPreparePanel(FragmentActivity.java:458)
07-27 15:45:54.389: E/AndroidRuntime(15580):    at android.support.v7.app.ActionBarActivity.superOnPreparePanel(ActionBarActivity.java:280)
07-27 15:45:54.389: E/AndroidRuntime(15580):    at android.support.v7.app.ActionBarActivityDelegate.onPreparePanel(ActionBarActivityDelegate.java:84)
07-27 15:45:54.389: E/AndroidRuntime(15580):    at android.support.v7.app.ActionBarActivityDelegateBase.preparePanel(ActionBarActivityDelegateBase.java:1006)
07-27 15:45:54.389: E/AndroidRuntime(15580):    at android.support.v7.app.ActionBarActivityDelegateBase.doInvalidatePanelMenu(ActionBarActivityDelegateBase.java:1182)
07-27 15:45:54.389: E/AndroidRuntime(15580):    at android.support.v7.app.ActionBarActivityDelegateBase.access0(ActionBarActivityDelegateBase.java:79)
07-27 15:45:54.389: E/AndroidRuntime(15580):    at android.support.v7.app.ActionBarActivityDelegateBase.run(ActionBarActivityDelegateBase.java:115)
07-27 15:45:54.389: E/AndroidRuntime(15580):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:803)
07-27 15:45:54.389: E/AndroidRuntime(15580):    at android.view.Choreographer.doCallbacks(Choreographer.java:603)
07-27 15:45:54.389: E/AndroidRuntime(15580):    at android.view.Choreographer.doFrame(Choreographer.java:572)
07-27 15:45:54.389: E/AndroidRuntime(15580):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:789)
07-27 15:45:54.389: E/AndroidRuntime(15580):    at android.os.Handler.handleCallback(Handler.java:733)
07-27 15:45:54.389: E/AndroidRuntime(15580):    at android.os.Handler.dispatchMessage(Handler.java:95)
07-27 15:45:54.389: E/AndroidRuntime(15580):    at android.os.Looper.loop(Looper.java:157)
07-27 15:45:54.389: E/AndroidRuntime(15580):    at android.app.ActivityThread.main(ActivityThread.java:5350)
07-27 15:45:54.389: E/AndroidRuntime(15580):    at java.lang.reflect.Method.invokeNative(Native Method)
07-27 15:45:54.389: E/AndroidRuntime(15580):    at java.lang.reflect.Method.invoke(Method.java:515)
07-27 15:45:54.389: E/AndroidRuntime(15580):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
07-27 15:45:54.389: E/AndroidRuntime(15580):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
07-27 15:45:54.389: E/AndroidRuntime(15580):    at dalvik.system.NativeStart.main(Native Method)

1) 好像是

View carerAwayDateTimeFragment = findViewById(R.id.carerawaydatetimefragment_container);

只是一个视图,但您试图将其转换为不合适的类型。

2) 这种方法不好,组织也不好。您可以在每个片段中分别为每个片段指定菜单。

onCreate 方法中指定 setHasOptionsMenu(true); 并覆盖片段内的 onCreateOptionsMenu

你不需要施放任何东西并找到碎片。每个片段将自己处理菜单。

使用findFragmentById()

您将通过调用以下命令取回框架或您正在使用的任何容器:

View carerAwayDateTimeFragment = findViewById(R.id.carerawaydatetimefragment_container);

相反,这样做:

CarerAwayDateTimeFragment carerAwayDateTimeFragment = findFragmentById(R.id.carerawaydatetimefragment_container);

使用findFragmentById()代替findViewById()并将变量更改为片段class类型。