谷歌地图实现java.lang.RuntimeException:无法启动活动

时间:2014-05-31 11:50:36

标签: java android google-maps-android-api-2

我继续提出这个例子

05-31 07:42:01.640: E/AndroidRuntime(4133): FATAL EXCEPTION: main
05-31 07:42:01.640: E/AndroidRuntime(4133): Process: com.start.littldeamone, PID: 4133
05-31 07:42:01.640: E/AndroidRuntime(4133): java.lang.RuntimeException: Unable to start  activity ComponentInfo{com.start.littldeamone/com.start.littldeamone.MainActivity}:   android.view.InflateException: Binary XML file line #2: Error inflating class fragment
05-31 07:42:01.640: E/AndroidRuntime(4133):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-31 07:42:01.640: E/AndroidRuntime(4133):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-31 07:42:01.640: E/AndroidRuntime(4133):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-31 07:42:01.640: E/AndroidRuntime(4133):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-31 07:42:01.640: E/AndroidRuntime(4133):     at android.os.Handler.dispatchMessage(Handler.java:102)
05-31 07:42:01.640: E/AndroidRuntime(4133):     at android.os.Looper.loop(Looper.java:136)
05-31 07:42:01.640: E/AndroidRuntime(4133):     at android.app.ActivityThread.main(ActivityThread.java:5017)
05-31 07:42:01.640: E/AndroidRuntime(4133):     at java.lang.reflect.Method.invokeNative(Native Method)
05-31 07:42:01.640: E/AndroidRuntime(4133):     at java.lang.reflect.Method.invoke(Method.java:515)
05-31 07:42:01.640: E/AndroidRuntime(4133):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-31 07:42:01.640: E/AndroidRuntime(4133):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-31 07:42:01.640: E/AndroidRuntime(4133):     at dalvik.system.NativeStart.main(Native Method)
05-31 07:42:01.640: E/AndroidRuntime(4133): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
05-31 07:42:01.640: E/AndroidRuntime(4133):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
05-31 07:42:01.640: E/AndroidRuntime(4133):     at android.view.LayoutInflater.inflate(LayoutInflater.java:469)
05-31 07:42:01.640: E/AndroidRuntime(4133):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
05-31 07:42:01.640: E/AndroidRuntime(4133):     at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
05-31 07:42:01.640: E/AndroidRuntime(4133):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
05-31 07:42:01.640: E/AndroidRuntime(4133):     at android.app.Activity.setContentView(Activity.java:1929)
05-31 07:42:01.640: E/AndroidRuntime(4133):     at com.start.littldeamone.MainActivity.onCreate(MainActivity.java:10)
05-31 07:42:01.640: E/AndroidRuntime(4133):     at android.app.Activity.performCreate(Activity.java:5231)
05-31 07:42:01.640: E/AndroidRuntime(4133):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-31 07:42:01.640: E/AndroidRuntime(4133):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-31 07:42:01.640: E/AndroidRuntime(4133):     ... 11 more
05-31 07:42:01.640: E/AndroidRuntime(4133): Caused by: android.app.Fragment$InstantiationException: Trying to instantiate a class com.google.android.gms.maps.SupportMapFragment that is not a Fragment
05-31 07:42:01.640: E/AndroidRuntime(4133):     at android.app.Fragment.instantiate(Fragment.java:585)
05-31 07:42:01.640: E/AndroidRuntime(4133):     at android.app.Fragment.instantiate(Fragment.java:561)
05-31 07:42:01.640: E/AndroidRuntime(4133):     at android.app.Activity.onCreateView(Activity.java:4778)
05-31 07:42:01.640: E/AndroidRuntime(4133):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
05-31 07:42:01.640: E/AndroidRuntime(4133):     ... 20 more
05-31 07:42:01.640: E/AndroidRuntime(4133): Caused by: java.lang.ClassCastException
05-31 07:42:01.640: E/AndroidRuntime(4133):     ... 24 more

我已经完成所有堆栈溢出solotion并且没有找到问题 从mapactivity到frgment活动等等...没有任何效果 这是我的代码 mainactivity.java

public class MainActivity extends Activity {

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

活动主要xml          

清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.start.littldeamone"
android:versionCode="1"
android:versionName="1.0" >
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyA58hdExVacurnuB235ojNOJVsF6dH8MMg"/>
<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET"/>
<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"/>
<!-- The following two permissions are not required to use
 Google Maps Android API v2, but are recommended. -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

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

请帮助!!

1 个答案:

答案 0 :(得分:2)

首先纠正此问题,您需要移动<meta-data><application>标记下的manifest.xml个标记。

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

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

</application>

此处还有你的 minsdk =&#34; 8&#34; 所以你需要改变这个

public class MainActivity extends Activity 

 public class MainActivity extends FragmentActivity 

你的logcat也清楚地说了

05-31 07:42:01.640: E/AndroidRuntime(4133): Caused by: android.app.Fragment$InstantiationException: Trying to instantiate a class com.google.android.gms.maps.SupportMapFragment that is not a Fragment

这意味着您需要使用

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

有关更多信息,请转到Getting Started Google Maps API V2