致命错误 - 使用地图API V2显示Android地图

时间:2014-03-08 23:20:07

标签: java android google-maps

我是Android应用程序的新手,正在我的Android应用程序上显示Google地图。我正在使用Min SDK:8&目标SDK:18。我搜索所有可能的解决方案,但没有用。我得到了同样的致命错误,“无法找到主要”。我在我的工作空间中导入了google-play-services lib项目。

我发布的代码供参考。

AndroidManifest.xml   

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

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <uses-permission 
        android:name="android.permission.INTERNET"/>
    <permission
        android:name="com.example.lbs.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />
    <uses-permission android:name="com.example.lbs.permission.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission
         android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>


    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <uses-library 
            android:name="com.google.android.maps"/>
        <activity
            android:name="com.example.hellomap.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.gms.version"
           android:value="@integer/google_play_services_version" />
    </application>

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


</manifest>

活动-main.xml中

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
         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"
         tools:context=".MainActivity" >

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

</LinearLayout>

MainActivity.java

   package com.example.hellomap;

 import com.google.android.gms.maps.GoogleMap;
 //import com.google.android.maps.MapActivity;
 //import com.google.android.maps.MapView;
 import com.google.android.gms.maps.SupportMapFragment;
 import com.google.android.gms.maps.model.LatLng;
 import com.google.android.gms.maps.model.MarkerOptions;


 import android.os.Bundle;
 import android.support.v4.app.FragmentActivity;
 //import android.app.Activity;
 //import android.view.Menu;

public class MainActivity extends FragmentActivity {
    private GoogleMap mMap;

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

    }

    @Override
    protected void onResume() {
        super.onResume();
        setUpMapIfNeeded();
    }

    private void setUpMapIfNeeded() {
        if (mMap == null) {
            mMap = ((SupportMapFragment)
            getSupportFragmentManager().findFragmentById(R.id.map))
                    .getMap();
            if (mMap != null) {
                setUpMap();
            }
        }
    }

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


}

The Logcat window

    03-08 15:11:27.070: W/ApplicationPackageManager(32035): getCSCPackageItemText()
    03-08 15:11:27.501: D/AndroidRuntime(32035): Shutting down VM
    03-08 15:11:27.501: W/dalvikvm(32035): threadid=1: thread exiting with uncaught exception (group=0x4174ada0)
    03-08 15:11:27.501: E/AndroidRuntime(32035): FATAL EXCEPTION: main
    03-08 15:11:27.501: E/AndroidRuntime(32035): Process: com.example.hellomap, PID: 32035
    03-08 15:11:27.501: E/AndroidRuntime(32035): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hellomap/com.example.hellomap.MainActivity}: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
    03-08 15:11:27.501: E/AndroidRuntime(32035):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
    03-08 15:11:27.501: E/AndroidRuntime(32035):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
    03-08 15:11:27.501: E/AndroidRuntime(32035):    at android.app.ActivityThread.access$900(ActivityThread.java:161)
    03-08 15:11:27.501: E/AndroidRuntime(32035):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
    03-08 15:11:27.501: E/AndroidRuntime(32035):    at android.os.Handler.dispatchMessage(Handler.java:102)
    03-08 15:11:27.501: E/AndroidRuntime(32035):    at android.os.Looper.loop(Looper.java:157)
    03-08 15:11:27.501: E/AndroidRuntime(32035):    at android.app.ActivityThread.main(ActivityThread.java:5356)
    03-08 15:11:27.501: E/AndroidRuntime(32035):    at java.lang.reflect.Method.invokeNative(Native Method)
    03-08 15:11:27.501: E/AndroidRuntime(32035):    at java.lang.reflect.Method.invoke(Method.java:515)
    03-08 15:11:27.501: E/AndroidRuntime(32035):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
    03-08 15:11:27.501: E/AndroidRuntime(32035):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
    03-08 15:11:27.501: E/AndroidRuntime(32035):    at dalvik.system.NativeStart.main(Native Method)
    03-08 15:11:27.501: E/AndroidRuntime(32035): Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
    03-08 15:11:27.501: E/AndroidRuntime(32035):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:719)
    03-08 15:11:27.501: E/AndroidRuntime(32035):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
    03-08 15:11:27.501: E/AndroidRuntime(32035):    at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
    03-08 15:11:27.501: E/AndroidRuntime(32035):    at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
    03-08 15:11:27.501: E/AndroidRuntime(32035):    at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
    03-08 15:11:27.501: E/AndroidRuntime(32035):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:359)
    03-08 15:11:27.501: E/AndroidRuntime(32035):    at android.app.Activity.setContentView(Activity.java:2010)
    03-08 15:11:27.501: E/AndroidRuntime(32035):    at com.example.hellomap.MainActivity.onCreate(MainActivity.java:58)
    03-08 15:11:27.501: E/AndroidRuntime(32035):    at android.app.Activity.performCreate(Activity.java:5426)
    03-08 15:11:27.501: E/AndroidRuntime(32035):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
    03-08 15:11:27.501: E/AndroidRuntime(32035):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
    03-08 15:11:27.501: E/AndroidRuntime(32035):    ... 11 more
    03-08 15:11:27.501: E/AndroidRuntime(32035): 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
    03-08 15:11:27.501: E/AndroidRuntime(32035):    at maps.e.ay.a(Unknown Source)
    03-08 15:11:27.501: E/AndroidRuntime(32035):    at maps.e.ay.a(Unknown Source)
    03-08 15:11:27.501: E/AndroidRuntime(32035):    at maps.e.ay.a(Unknown Source)
    03-08 15:11:27.501: E/AndroidRuntime(32035):    at maps.e.ay.a(Unknown Source)
    03-08 15:11:27.501: E/AndroidRuntime(32035):    at maps.e.al.a(Unknown Source)
    03-08 15:11:27.501: E/AndroidRuntime(32035):    at maps.e.bg.a(Unknown Source)
    03-08 15:11:27.501: E/AndroidRuntime(32035):    at maps.e.bf.a(Unknown Source)
    03-08 15:11:27.501: E/AndroidRuntime(32035):    at eio.onTransact(SourceFile:107)
    03-08 15:11:27.501: E/AndroidRuntime(32035):    at android.os.Binder.transact(Binder.java:361)
    03-08 15:11:27.501: E/AndroidRuntime(32035):    at com.google.android.gms.maps.internal.IMapFragmentDelegate$a$a.onCreateView(Unknown Source)
    03-08 15:11:27.501: E/AndroidRuntime(32035):    at com.google.android.gms.maps.SupportMapFragment$a.onCreateView(Unknown Source)
    03-08 15:11:27.501: E/AndroidRuntime(32035):    at com.google.android.gms.dynamic.a$4.b(Unknown Source)
    03-08 15:11:27.501: E/AndroidRuntime(32035):    at com.google.android.gms.dynamic.a.a(Unknown Source)
    03-08 15:11:27.501: E/AndroidRuntime(32035):    at com.google.android.gms.dynamic.a.onCreateView(Unknown Source)
    03-08 15:11:27.501: E/AndroidRuntime(32035):    at com.google.android.gms.maps.SupportMapFragment.onCreateView(Unknown Source)
    03-08 15:11:27.501: E/AndroidRuntime(32035):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
    03-08 15:11:27.501: E/AndroidRuntime(32035):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:900)
    03-08 15:11:27.501: E/AndroidRuntime(32035):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1082)
    03-08 15:11:27.501: E/AndroidRuntime(32035):    at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1184)
    03-08 15:11:27.501: E/AndroidRuntime(32035):    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:291)
    03-08 15:11:27.501: E/AndroidRuntime(32035):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)
    03-08 15:11:27.501: E/AndroidRuntime(32035):    ... 21 more

应用错误

不幸的是app停止了,我试图清除我的应用程序管理器中的缓存和数据。

我想知道我哪里出错了,为什么会出现这个错误。如果有人能在这种情况下帮助我,那将是很棒的。

1 个答案:

答案 0 :(得分:1)

第一个问题出在您的清单文件中。你必须将下一段代码放在应用程序标签中(因为现在你已将它放在外面)。

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

这是我看到的第一件事。尝试它是否有效。如果我们不检查整个代码。

PD:永远不要发布您的API密钥。