5

我试图合并两个 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天。

太感谢了!

4

3 回答 3

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>
于 2013-09-20T09:34:11.427 回答
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。这是文档如何获得一个。

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

于 2013-09-20T08:19:50.747 回答
1

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

1.-“android.view.InflateException:二进制 XML 文件第 18 行:膨胀类片段时出错”

2.-“java.lang.RuntimeException:未找到 API 密钥。检查它是否在 AndroidManifest.xml 的元素中”

“yume117”提供的解决方案解决了数字2,但我认为“膨胀类片段”的问题是由于BasicMapActivity的xml文件造成的。

您需要更改为以下内容,而不是“布局”中的“片段”:

<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 类。在这个 url 中是一个控制这个的例子(是西班牙语,但我认为代码是可以理解的): https://groups.google.com/forum/#!msg/desarrolladores-android/1cvqPm0EZZU/9srw_9feamUJ

于 2014-04-10T11:03:38.863 回答