Android - 将地图v2实施到导航抽屉

时间:2013-09-20 08:04:30

标签: java android google-maps android-fragments

我试图合并两个Android演示程序,但我无法正确使用它。

我尝试了几种方法,但我无法理解。你能帮我吗?

这是我的错误消息:

09-20 03:28:25.617: E/AndroidRuntime(20108): FATAL EXCEPTION: main
09-20 03:28:25.617: E/AndroidRuntime(20108): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.navigation/com.example.android.navigation.MainActivity}: android.view.InflateException: Binary XML file line #18: Error inflating class fragment
09-20 03:28:25.617: E/AndroidRuntime(20108):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at android.os.Looper.loop(Looper.java:137)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at android.app.ActivityThread.main(ActivityThread.java:5041)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at java.lang.reflect.Method.invokeNative(Native Method)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at java.lang.reflect.Method.invoke(Method.java:511)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at dalvik.system.NativeStart.main(Native Method)
09-20 03:28:25.617: E/AndroidRuntime(20108): Caused by: android.view.InflateException: Binary XML file line #18: Error inflating class fragment
09-20 03:28:25.617: E/AndroidRuntime(20108):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at com.example.android.navigation.BasicMapActivity.onCreateView(BasicMapActivity.java:57)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1470)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:925)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1102)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1458)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:551)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1164)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at android.app.Activity.performStart(Activity.java:5114)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2153)
09-20 03:28:25.617: E/AndroidRuntime(20108):    ... 11 more
09-20 03:28:25.617: E/AndroidRuntime(20108): Caused by: java.lang.RuntimeException: API key not found.  Check that <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="your API key"/> is in the <application> element of AndroidManifest.xml
09-20 03:28:25.617: E/AndroidRuntime(20108):    at maps.ag.bb.a(Unknown Source)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at maps.ag.bb.a(Unknown Source)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at maps.ag.an.a(Unknown Source)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at maps.ag.bi.a(Unknown Source)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at maps.ag.bh.a(Unknown Source)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at blf.onTransact(SourceFile:107)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at android.os.Binder.transact(Binder.java:310)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at com.google.android.gms.maps.internal.IMapFragmentDelegate$a$a.onCreateView(Unknown Source)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at com.google.android.gms.maps.SupportMapFragment$a.onCreateView(Unknown Source)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at com.google.android.gms.internal.bh$4.b(Unknown Source)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at com.google.android.gms.internal.bh.a(Unknown Source)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at com.google.android.gms.internal.bh.onCreateView(Unknown Source)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at com.google.android.gms.maps.SupportMapFragment.onCreateView(Unknown Source)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1470)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:898)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1080)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1182)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:280)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
09-20 03:28:25.617: E/AndroidRuntime(20108):    ... 23 more
09-20 03:30:16.829: D/dalvikvm(20329): GC_CONCURRENT freed 189K, 3% free 8774K/9016K, paused 1ms+0ms, total 3ms
09-20 03:30:17.005: D/AndroidRuntime(20329): Shutting down VM
09-20 03:30:17.009: W/dalvikvm(20329): threadid=1: thread exiting with uncaught exception (group=0xa617f908)
09-20 03:30:17.025: E/AndroidRuntime(20329): FATAL EXCEPTION: main
09-20 03:30:17.025: E/AndroidRuntime(20329): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.navigation/com.example.android.navigation.MainActivity}: android.view.InflateException: Binary XML file line #18: Error inflating class fragment
09-20 03:30:17.025: E/AndroidRuntime(20329):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at android.os.Looper.loop(Looper.java:137)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at android.app.ActivityThread.main(ActivityThread.java:5041)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at java.lang.reflect.Method.invokeNative(Native Method)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at java.lang.reflect.Method.invoke(Method.java:511)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at dalvik.system.NativeStart.main(Native Method)
09-20 03:30:17.025: E/AndroidRuntime(20329): Caused by: android.view.InflateException: Binary XML file line #18: Error inflating class fragment
09-20 03:30:17.025: E/AndroidRuntime(20329):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at com.example.android.navigation.BasicMapActivity.onCreateView(BasicMapActivity.java:57)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1470)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:925)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1102)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1458)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:551)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1164)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at android.app.Activity.performStart(Activity.java:5114)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2153)
09-20 03:30:17.025: E/AndroidRuntime(20329):    ... 11 more
09-20 03:30:17.025: E/AndroidRuntime(20329): Caused by: java.lang.RuntimeException: API key not found.  Check that <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="your API key"/> is in the <application> element of AndroidManifest.xml
09-20 03:30:17.025: E/AndroidRuntime(20329):    at maps.ag.bb.a(Unknown Source)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at maps.ag.bb.a(Unknown Source)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at maps.ag.an.a(Unknown Source)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at maps.ag.bi.a(Unknown Source)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at maps.ag.bh.a(Unknown Source)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at blf.onTransact(SourceFile:107)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at android.os.Binder.transact(Binder.java:310)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at com.google.android.gms.maps.internal.IMapFragmentDelegate$a$a.onCreateView(Unknown Source)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at com.google.android.gms.maps.SupportMapFragment$a.onCreateView(Unknown Source)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at com.google.android.gms.internal.bh$4.b(Unknown Source)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at com.google.android.gms.internal.bh.a(Unknown Source)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at com.google.android.gms.internal.bh.onCreateView(Unknown Source)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at com.google.android.gms.maps.SupportMapFragment.onCreateView(Unknown Source)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1470)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:898)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1080)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1182)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:280)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
09-20 03:30:17.025: E/AndroidRuntime(20329):    ... 23 more

这是我的源代码,我只更改了这个功能:

private void selectItem(int position) {
    // update the main content by replacing fragments

    Fragment mapfragment = new BasicMapActivity();

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

    mapfragment.getFragmentManager();
    FragmentManager fragmentManager = getSupportFragmentManager();
    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);
}

这是我的地图片段:

public class BasicMapActivity extends Fragment {
/**
 * Note that this may be null if the Google Play services APK is not available.
 */
private GoogleMap mMap;

@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.basic_demo, container, false);
     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 (mMap == null) {
        // Try to obtain the map from the SupportMapFragment.
        mMap = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
        // Check if we were successful in obtaining the map.
        if (mMap != null) {
            setUpMap();
        }
    }
}

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

BasicMapActivity的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.SupportMapFragment"/>

编辑:

这是主程序的XML:

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<!-- As the main content view, the view below consumes the entire
     space available using match_parent in both dimensions. -->
<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />


<!-- android:layout_gravity="start" tells DrawerLayout to treat
     this as a sliding drawer on the left side for left-to-right
     languages and on the right side for right-to-left languages.
     The drawer is given a fixed width in dp and extends the full height of
     the container. A solid background is used for contrast
     with the content view. -->
<ListView
    android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:background="#111"/>
</android.support.v4.widget.DrawerLayout>

只要我的清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.navigation"
android:versionCode="1"
android:versionName="1.0">

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

<permission
android:name="com.example.mapdemo.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.example.mapdemo.permission.MAPS_RECEIVE"/>
<!-- Copied from Google Maps Library/AndroidManifest.xml. -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<!-- External storage for caching. -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<!-- My Location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<!-- Maps API needs OpenGL ES 2.0. -->
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<meta-data android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyDJbPX63ZJni6sbzNxaFvFUCnYLb7-9o28"/>

<application
    android:label="@string/app_name"
    android:icon="@drawable/ic_launcher"
    android:theme="@android:style/Theme.Holo.Light.DarkActionBar"
    android:allowBackup="true">


    <activity
        android:name=".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>

</application>

</manifest>

有人能给我一个提示吗?我在这里被挤了2-3天。

非常感谢你!

3 个答案:

答案 0 :(得分:4)

您的发布很简单

API KEY必须位于Application标记内,您也应将其放在最后

示例:

<application
    android:label="@string/app_name"
    android:icon="@drawable/ic_launcher"
    android:theme="@android:style/Theme.Holo.Light.DarkActionBar"
    android:allowBackup="true">

   <activity
        android:name=".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="AIzaSyDJbPX63ZJni6sbzNxaFvFUCnYLb7-9o28"/>

</application>

答案 1 :(得分:1)

Caused by: java.lang.RuntimeException: API key not found.  Check that <meta-data  
android:name="com.google.android.maps.v2.API_KEY" android:value="your API key"/>  
is in the <application> element of AndroidManifest.xml

获取有效的API密钥。除此之外,您无法在应用程序中运行地图V2。这是doc如何获得一个。

第18行的BasicMapActivity的xml布局中发生了另一个错误。请在您的问题中发布该XML。

答案 2 :(得分:1)

在错误消息中,您遇到两个问题:

  

1.-&#34; android.view.InflateException:二进制XML文件行#18:错误扩充类片段&#34;

     

2.-&#34; java.lang.RuntimeException:找不到API密钥。检查是否在元素中   的AndroidManifest.xml&#34;

&#34; yume117&#34;提供的解决方案解决了数字2,但我认为这个问题&#34;膨胀类片段&#34;是由于BasicMapActivity的xml文件。

而不是&#34;片段&#34;在你的&#34;布局&#34;你需要改成这样的东西:

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

此外,您还必须更改java类。在这个网址是一个控制它的例子(西班牙语,但我认为代码是可理解的): https://groups.google.com/forum/#!msg/desarrolladores-android/1cvqPm0EZZU/9srw_9feamUJ