Android的抽屉例子,如何实现谷歌地图碎片抽屉、如何实现、碎片、例子

2023-09-06 08:35:29 作者:愛裏行乞

其实我想实现在Android抽屉例如谷歌地图,基本上我想通过选择一个菜单项来显示地图。所提供的所有例子都使用了所有地图的FragmentActivity,我安排延长片段类(由出票人使用),创建适合于地图的类。不幸的是上推出的应用程序崩溃:

Actually I am trying to implement Google Map on Android Drawer example, basically I would like to show a map by selecting the first menu item. All the provided examples are using the FragmentActivity for all maps, I arranged to extend the Fragment Class (used by the drawer), creating a class suitable for Map. Unfortunately the App crash on launch:

主要活动

public class HLMainActivity extends ActionBarActivity implements
    NavigationDrawerFragment.NavigationDrawerCallbacks {

/**
 * Fragment managing the behaviors, interactions and presentation of the
 * navigation drawer.
 */
private NavigationDrawerFragment mNavigationDrawerFragment;

/**
 * Used to store the last screen title. For use in
 * {@link #restoreActionBar()}.
 */
private CharSequence mTitle;

public static FragmentManager fragmentManager;

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

    // initialising the object of the FragmentManager. Here I'm passing getSupportFragmentManager(). You can pass getFragmentManager() if you are coding for Android 3.0 or above.
    fragmentManager = getSupportFragmentManager();

    mNavigationDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager()
            .findFragmentById(R.id.navigation_drawer);
    mTitle = getTitle();

    // Set up the drawer.
    mNavigationDrawerFragment.setUp(R.id.navigation_drawer,
            (DrawerLayout) findViewById(R.id.drawer_layout));
}

@Override
public void onNavigationDrawerItemSelected(int position) {
    // update the main content by replacing fragments
    fragmentManager = getSupportFragmentManager();

    HLMainMapFragment fragment = new HLMainMapFragment();
    getSupportFragmentManager().beginTransaction()
            .replace(R.id.container, fragment).commit();

}

public void onSectionAttached(int number) {
    switch (number) {
    case 1:
        mTitle = getString(R.string.title_section1);
        break;
    case 2:
        mTitle = getString(R.string.title_section2);
        break;
    case 3:
        mTitle = getString(R.string.title_section3);
        break;
    }
}

public void restoreActionBar() {
    ActionBar actionBar = getSupportActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
    actionBar.setDisplayShowTitleEnabled(true);
    actionBar.setTitle(mTitle);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    if (!mNavigationDrawerFragment.isDrawerOpen()) {
        // Only show items in the action bar relevant to this screen
        // if the drawer is not showing. Otherwise, let the drawer
        // decide what to show in the action bar.
        getMenuInflater().inflate(R.menu.hlmain, menu);
        restoreActionBar();
        return true;
    }
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}   

}

地图碎片

public class HLMainMapFragment extends Fragment {
private static View view;
/**
 * Note that this may be null if the Google Play services APK is not
 * available.
 */

private static GoogleMap mMap;
private static Double latitude, longitude;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    if (container == null) {
        return null;
    }

    view = (RelativeLayout) inflater.inflate(R.layout.location_fragment,
            container, false);
    // Passing harcoded values for latitude & longitude. Please change as
    // per your need. This is just used to drop a Marker on the Map
    latitude = 26.78;
    longitude = 72.56;

    setUpMapIfNeeded(); // For setting up the MapFragment

    return view;
}

/***** Sets up the map if it is possible to do so *****/
public static 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) HLMainActivity.fragmentManager
                .findFragmentById(R.id.map)).getMap();
        // Check if we were successful in obtaining the map.
        if (mMap != null)
            setUpMap();
    }
}

/**
 * This is where we can add markers or lines, add listeners or move the
 * camera.
 * <p>
 * This should only be called once and when we are sure that {@link #mMap}
 * is not null.
 */
private static void setUpMap() {
    // For showing a move to my loction button
    mMap.setMyLocationEnabled(true);
    // For dropping a marker at a point on the Map
    mMap.addMarker(new MarkerOptions()
            .position(new LatLng(latitude, longitude)).title("My Home")
            .snippet("Home Address"));
    // For zooming automatically to the Dropped PIN Location
    mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(
            latitude, longitude), 12.0f));
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    if (mMap != null)
        setUpMap();

    if (mMap == null) {
        // Try to obtain the map from the SupportMapFragment.
        mMap = ((SupportMapFragment) HLMainActivity.fragmentManager
                .findFragmentById(R.id.map)).getMap();
        // Check if we were successful in obtaining the map.
        if (mMap != null)
            setUpMap();
    }
}

/****
 * The mapfragment's id must be removed from the FragmentManager or else if
 * the same it is passed on the next time then app will crash
 ****/
@Override
public void onDestroyView() {
    super.onDestroyView();
    if (mMap != null) {
        HLMainActivity.fragmentManager
                .beginTransaction()
                .remove(HLMainActivity.fragmentManager
                        .findFragmentById(R.id.map)).commit();
        mMap = null;
    }
}
}

位置片段XML

<?xml version="1.0" encoding="utf-8"?>
<MapFragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment" />

LogCat中

    04-14 16:43:36.984: E/AndroidRuntime(1159): FATAL EXCEPTION: main
    04-14 16:43:36.984: E/AndroidRuntime(1159): Process: com.studiohangloose.iMobility, PID: 1159
    04-14 16:43:36.984: E/AndroidRuntime(1159): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.studiohangloose.iMobility/com.studiohangloose.iMobility.HLMainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class MapFragment
    04-14 16:43:36.984: E/AndroidRuntime(1159):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
    04-14 16:43:36.984: E/AndroidRuntime(1159):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
    04-14 16:43:36.984: E/AndroidRuntime(1159):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
    04-14 16:43:36.984: E/AndroidRuntime(1159):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
    04-14 16:43:36.984: E/AndroidRuntime(1159):     at android.os.Handler.dispatchMessage(Handler.java:102)
    04-14 16:43:36.984: E/AndroidRuntime(1159):     at android.os.Looper.loop(Looper.java:136)
    04-14 16:43:36.984: E/AndroidRuntime(1159):     at android.app.ActivityThread.main(ActivityThread.java:5017)
    04-14 16:43:36.984: E/AndroidRuntime(1159):     at java.lang.reflect.Method.invokeNative(Native Method)
    04-14 16:43:36.984: E/AndroidRuntime(1159):     at java.lang.reflect.Method.invoke(Method.java:515)
    04-14 16:43:36.984: E/AndroidRuntime(1159):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
    04-14 16:43:36.984: E/AndroidRuntime(1159):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
    04-14 16:43:36.984: E/AndroidRuntime(1159):     at dalvik.system.NativeStart.main(Native Method)
    04-14 16:43:36.984: E/AndroidRuntime(1159): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class MapFragment
    04-14 16:43:36.984: E/AndroidRuntime(1159):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:707)
    04-14 16:43:36.984: E/AndroidRuntime(1159):     at android.view.LayoutInflater.inflate(LayoutInflater.java:469)
    04-14 16:43:36.984: E/AndroidRuntime(1159):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
    04-14 16:43:36.984: E/AndroidRuntime(1159):     at com.studiohangloose.iMobility.HLMainMapFragment.onCreateView(HLMainMapFragment.java:33)
    04-14 16:43:36.984: E/AndroidRuntime(1159):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
    04-14 16:43:36.984: E/AndroidRuntime(1159):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
    04-14 16:43:36.984: E/AndroidRuntime(1159):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
    04-14 16:43:36.984: E/AndroidRuntime(1159):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
    04-14 16:43:36.984: E/AndroidRuntime(1159):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
    04-14 16:43:36.984: E/AndroidRuntime(1159):     at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)
    04-14 16:43:36.984: E/AndroidRuntime(1159):     at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
    04-14 16:43:36.984: E/AndroidRuntime(1159):     at android.app.Activity.performStart(Activity.java:5241)
    04-14 16:43:36.984: E/AndroidRuntime(1159):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2168)
    04-14 16:43:36.984: E/AndroidRuntime(1159):     ... 11 more
    04-14 16:43:36.984: E/AndroidRuntime(1159): Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.MapFragment" on path: DexPathList[[zip file "/data/app/com.studiohangloose.iMobility-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.studiohangloose.iMobility-2, /system/lib]]
    04-14 16:43:36.984: E/AndroidRuntime(1159):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
    04-14 16:43:36.984: E/AndroidRuntime(1159):     at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
    04-14 16:43:36.984: E/AndroidRuntime(1159):     at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
    04-14 16:43:36.984: E/AndroidRuntime(1159):     at android.view.LayoutInflater.createView(LayoutInflater.java:559)
    04-14 16:43:36.984: E/AndroidRuntime(1159):     at android.view.LayoutInflater.onCreateView(LayoutInflater.java:652)
    04-14 16:43:36.984: E/AndroidRuntime(1159):     at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
    04-14 16:43:36.984: E/AndroidRuntime(1159):     at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
    04-14 16:43:36.984: E/AndroidRuntime(1159):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
    04-14 16:43:36.984: E/AndroidRuntime(1159):     ... 23 more

任何建议!?或者在该溶液链路...

Any suggestion!? Or maybe a link to the solution...

推荐答案

如果你想在片段中使用的地图你应该使用类的MapView

If you want to use maps within the fragment class you should use MapView.

MapView mapView;

GoogleMap googleMap;

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    {
        View view = inflater.inflate(R.layout.your_layout_name, container, false);

        mapView = (MapView) view.findViewById(R.id.map);

            mapView.onCreate(savedInstanceState);

            if(mapView!=null)
            {
                googleMap = mapView.getMap();

                googleMap.getUiSettings().setMyLocationButtonEnabled(false);

                googleMap.setMyLocationEnabled(true);

                googleMap.getUiSettings().setZoomControlsEnabled(true);
            }

        return view;

    }

  @Override
    public void onResume() 
    {
        mapView.onResume();

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

        mapView.onDestroy();
    }
    @Override
    public void onLowMemory() 
    {
        super.onLowMemory();

        mapView.onLowMemory();
    }

在你的 XML 文件贴$ C $以下

In your xml file paste code below

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <com.google.android.gms.maps.MapView
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

试试这个,你会看到一个好的结果...

try this you will see a good result...