2014-10-08 83 views
0

我堅持抽屜佈局,我嘗試實施谷歌地圖上的抽屜項按下時1 它類似於像什麼,我在這裏找到:Google Maps V2 + Drawer LayoutDrawerlayout +谷歌地圖會導致死機

我試圖重現他做了什麼,並且把我推向了正確的方向。

更新

我提出 「setUpMapIfNeeded」 和 「SetUpMap」 到MapFragment並移除了類的靜態。 Google地圖現在可以工作,但在嘗試導航到抽屜導航中的片段後崩潰。

有人知道如何幫助我嗎?我非常感謝!

基本的東西

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

    mTitle = mDrawerTitle = getTitle(); 
    mMenuTitles = getResources().getStringArray(R.array.menu_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, mMenuTitles)); 
    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); 
    } 
} 

我的選擇

Fragment mapFragment; 
Fragment fragment; 
private void selectItem(int position) { 
    FragmentManager fragmentManager = getFragmentManager(); 

    if (position == 0) { 
     if (mapFragment == null) { 
      mapFragment = new MapFragment(); 
     } 
     Bundle args = new Bundle(); 

     args.putInt(MapFragment.ARG_NUMBER, position); 
     mapFragment.setArguments(args); 

     fragmentManager.beginTransaction().replace(R.id.content_frame, mapFragment).commit(); 

    } else if (position == 1) { 
     if (fragment == null) { 
      fragment = new InformationFragment(); 
     } 
     Bundle args = new Bundle(); 

     args.putInt(InformationFragment.ARG_NUMBER, position); 
     fragment.setArguments(args); 

     fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit(); 
    } 

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

我MapFragments(抽屜第1項)

public class MapFragment extends Fragment { 

    public static final String ARG_NUMBER = "arg_number"; 

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

    private void setUpMapIfNeeded() { 
     // Do a null check to confirm that we have not already instantiated the map. 
     if (mMap == null) { 
      // Try to obtain the map from the SupportMapFragment. 
      mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)) 
        .getMap(); 
      // Check if we were successful in obtaining the map. 
      if (mMap != null) { 
       setUpMap(); 
      } 
     } 
    } 

    private void setUpMap() { 
     LatLng museum = new LatLng(52.360011, 4.885216); 

     mMap.setBuildingsEnabled(true); 
     mMap.setMyLocationEnabled(false); 

     // Construct a CameraPosition focusing on Mountain View and animate the camera to that position. 
     CameraPosition cameraPosition = new CameraPosition.Builder() 
       .target(museum)    // Sets the center of the map to Mountain View 
       .zoom(19)     // Sets the zoom 
       .bearing(90)    // Sets the orientation of the camera to east 
       .tilt(30)     // Sets the tilt of the camera to 30 degrees 
       .build();     // Creates a CameraPosition from the builder 

     mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); 

     CameraUpdate upd = CameraUpdateFactory.newLatLngZoom(new LatLng(52.360011, 4.885216), 18); 
     mMap.moveCamera(upd); 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 

     View rootView = inflater.inflate(R.layout.maps_fragment, container, false); 
     int i = getArguments().getInt(ARG_NUMBER); 

     String title = getResources().getStringArray(R.array.menu_array)[i]; 
     setUpMapIfNeeded(); 
     getActivity().setTitle(title); 
     return rootView; 
    } 
} 

我information_fragment.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<TextView 
    android:id="@+id/textViewTitle" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:paddingLeft="20dp" 
    android:paddingTop="20dp" 
    android:text="Title 2" 
    android:textStyle="bold" /> 

    <TextView 
     android:id="@+id/textViewDescription" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:paddingLeft="20dp" 
     android:paddingTop="10dp" 
     android:paddingRight="20dp" 
     android:text="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry&apos;s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum." /> 

我maps_fragment.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<fragment 
    android:id="@+id/map" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    class="com.google.android.gms.maps.SupportMapFragment" 
    android:layout_weight="0.29" /> 

+0

添加崩潰日誌 – 2014-10-08 15:23:30

+0

我已經加入了崩潰日誌 – Shin 2014-10-09 08:11:10

+0

烏爾日誌說這是有重複的ID。 – 2014-10-09 08:23:01

回答

0

嘗試修改喜歡你的代碼add fragmentManager.executePendingTransactions();提交後

Fragment mapFragment; 
Fragment fragment; 
private void selectItem(int position) { 
    FragmentManager fragmentManager = getFragmentManager(); 

    if (position == 0) { 
     if (mapFragment == null) { 
      mapFragment = new MapFragment(); 
     } 
     Bundle args = new Bundle(); 

     args.putInt(MapFragment.ARG_NUMBER, position); 
     mapFragment.setArguments(args); 

     fragmentManager.beginTransaction().replace(R.id.content_frame, mapFragment).commit(); 

    } else if (position == 1) { 
     if (fragment == null) { 
      fragment = new InformationFragment(); 
     } 
     Bundle args = new Bundle(); 

     args.putInt(InformationFragment.ARG_NUMBER, position); 
     fragment.setArguments(args); 

     fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit(); 
    } 

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