java – Google Maps API V2崩溃 – 致命异常

我已经尝试过在我的应用程序中实现Google Maps的教程.
http://www.androidhive.info/2013/08/android-working-with-google-maps-v2/

我会尝试这个例子,但它在这个级别崩溃:setContentView(R.layout.activity_main);

我正确地遵循tuto,通常没有API Key问题.

编辑:这是我所有已解决代码错误显示评论

MANIFEST.XML:

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

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

    <uses-permission android:name="com.example.essaimap.permission.MAPS_RECEIVE" />

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="16" />

    <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" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <!-- required to show current location -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <!-- required OpenGL ES 2.0. for Maps V2 -->
    <uses-feature
        android:glEsversion="0x00020000"
        android:required="true" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <!-- IMPORTANT ! DO NOT FORGET THIS Meta-DATA !!! -->
        <Meta-data 
            android:name="com.google.android.gms.version" 
            android:value="@integer/google_play_services_version" />
        <!-- IMPORTANT ! DO NOT PUT THIS Meta IN ACTIVITY ELEMENT-->
        <Meta-data 
                android:name="com.google.android.maps.v2.API_KEY"
                android:value="AIzaSyDWCZGT4OIUAZEOUIYAZEOIYAZOEIYAZOEIY6WRvbh6BnHSAipgg" />

        <activity
            android:name="com.example.essaimap.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <!-- I put my Meta here, that is why my app was crashing 
            <Meta-data 
                android:name="com.google.android.maps.v2.API_KEY"
                android:value="AIzaSyDWCZGT4n2HLZ5bUCM6WRvbh6BnHSAipgg" /> -->            
        </activity>
    </application>
</manifest>

ACTIVITY_MAIN.XML:

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

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

</RelativeLayout>

MAINACTIVITY.JAVA:

    public class MainActivity extends FragmentActivity // Not ACTIVITY
{
    // Google Map
    private GoogleMap googleMap;

    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        try 
        {
            // Loading map
            initilizeMap();
        } 
        catch (Exception e) 
        {
            e.printstacktrace();
        }
    }

    /**
     * function to load map. If map is not created it will create it for you
     * */
    private void initilizeMap() 
    {
        if (googleMap == null) 
        {
            googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

            // check if map is created successfully or not
            if (googleMap == null)
            {
                Toast.makeText(getApplicationContext(),"Sorry! unable to create maps",Toast.LENGTH_SHORT).show();
            }
        }
    }

    protected void onResume() 
    {
        super.onResume();
        initilizeMap();
    }
}

logcat的

感谢@Piyush Gupta,@ fasteque,@ WarrenFaith,@ Sankar V和@ViragBrahme解决了我的问题.

Tofuw

解决方法:

您在清单文件中缺少新的强制元标记,它是在logcat中编写的.

在清单文件的应用程序部分中添加以下行:

<Meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />

此外,您的清单中还有另一个问题,您已在活动中复制了元标记com.google.android.maps.v2.API_KEY,同时也应在应用程序部分进行设置.

相关文章

php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念