指定的子项已有父项。更改片段时,必须首先在子代的父级上调用removeView()

时间:2016-01-20 10:06:25

标签: navigation fragment drawer

您好我已经使用Imageview实现了导航抽屉,但是当我点击列表视图更改代码时它会给我上面的错误,请您检查下面的代码,让我知道我的代码有什么问题

import android.app.FragmentManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

import kangaroo.com.begazad.Fragment.UserProfile;
import kangaroo.com.begazad.R;
import kangaroo.com.begazad.adapter.DrawerItemCustomAdapter;
import kangaroo.com.begazad.bean.ObjectDrawerItem;

/**
 * Created by user on 1/20/2016.
 */
public class SliderNavigation extends FragmentActivity implements View.OnClickListener {

    private String url ="http://img11.deviantart.net/0d86/i/2012/071/5/4/brad_pitt_png_by_sweetteens-d4sk57v.png";
    private DrawerLayout mDrawerLayout;
    private RelativeLayout rl_Navigationdrawer;
    private ListView mDrawerList;
    private CharSequence mDrawerTitle;
    private CharSequence mTitle;
    ActionBarDrawerToggle mDrawerToggle;
    View headerView;

    @Override
    protected void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.slidernavigationlayout);
        mTitle = mDrawerTitle = getTitle();
        init();
        //Onclick();
        mDrawerLayout.openDrawer(mDrawerList);
        rl_Navigationdrawer.setOnClickListener(this);


    }

    public void init() {
        rl_Navigationdrawer = (RelativeLayout) findViewById(R.id.rl_Navigationdrawer);
        //  mNavigationDrawerItemTitles = getResources().getStringArray(R.array.navigation_drawer_items_array);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.left_drawer);

        ObjectDrawerItem[] drawerItem = new ObjectDrawerItem[5];

        drawerItem[0] = new ObjectDrawerItem(R.drawable.home, "Dashboard");
        drawerItem[1] = new ObjectDrawerItem(R.drawable.friends, "Friends");
        drawerItem[2] = new ObjectDrawerItem(R.drawable.groups, "Groups");
        drawerItem[3] = new ObjectDrawerItem(R.drawable.message, "Messages");
        drawerItem[4] = new ObjectDrawerItem(R.drawable.cart, "Shopping Cart");
        // drawerItem[5] = new ObjectDrawerItem(R.mipmap.transaction, "Transaction");
        //  drawerItem[6] = new ObjectDrawerItem(R.mipmap.inivtefriend, "Help");

        DrawerItemCustomAdapter adapter = new DrawerItemCustomAdapter(this, R.layout.list_item, drawerItem);
        mDrawerList.setAdapter(adapter);
        headerView = LayoutInflater.from(this).inflate(R.layout.list_headerview,null);

        ImageLoader imageLoader = ImageLoader.getInstance();

        imageLoader.init(ImageLoaderConfiguration.createDefault(getApplicationContext()));

        DisplayImageOptions options = new DisplayImageOptions.Builder().displayer(new RoundedBitmapDisplayer(1000)).cacheInMemory(true)
                .cacheOnDisc(true).resetViewBeforeLoading(true)
                .showImageForEmptyUri(R.drawable.images)
                .showImageOnFail(R.drawable.images)
                .showImageOnLoading(R.drawable.images).bitmapConfig(Bitmap.Config.RGB_565).build();


        ImageView imgview= ((ImageView) headerView.findViewById(R.id.iv_navigation));


        imageLoader.displayImage(url, imgview, options);

        mDrawerList.addHeaderView(headerView);



        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.rl_Navigationdrawer:
                rl_Navigationdrawer.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mDrawerLayout.openDrawer(mDrawerList);
                    }
                });
        }
    }


    private class DrawerItemClickListener implements ListView.OnItemClickListener {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            selectItem(position);
            mDrawerLayout.closeDrawer(mDrawerList);
        }

    }

    private void selectItem(int position) {

        Fragment fragment = null;

        View view = this.getCurrentFocus();
        if (view != null) {
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }

        switch (position) {
            case 0:

                //     fragment = new Dashboard_Activity();
                System.out.println("Clicked"+mDrawerList.getItemAtPosition(position));
                fragment= new UserProfile();

                Toast.makeText(SliderNavigation.this, "Clicked", Toast.LENGTH_SHORT).show();

                //  fragment = new CreateFragment();
                break;
            case 1:
                //fragment = new BookAppointment();

                Toast.makeText(SliderNavigation.this, "Clicked", Toast.LENGTH_SHORT).show();
                break;
            case 2:
//                    fragment = new UnderDevelopment();
                //  Toast.makeText(Dashboard_Activity.this, "Clicked", Toast.LENGTH_SHORT).show();
                Toast.makeText(SliderNavigation.this, "Clicked", Toast.LENGTH_SHORT).show();
                break;


            default:
                break;
        }

        if (fragment != null) {

            System.out.println("IN IF");
            android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

            // update selected item and title, then close the drawer
            mDrawerList.setItemChecked(position, true);
            mDrawerList.setSelection(position);
            // setTitle(navMenuTitles[position]);
            mDrawerLayout.closeDrawer(mDrawerList);

        } else {
            Log.e("MainActivity", "Error in creating fragment");
        }
    }
}

0 个答案:

没有答案