NavigationDrawer适配器 - 指定的子级已经具有父级

时间:2014-10-17 00:13:33

标签: android navigation-drawer layout-inflater android-nested-fragment

正如标题所提到的,我遇到了一个问题,其中一个视图有多个父级。是否有使用NavigationBar的特定协议,它会使NestedFragments onItemSelected()膨胀?我在这个项目中缺少什么?任何指导都将不胜感激。

错误日志:

  10-16 17:27:02.282: E/AndroidRuntime(18090): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.inviscidlabs.schooled/com.inviscidlabs.schooled.ActivityClassEdit}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
    10-16 17:27:02.282: E/AndroidRuntime(18090):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java)
    10-16 17:27:02.282: E/AndroidRuntime(18090):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java)
    10-16 17:27:02.282: E/AndroidRuntime(18090):    at android.app.ActivityThread.access$800(ActivityThread.java)
    10-16 17:27:02.282: E/AndroidRuntime(18090):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java)
    10-16 17:27:02.282: E/AndroidRuntime(18090):    at android.os.Handler.dispatchMessage(Handler.java)
    10-16 17:27:02.282: E/AndroidRuntime(18090):    at android.os.Looper.loop(Looper.java)
    10-16 17:27:02.282: E/AndroidRuntime(18090):    at android.app.ActivityThread.main(ActivityThread.java)
    10-16 17:27:02.282: E/AndroidRuntime(18090):    at java.lang.reflect.Method.invokeNative(Native Method)
    10-16 17:27:02.282: E/AndroidRuntime(18090):    at java.lang.reflect.Method.invoke(Method.java:515)
    10-16 17:27:02.282: E/AndroidRuntime(18090):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
    10-16 17:27:02.282: E/AndroidRuntime(18090):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
    10-16 17:27:02.282: E/AndroidRuntime(18090):    at dalvik.system.NativeStart.main(Native Method)
    10-16 17:27:02.282: E/AndroidRuntime(18090): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
    10-16 17:27:02.282: E/AndroidRuntime(18090):    at android.view.ViewGroup.addViewInner(ViewGroup.java)
    10-16 17:27:02.282: E/AndroidRuntime(18090):    at android.view.ViewGroup.addView(ViewGroup.java)
    10-16 17:27:02.282: E/AndroidRuntime(18090):    at android.view.ViewGroup.addView(ViewGroup.java)
    10-16 17:27:02.282: E/AndroidRuntime(18090):    at android.view.ViewGroup.addView(ViewGroup.java)
    10-16 17:27:02.282: E/AndroidRuntime(18090):    at android.support.v4.app.NoSaveStateFrameLayout.wrap(NoSaveStateFrameLayout.java:40)
    10-16 17:27:02.282: E/AndroidRuntime(18090):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:946)
    10-16 17:27:02.282: E/AndroidRuntime(18090):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1121)
    10-16 17:27:02.282: E/AndroidRuntime(18090):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
    10-16 17:27:02.282: E/AndroidRuntime(18090):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1484)
    10-16 17:27:02.282: E/AndroidRuntime(18090):    at android.support.v4.app.Fragment.performStart(Fragment.java:1525)
    10-16 17:27:02.282: E/AndroidRuntime(18090):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:972)
    10-16 17:27:02.282: E/AndroidRuntime(18090):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1121)
    10-16 17:27:02.282: E/AndroidRuntime(18090):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1103)
    10-16 17:27:02.282: E/AndroidRuntime(18090):    at android.support.v4.app.FragmentManagerImpl.dispatchStart(FragmentManager.java:1906)
    10-16 17:27:02.282: E/AndroidRuntime(18090):    at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:588)
    10-16 17:27:02.282: E/AndroidRuntime(18090):    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java)
    10-16 17:27:02.282: E/AndroidRuntime(18090):    at android.app.Activity.performStart(Activity.

违规活动:

public class ActivityClassEdit extends FragmentActivity{

//Variables
    //Fragments
    private ContainerFragmentCriteria frag_Criteria;


    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private ActionBarDrawerToggle mDrawerToggle;


    //used as the effective ArrayAdapter for the NavigationDrawer
    private ClassEditDrawerAdapter mNavAdapter;
    private CharSequence sDrawerTitle;
    private CharSequence sTitle;

    //Serves as List of Strings to populate Nav Drawer's ListView of options
    List<ClassEditDrawerItem> navOptions;

     @Override
     protected void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);

           setContentView(R.layout.activity_classedit);

           //Initialize List
           navOptions= new ArrayList<ClassEditDrawerItem>();
           sTitle=sDrawerTitle=getTitle();

           mDrawerLayout = (DrawerLayout) findViewById(R.id.ace_drawer);
           mDrawerList=(ListView) findViewById(R.id.ace_drawer_list);

           navOptions.add(new ClassEditDrawerItem("Criteria", R.drawable.ic_launcher));

           mNavAdapter = new ClassEditDrawerAdapter(this, R.layout.item_navbar, navOptions);
           mDrawerList.setAdapter(mNavAdapter);
           //Set ListView onClickListener
           mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

           //Set up the home button to open the nav drawer
           getActionBar().setDisplayHomeAsUpEnabled(true);
           getActionBar().setHomeButtonEnabled(true);

           mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                   R.drawable.ic_launcher, R.string.hello_world,
                   R.string.name);

           mDrawerLayout.setDrawerListener(mDrawerToggle);

           //Select first item by default
           if(savedInstanceState==null){
               SelectItem(0);
           }
            //What to do when Item is Selected
     public void SelectItem(int position){
         FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

         switch(position){
         //Class Edit
         case 0: 
             if(frag_Criteria==null){
                 frag_Criteria = new ContainerFragmentCriteria();
             }
             transaction.replace(R.id.ace_frame, frag_Criteria);
             transaction.commit();
             break;


         }

      Toast.makeText(this, String.valueOf(position), Toast.LENGTH_LONG).show();
     }


     private class DrawerItemClickListener implements ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                   long id) {
                SelectItem(position);
        }
     }

自动充气的第一个片段:

public class ContainerFragmentCriteria extends Fragment{



    private FragmentManager fm;

    private boolean insertMode;

    //=====================ACTIVITY LIFECYCLE==============================
            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
                View v = inflater.inflate(R.layout.frag_container_criteria, container, false);
                return v;   
            }


        @Override
        public void onCreate(Bundle savedInstanceState){
            super.onCreate(savedInstanceState);

            //Instantiate Fragments, set Arguments
            FragmentCriteriaEdit fCrE= new FragmentCriteriaEdit();
            FragmentCriteriaList fCrL = new FragmentCriteriaList();

            Bundle fCritListArguments = new Bundle();
            fCritListArguments.putBoolean(CM.BKEY_INSERTMODE, insertMode);
            //Begin the Transaction
            fm=getChildFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            ft.add(R.id.fccr_rootLayout, fCrE, FragmentCriteriaEdit.sTag);
            ft.add(R.id.fccr_rootLayout, fCrL, FragmentCriteriaList.sTag);
            ft.commit();

        }

        @Override
        public void onStart() {
            super.onStart();
        //Get our arguments
            Bundle args = getArguments();
            if(args!=null){
                insertMode=args.getBoolean(CM.BKEY_INSERTMODE);
            }
        }

        @Override 
        public void onDetach() { 
            super.onDetach(); 

            try { 
                Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");
                childFragmentManager.setAccessible(true);
                childFragmentManager.set(this, null);

            } catch (NoSuchFieldException e) {
                throw new RuntimeException(e);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            } 
        } 



}

自定义NavigationDrawer适配器:

public class ClassEditDrawerAdapter extends ArrayAdapter<ClassEditDrawerItem>{

    Context ctx;
    List<ClassEditDrawerItem> itemList;
    int layoutResID;

    public ClassEditDrawerAdapter(Context context, int layoutResourceID, List<ClassEditDrawerItem> items){
        super(context, layoutResourceID, items);
        ctx=context;
        layoutResID=layoutResourceID;
        itemList=items;
    }

    //An Effective ViewHolder, but for this Array!
    private class DrawerItemHolderThing{
        TextView itemName;
    }


     @Override
     public View getView(int position, View convertView, ViewGroup parent) {
         DrawerItemHolderThing bobSagget;
         View v = convertView;

         if(v==null){
             Log.d("adapter", "v was null");
             LayoutInflater inflater = ((Activity) ctx).getLayoutInflater();
             bobSagget= new DrawerItemHolderThing();

             v=inflater.inflate(layoutResID, null);

             bobSagget.itemName=((TextView) v.findViewById(R.id.item_navbar_title));
             v.setTag(bobSagget);
         } else {
             bobSagget = (DrawerItemHolderThing) v.getTag();
         }


         ClassEditDrawerItem drawerItem = (ClassEditDrawerItem) this.itemList.get(position);
         if(bobSagget.itemName==null){Log.e("Adapter", "no TextView");}
         bobSagget.itemName.setText(drawerItem.getItemName());

         return v;

     }

}

1 个答案:

答案 0 :(得分:0)

在第一个自动膨胀的片段中,不应该引用父ViewGroup。在这种情况下,错误位于下面片段的onCreateView部分

 //=====================ACTIVITY LIFECYCLE============================== 
            @Override 
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
                View v = inflater.inflate(R.layout.frag_container_criteria, container, false);
                return v;   
            } 

应将inflater更改为:

View v = inflater.inflate(R.layout.frag_container_criteria, null, false);

注意容器的null参数。现在,当我尝试从NavigationDrawer中扩充它时,由于我们传递了null,因此View尚未注册到父级。