Domanda

I have a DialogFragment with a simple layout. I want to add a fragment (and in the future replace this fragment with another) to a FrameLayout that is inside the DialogFragment's layout. However, the method of adding the new fragment fails with the error:

"No view found for id 0x7f0b004f com.kennel39.diabeteslive_adtdev:id/frameAlertsContainer) for fragment Fragment_AlertsManage {41e7cb68}"

I have checked my xml, attempted different methods and have read a number of similar issues on stackoverflow but I cannot find a solution.

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>

And the Fragment_AlertsManage class:

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

Why am I getting this error, and what can I do to solve it?

È stato utile?

Soluzione

The doc on getFragmentManager():

Return the FragmentManager for interacting with fragments associated with this fragment's activity.

R.id.frameAlertsContainer is in the DialogFragment's layout, not the activity's layout, so it can't find it. Try using getChildFragmentManager() instead. However I can't tell if this will work inside the onCreateView() method since the view isn't associated with the fragment yet. You may need to put it in onStart() or something else.

Altri suggerimenti

I had this problem, for resolved, i can the getFragmentManager() in method OnStart() and work perfect.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top