Android 将片段切换到容器时的ClassCastException
我的应用程序有一个活动,其布局包含3个片段。左边和中间的片段是列表,右边的片段是详细信息 我创建了一个名为onArticleSelected的方法,无论用户是否长时间单击左侧列表,该方法都会传入一个布尔值。如果长时间单击,将加载片段CareAwayDatesFragment,而如果短时间单击,将加载片段CareAwayListFragment 我在PrepareOptions菜单上收到一个ClassCastException。如果首先加载Fragment CareAwayDatesFragment,然后短按左侧列表,则会出现此异常 我希望根据加载的片段在菜单中加载不同的选项 有人知道我为什么会得到这个例外吗 提前谢谢Android 将片段切换到容器时的ClassCastException,android,android-fragments,classcastexception,Android,Android Fragments,Classcastexception,我的应用程序有一个活动,其布局包含3个片段。左边和中间的片段是列表,右边的片段是详细信息 我创建了一个名为onArticleSelected的方法,无论用户是否长时间单击左侧列表,该方法都会传入一个布尔值。如果长时间单击,将加载片段CareAwayDatesFragment,而如果短时间单击,将加载片段CareAwayListFragment 我在PrepareOptions菜单上收到一个ClassCastException。如果首先加载Fragment CareAwayDatesFragmen
@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
1) 好像
View carerAwayDateTimeFragment = findViewById(R.id.carerawaydatetimefragment_container);
只是一个视图,但您试图将其转换为不合适的类型
2) 这种方法不好,也没有很好的组织。您可以在每个片段中分别为每个片段指定菜单
在onCreate
方法中指定sethaspoptions菜单(true)代码>和重写片段内的创建选项菜单
你不需要投掷任何东西,也不需要找到碎片。每个片段将自己处理菜单。使用
findFragmentById()
通过调用以下命令,您将返回框架或正在使用的任何容器:
View carerAwayDateTimeFragment = findViewById(R.id.carerawaydatetimefragment_container);
相反,请执行以下操作:
CarerAwayDateTimeFragment CarerAwayDateTimeFragment=findFragmentById(R.id.CarerAwayDateTimeFragment_容器)
使用
findFragmentById()
而不是findViewById()
并将变量更改为片段类类型 您好,我需要查看carerAwayDateTimeFragment=findViewById(R.id.carerAwayDateTimeFragment\u容器);因为如果它返回null,那么应用程序必须在手机上运行,并加载DisplayCarerAwayDateTimeFragmentActivityworks,就像一个符咒和许多更干净的代码一样。感谢alothi,它说findFragmentById()对于活动是未定义的,所以不能使用它。getSupportFragmentManager()。findFragmentById()试试这个?
View carerAwayDateTimeFragment = findViewById(R.id.carerawaydatetimefragment_container);
View carerAwayDateTimeFragment = findViewById(R.id.carerawaydatetimefragment_container);