我的应用有一个Activity,其布局包含3个片段。左侧和中间片段是右侧片段为细节的列表。
我创建了一个名为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$1.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.access$100(ActionBarActivityDelegateBase.java:79)
07-27 15:45:54.389: E/AndroidRuntime(15580): at android.support.v7.app.ActionBarActivityDelegateBase$1.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)
答案 0 :(得分:1)
1)似乎
View carerAwayDateTimeFragment = findViewById(R.id.carerawaydatetimefragment_container);
只是一个视图,但您尝试将其转换为不合适的类型。
2)这种方法不好,组织良好。您可以在每个片段中分别为每个片段指定菜单。
在onCreate
方法中指定setHasOptionsMenu(true);
并覆盖片段内的onCreateOptionsMenu
。
你不需要施放任何东西并找到碎片。每个片段都会处理自己的菜单。
答案 1 :(得分:0)
使用findFragmentById()
通过调用它来取回框架或正在使用的任何容器:
View carerAwayDateTimeFragment = findViewById(R.id.carerawaydatetimefragment_container);
相反,这样做:
CarerAwayDateTimeFragment carerAwayDateTimeFragment = findFragmentById(R.id.carerawaydatetimefragment_container);
使用findFragmentById()
代替findViewById()
并将变量更改为片段类类型。