地图片段与导航抽屉

时间:2014-01-28 21:06:48

标签: android google-maps fragment navigation-drawer

  

我试图让地图碎片首先出现但没有任何效果   当我试图改变位置力关闭显示然后我回到发射   直接活动而不按任何东西

     
    

这里的错误:

  
        01-28 21:02:44.289: E/AndroidRuntime(27684): FATAL EXCEPTION: main
    01-28 21:02:44.289: E/AndroidRuntime(27684): android.view.InflateException: Binary XML file line #1: Error inflating class fragment
    01-28 21:02:44.289: E/AndroidRuntime(27684):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
    01-28 21:02:44.289: E/AndroidRuntime(27684):    at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
    01-28 21:02:44.289: E/AndroidRuntime(27684):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
    01-28 21:02:44.289: E/AndroidRuntime(27684):    at com.pickmeup.maps.MapActivity.onCreateView(MapActivity.java:33)
    01-28 21:02:44.289: E/AndroidRuntime(27684):    at android.app.Fragment.performCreateView(Fragment.java:1785)
    01-28 21:02:44.289: E/AndroidRuntime(27684):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:887)
    01-28 21:02:44.289: E/AndroidRuntime(27684):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1037)
    01-28 21:02:44.289: E/AndroidRuntime(27684):    at android.app.Activity.onCreateView(Activity.java:4797)
    01-28 21:02:44.289: E/AndroidRuntime(27684):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
    01-28 21:02:44.289: E/AndroidRuntime(27684):    at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
    01-28 21:02:44.289: E/AndroidRuntime(27684):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
    01-28 21:02:44.289: E/AndroidRuntime(27684):    at com.pickmeup.maps.MapActivity.onCreateView(MapActivity.java:33)
    01-28 21:02:44.289: E/AndroidRuntime(27684):    at android.app.Fragment.performCreateView(Fragment.java:1785)
    01-28 21:02:44.289: E/AndroidRuntime(27684):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:887)
    01-28 21:02:44.289: E/AndroidRuntime(27684):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1059)
    01-28 21:02:44.289: E/AndroidRuntime(27684):    at android.app.BackStackRecord.run(BackStackRecord.java:682)
    01-28 21:02:44.289: E/AndroidRuntime(27684):    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1437)
    01-28 21:02:44.289: E/AndroidRuntime(27684):    at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
    01-28 21:02:44.289: E/AndroidRuntime(27684):    at android.os.Handler.handleCallback(Handler.java:725)
    01-28 21:02:44.289: E/AndroidRuntime(27684):    at android.os.Handler.dispatchMessage(Handler.java:92)
    01-28 21:02:44.289: E/AndroidRuntime(27684):    at android.os.Looper.loop(Looper.java:158)
    01-28 21:02:44.289: E/AndroidRuntime(27684):    at android.app.ActivityThread.main(ActivityThread.java:5751)
    01-28 21:02:44.289: E/AndroidRuntime(27684):    at java.lang.reflect.Method.invokeNative(Native Method)
    01-28 21:02:44.289: E/AndroidRuntime(27684):    at java.lang.reflect.Method.invoke(Method.java:511)
    01-28 21:02:44.289: E/AndroidRuntime(27684):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1083)
    01-28 21:02:44.289: E/AndroidRuntime(27684):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850)
    01-28 21:02:44.289: E/AndroidRuntime(27684):    at dalvik.system.NativeStart.main(Native Method)
    01-28 21:02:44.289: E/AndroidRuntime(27684): Caused by: java.lang.IllegalArgumentException: Binary XML file line #1: Duplicate id 0x7f05002a, tag null, or parent id 0x7f05001a with another fragment for com.google.android.gms.maps.MapFragment
    01-28 21:02:44.289: E/AndroidRuntime(27684):    at android.app.Activity.onCreateView(Activity.java:4783)
    01-28 21:02:44.289: E/AndroidRuntime(27684):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
    01-28 21:02:44.289: E/AndroidRuntime(27684):    ... 26 more
  
    

MapActivity.java

  
            public class MapActivity extends Fragment {
        private GoogleMap mMap;
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {

            View rootView = inflater.inflate(R.layout.map_fragment, container, false);
            setUpMapIfNeeded();
            return rootView;
        }

        private void setUpMapIfNeeded() {

            if (mMap == null) {

                mMap = ((MapFragment) getFragmentManager().findFragmentById(
                        R.id.map)).getMap();

                if (mMap != null) {
                    setUpMap();
                }
            }
        }

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

map_fragment.xml

  
    <fragment 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" />
  
    

这里是selectitem

  
            android.app.Fragment mapfragment = new MapActivity();

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

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

mainfest.xml

  
   <?xml version="1.0" encoding="utf-8"?>
  <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.navigationdrawerexample"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:maxSdkVersion="19"
    android:minSdkVersion="14"
    android:targetSdkVersion="17" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.pickmeup.mapstrial.permission.MAPS_RECEIVE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />

<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />

<permission
    android:name="com.pickmeup.maps.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" >
</permission>

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.Holo.Light.DarkActionBar" >
    <activity
        android:name="com.pickmeup.gui.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="AIzaSyDRCTjlHnNX96nzN9PA69SYt7ZlqWGpsZ4" />
</application>

提前致谢

1 个答案:

答案 0 :(得分:0)

请参阅以下要点

您确定在map_fragment.xml

中添加了第一行吗?
<?xml version="1.0" encoding="utf-8"?>
        <fragment 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" />

什么是com.pickmeup.maps.permission?当您的包裹名称为com.example.android.navigationdrawerexample

<permission
    android:name="com.example.android.navigationdrawerexample.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" >
</permission>

</application>代码

之后添加
<uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />