导航抽屉出错

时间:2014-03-17 11:37:44

标签: android map drawer

这是我的logcat: 实际上我正面临着抽屉里的地图。我尽我所能,但仍然缺少一些东西。试图显示像卫星,普通,混合等地图类型这里是我的代码。点击抽屉时出现错误。我在这里贴了我的原木猫。如果此处有人有任何建议请分享:

   03-15 16:05:16.146: E/AndroidRuntime(25955): FATAL EXCEPTION: main
   03-15 16:05:16.146: E/AndroidRuntime(25955): android.view.InflateException: Binary XML file line #20: Error inflating class fragment
   03-15 16:05:16.146: E/AndroidRuntime(25955):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
   03-15 16:05:16.146: E/AndroidRuntime(25955):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
   03-15 16:05:16.146: E/AndroidRuntime(25955):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
   03-15 16:05:16.146: E/AndroidRuntime(25955):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
   03-15 16:05:16.146: E/AndroidRuntime(25955):     at com.example.mapcheck.MainActivity$PlanetFragment.onCreateView(MainActivity.java:247)
   03-15 16:05:16.146: E/AndroidRuntime(25955):     at android.app.Fragment.performCreateView(Fragment.java:1695)
   03-15 16:05:16.146: E/AndroidRuntime(25955):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:885)
   03-15 16:05:16.146: E/AndroidRuntime(25955):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
  03-15 16:05:16.146: E/AndroidRuntime(25955):  at android.app.BackStackRecord.run(BackStackRecord.java:694)
     03-15 16:05:16.146: E/AndroidRuntime(25955):   at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
     03-15 16:05:16.146: E/AndroidRuntime(25955):   at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441)
    03-15 16:05:16.146: E/AndroidRuntime(25955):    at android.os.Handler.handleCallback(Handler.java:800)
    03-15 16:05:16.146: E/AndroidRuntime(25955):    at android.os.Handler.dispatchMessage(Handler.java:100)
    03-15 16:05:16.146: E/AndroidRuntime(25955):    at android.os.Looper.loop(Looper.java:194)
   03-15 16:05:16.146: E/AndroidRuntime(25955):     at android.app.ActivityThread.main(ActivityThread.java:5371)
   03-15 16:05:16.146: E/AndroidRuntime(25955):     at java.lang.reflect.Method.invokeNative(Native Method)
   03-15 16:05:16.146: E/AndroidRuntime(25955):     at java.lang.reflect.Method.invoke(Method.java:525)
   03-15 16:05:16.146: E/AndroidRuntime(25955):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
   03-15 16:05:16.146: E/AndroidRuntime(25955):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
   03-15 16:05:16.146: E/AndroidRuntime(25955):     at dalvik.system.NativeStart.main(Native Method)
   03-15 16:05:16.146: E/AndroidRuntime(25955): Caused by: java.lang.IllegalArgumentException: Binary XML file line #20: Duplicate id 0x7f050008, tag null, or parent id 0x0 with another fragment for com.google.android.gms.maps.SupportMapFragment
  03-15 16:05:16.146: E/AndroidRuntime(25955):  at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:285)
  03-15 16:05:16.146: E/AndroidRuntime(25955):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
  03-15 16:05:16.146: E/AndroidRuntime(25955):  ... 19 more

这是我的代码:

   public class MainActivity extends FragmentActivity {
 private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private ActionBarDrawerToggle mDrawerToggle;

    private CharSequence mDrawerTitle;
    private CharSequence mTitle;
    private String[] mPlanetTitles;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mTitle = mDrawerTitle = getTitle();
        mPlanetTitles = getResources().getStringArray(R.array.planets_array);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.left_drawer);

            // set a custom shadow that overlays the main content when the drawer opens
        mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
            // set up the drawer's list view with items and click listener
        mDrawerList.setAdapter(new ArrayAdapter<String>(this,
                R.layout.drawer_list_item, mPlanetTitles));
        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

        // enable ActionBar app icon to behave as action to toggle nav drawer
        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);

        // ActionBarDrawerToggle ties together the the proper interactions
        // between the sliding drawer and the action bar app icon
        mDrawerToggle = new ActionBarDrawerToggle(
                this,                  /* host Activity */
                mDrawerLayout,         /* DrawerLayout object */
                R.drawable.ic_drawer,  /* nav drawer image to replace 'Up' caret */
                R.string.drawer_open,  /* "open drawer" description for accessibility */
                R.string.drawer_close  /* "close drawer" description for accessibility */
                ) {
            public void onDrawerClosed(View view) {
                getActionBar().setTitle(mTitle);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }

            public void onDrawerOpened(View drawerView) {
                getActionBar().setTitle(mDrawerTitle);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }
        };
        mDrawerLayout.setDrawerListener(mDrawerToggle);

        if (savedInstanceState == null) {
            selectItem(0);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    /* Called whenever we call invalidateOptionsMenu() */
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        // If the nav drawer is open, hide action items related to the content view
        boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
        menu.findItem(R.id.action_websearch).setVisible(!drawerOpen);
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
         // The action bar home/up action should open or close the drawer.
         // ActionBarDrawerToggle will take care of this.
        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        // Handle action buttons
        switch(item.getItemId()) {
        case R.id.action_websearch:
            // create intent to perform web search for this planet
            Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
            intent.putExtra(SearchManager.QUERY, getActionBar().getTitle());
            // catch event that there's no activity to handle intent
            if (intent.resolveActivity(getPackageManager()) != null) {
                startActivity(intent);
            } else {
                Toast.makeText(this, R.string.app_not_available, Toast.LENGTH_LONG).show();
            }
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

    /* The click listner for ListView in the navigation drawer */
    private class DrawerItemClickListener implements ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            selectItem(position);
        }
    }

private void selectItem(int position) {

     Fragment mapfragment = new PlanetFragment();

        Bundle args = new Bundle();
        args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position);
        mapfragment.setArguments(args);

        mapfragment.getFragmentManager();

        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.content_frame, mapfragment).commit();

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

@Override
public void setTitle(CharSequence title) {
    mTitle = title;
    getActionBar().setTitle(mTitle);
}

/**
 * When using the ActionBarDrawerToggle, you must call it during
 * onPostCreate() and onConfigurationChanged()...
 */

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    // Sync the toggle state after onRestoreInstanceState has occurred.
    mDrawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    // Pass any configuration change to the drawer toggls
    mDrawerToggle.onConfigurationChanged(newConfig);
}


public class PlanetFragment extends Fragment {
    public static final String ARG_PLANET_NUMBER = "planet_number";
    GoogleMap mGoogleMap;

    public PlanetFragment() {
        // Empty constructor required for fragment subclasses
    }



        @Override
        public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
             //setUpMapIfNeeded();
             //setContentView(R.layout.basic_demo);

            }
           public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState) {
              setUpMapIfNeeded();
              View rootView = inflater.inflate(R.layout.fragment_planet, container, false);
              int i = getArguments().getInt(ARG_PLANET_NUMBER);
                switch (i) {
        case 1:
             if (mGoogleMap == null) {
                    SupportMapFragment fragment = ( SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);

                    // Getting Google Map
                    mGoogleMap = fragment.getMap();
                    mGoogleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
             }else
                    mGoogleMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));

            break;
          case 2:
             if (mGoogleMap == null) {
                    SupportMapFragment fragment = ( SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);

                    // Getting Google Map
                    mGoogleMap = fragment.getMap();
                    mGoogleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
             }else
                    mGoogleMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
             break;
        case 3:
             if (mGoogleMap == null) {
                    SupportMapFragment fragment = ( SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);

                    // Getting Google Map
                    mGoogleMap = fragment.getMap();
                    mGoogleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
             }else
                    mGoogleMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
             break;
        default:
            break;
        }
         setUpMapIfNeeded();
         return rootView;
          }

        @Override
        public void onResume() {
        super.onResume();
        setUpMapIfNeeded();
    }

        private void setUpMapIfNeeded() {
        // Do a null check to confirm that we have not already instantiated the map.
        if (mGoogleMap == null) {
            SupportMapFragment fragment = ( SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);

            // Getting Google Map
            mGoogleMap = fragment.getMap();

            // Check if we were successful in obtaining the map.
            if (mGoogleMap != null) {
                setUpMap();
            }
        }
    }

        private void setUpMap() {
        mGoogleMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
    }

        @Override
    public void onDestroyView() {
        super.onDestroyView();
        MapFragment f = (MapFragment) getFragmentManager()
                                             .findFragmentById(R.id.map);
        if (f != null) 
            getFragmentManager().beginTransaction().remove(f).commit();
    }    
  }
}

1 个答案:

答案 0 :(得分:1)

你的地图碎片第二次骑了..所以,你就像下面那样摧毁:

<fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:clickable="true"
         />

并覆盖方法OnDestroyView并将此代码放在OnDestroyView()

public void onDestroyView() 
 {
    super.onDestroyView(); 
    Fragment fragment = (getFragmentManager().findFragmentById(R.id.map));  
    FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
    ft.remove(fragment);
    ft.commit();
}

Refer