简体   繁体   English

Drawerlayout + Google Maps导致当机

[英]Drawerlayout + Google Maps results in crash

I am stuck with a drawer layout where I try to implement google maps when pressed on drawer item 1. It's something similar like what I found here: Google Maps V2 + Drawer Layout 我受制于抽屉布局,在按抽屉项目1时尝试在其中实现google地图。这类似于我在这里找到的内容: Google Maps V2 +抽屉布局

I tried to reproduce what he did, and it pushed me in the right direction. 我试图重现他的所作所为,这将我推向正确的方向。

UPDATED 更新

I have moved "setUpMapIfNeeded" and "SetUpMap" to the MapFragment and removed static from the class. 我已将“ setUpMapIfNeeded”和“ SetUpMap”移至MapFragment,并从该类中删除了静态对象。 Google maps works now, but crashes after trying to navigate to the fragment in the drawer navigation. Google地图现在可以使用,但是在尝试导航到抽屉式导航中的片段后会崩溃。

Does somebody know how to help me with this? 有人知道如何帮助我吗? I very much appreciate it! 我非常感谢!

The Basic stuff 基本的东西

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

My On Select 我的精选

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

My MapFragments (drawer item 1) 我的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;
    }
}

My information_fragment.xml 我的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." />

My maps_fragment.xml 我的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" />

Try modifying your code like add fragmentManager.executePendingTransactions(); 尝试修改您的代码,例如添加fragmentManager.executePendingTransactions(); after commit 提交后

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM