我有一个简单布局的DialogFragment。我想添加一个片段(并在将来将此片段替换为另一个片段)到DialogFragment布局内的FrameLayout。但是,添加新片段的方法失败,错误为:
“找不到id 0x7f0b004f com.kennel39.diabeteslive_adtdev:id / frameAlertsContainer的视图”片段Fragment_AlertsManage {41e7cb68}“
我已经检查了我的xml,尝试了不同的方法,并在stackoverflow上读了很多类似的问题,但我找不到解决方案。
public class DialogManageAlerts extends DialogFragment{
static int patient_id;
public static DialogManageAlerts newInstance(int given_patient_id){
DialogManageAlerts frag = new DialogManageAlerts();
patient_id = given_patient_id;
Bundle bund = new Bundle();
bund.putInt("Patient_id", patient_id);
frag.setArguments(bund);
return frag;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View mainView = inflater.inflate(R.layout.dialog_alerts_master, container, false);
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
FragmentManager myFragmentManager = getFragmentManager();
FragmentTransaction myTransact = myFragmentManager.beginTransaction();
Fragment_AlertsManage manageAlertsFragment = new Fragment_AlertsManage();
myTransact.add(R.id.frameAlertsContainer, manageAlertsFragment);
myTransact.commit();
return mainView;
}
layout.dialog_alerts_master:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/white_regular" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:background="@drawable/lower_border_background_white">
<TextView
android:id="@+id/tvManageAlertsTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/manage_alerts_title"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_horizontal" />
</LinearLayout>
<FrameLayout
android:id="@+id/frameAlertsContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</FrameLayout>
</LinearLayout>
Fragment_AlertsManage类:
public class Fragment_AlertsManage extends Fragment implements OnClickListener {
int patient_id;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View myView = inflater.inflate(R.layout.fragment_alerts_manage, container, false);
//Get buttons
Button btnAdd = (Button)myView.findViewById(R.id.btnAddAlert);
btnAdd.setOnClickListener(this);
Button btnBack = (Button)myView.findViewById(R.id.btnBack);
btnBack.setOnClickListener(this);
//FILL CONTENT
populateContent(myView);
return myView;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btnAddAlert: {
//Switch fragment
FragmentManager myFragmentManager = getFragmentManager();
FragmentTransaction myTransact = myFragmentManager.beginTransaction();
Fragment_AlertsAdd addAlertFragment = new Fragment_AlertsAdd();
myTransact.addToBackStack("Previous");
myTransact.replace(this.getView().getId(), addAlertFragment);
myTransact.commit();
break;
}
case R.id.btnBack: {
FragmentManager myFragmentManager = getFragmentManager();
FragmentTransaction myTransact = myFragmentManager.beginTransaction();
myTransact.remove(Fragment_AlertsManage.this);
//Launch Home
Intent homeIntent = new Intent(this.getActivity(), HomeActivity.class);
homeIntent.putExtra("patient_id", patient_id);
startActivity(homeIntent);
getActivity().finish();
break;
}
}
}
public void populateContent(View myView){
try {
ArrayList<Alert> alerts = new RetrieveAlerts(patient_id).execute().get();
ListView list = (ListView) myView.findViewById(R.id.reminder_listview);
AlertsListAdapter alertsAdapter = new AlertsListAdapter(this.getActivity(), alerts);
list.setAdapter(alertsAdapter);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
为什么我会收到此错误,我该怎么做才能解决?
答案 0 :(得分:5)
getFragmentManager()
上的文档:
返回FragmentManager以与相关的片段进行交互 这个片段的活动。
R.id.frameAlertsContainer位于DialogFragment的布局中,而不是活动的布局,因此无法找到它。请尝试使用getChildFragmentManager()。但是我无法判断这是否在onCreateView()方法中有效,因为视图尚未与片段相关联。您可能需要将其放在onStart()或其他内容中。
答案 1 :(得分:0)
我有这个问题,为了解决,我可以在方法OnStart()中使用getFragmentManager()并且工作完美。