问题描述
我试图让使用谷歌地图的样本项目,但我不能。请帮助我!
测试设备:安卓4.0.4
错误消息:
12-29 23:45:32.605:E / AndroidRuntime(9437):致命异常:主要
12-29 23:45:32.605:E / AndroidRuntime(9437):
java.lang.RuntimeException的:无法启动的活动
ComponentInfo {com.example.test_googlemap / com.example.test_googlemap.MainActivity}:
android.view.InflateException:二进制XML文件中的行#2:错误
充气类片段12-29 23:45:32.605:E / AndroidRuntime(9437):
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1968)
12-29 23:45:32.605:E / AndroidRuntime(9437):在
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1993)
12-29 23:45:32.605:E / AndroidRuntime(9437):在
android.app.ActivityThread.access $ 600(ActivityThread.java:127)12-29
23:45:32.605:E / AndroidRuntime(9437):在
android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1159)
12-29 23:45:32.605:E / AndroidRuntime(9437):在
android.os.Handler.dispatchMessage(Handler.java:99)12-29
23:45:32.605:E / AndroidRuntime(9437):在
android.os.Looper.loop(Looper.java:137)12-29 23:45:32.605:
E / AndroidRuntime(9437):在
android.app.ActivityThread.main(ActivityThread.java:4507)12-29
23:45:32.605:E / AndroidRuntime(9437):在
java.lang.reflect.Method.invokeNative(本机方法)12-29
23:45:32.605:E / AndroidRuntime(9437):在
java.lang.reflect.Method.invoke(Method.java:511)12-29 23:45:32.605:
E / AndroidRuntime(9437):在
com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:790)
12-29 23:45:32.605:E / AndroidRuntime(9437):在
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)12-29
23:45:32.605:E / AndroidRuntime(9437):在
dalvik.system.NativeStart.main(本机方法)12-29 23:45:32.605:
E / AndroidRuntime(9437):android.view.InflateException:产生的原因:
二进制XML文件行#2:错误充气类片段12-29
23:45:32.605:E / AndroidRuntime(9437):在
android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
12-29 23:45:32.605:E / AndroidRuntime(9437):在
android.view.LayoutInflater.inflate(LayoutInflater.java:466)12-29
23:45:32.605:E / AndroidRuntime(9437):在
android.view.LayoutInflater.inflate(LayoutInflater.java:396)12-29
23:45:32.605:E / AndroidRuntime(9437):在
android.view.LayoutInflater.inflate(LayoutInflater.java:352)12-29
23:45:32.605:E / AndroidRuntime(9437):在
com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:271)
12-29 23:45:32.605:E / AndroidRuntime(9437):在
android.app.Activity.setContentView(Activity.java:1835)12-29
23:45:32.605:E / AndroidRuntime(9437):在
com.example.test_googlemap.MainActivity.onCreate(MainActivity.java:11)
12-29 23:45:32.605:E / AndroidRuntime(9437):在
android.app.Activity.performCreate(Activity.java:4465)12-29
23:45:32.605:E / AndroidRuntime(9437):在
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
12-29 23:45:32.605:E / AndroidRuntime(9437):在
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1932)
12-29 23:45:32.605:E / AndroidRuntime(9437):11 ...更多12-29
23:45:32.605:E / AndroidRuntime(9437):产生的原因:
java.lang.ClassCastException:com.google.android.gms.maps.MapFragment
不能转换为android.support.v4.app.Fragment 12-29 23:45:32.605:
E / AndroidRuntime(9437):在
android.support.v4.app.Fragment.instantiate(Fragment.java:394)12-29
23:45:32.605:E / AndroidRuntime(9437):在
android.support.v4.app.Fragment.instantiate(Fragment.java:369)12-29
23:45:32.605:E / AndroidRuntime(9437):在
android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:272)
12-29 23:45:32.605:E / AndroidRuntime(9437):在
android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:669)
12-29 23:45:32.605:E / AndroidRuntime(9437):20 ...更多
来源$ C $ C:
包com.example.test_googlemap;
进口android.os.Bundle;
进口android.support.v4.app.FragmentActivity;
公共类MainActivity扩展FragmentActivity {
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
}
}
activity_main.xml:
< XML版本=1.0编码=UTF-8&GT?;
<片段的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:ID =@ + ID /图
机器人:layout_width =match_parent
机器人:layout_height =match_parent
类=com.google.android.gms.maps.MapFragment/>
的Manifest.xml:
< XML版本=1.0编码=UTF-8&GT?;
<舱单的xmlns:机器人=http://schemas.android.com/apk/res/android
包=com.example.test_googlemap
安卓版code =1
机器人:VERSIONNAME =1.0>
<用途-SDK
安卓的minSdkVersion =3
机器人:targetSdkVersion =15/>
<应用
机器人:allowBackup =真
机器人:图标=@可绘制/ ic_launcher
机器人:标签=@字符串/ APP_NAME
机器人:主题=@风格/ AppTheme>
<活动
机器人:名称=com.example.test_googlemap.MainActivity
机器人:标签=@字符串/ APP_NAME>
<意向滤光器>
<作用机器人:名称=android.intent.action.MAIN/>
<类机器人:名称=android.intent.category.LAUNCHER/>
&所述; /意图滤光器>
< /活性GT;
&所述;元数据
机器人:名称=com.google.android.maps.v2.API_KEY
机器人:值=我删除它!/>
< /用途>
<许可
机器人:名称=com.example.Test_GoogleMap.permission.MAPS_RECEIVE
安卓的ProtectionLevel =签名/>
<使用-权限的Android:名称=com.example.Test_GoogleMap.permission.MAPS_RECEIVE/>
<使用-权限的Android:名称=android.permission.INTERNET对/>
<使用-权限的Android:名称=android.permission.WRITE_EXTERNAL_STORAGE/>
<使用-权限的Android:名称=com.google.android.providers.gsf.permission.READ_GSERVICES/>
<使用-权限的Android:名称=android.permission.ACCESS_COARSE_LOCATION/>
<使用-权限的Android:名称=android.permission.ACCESS_FINE_LOCATION/>
<用途特征
机器人:glEsVersion =0x00020000
机器人:要求=真/>
< /舱单>
您正在扩展 FragmentActivity
,表示您要使用的片段支持Android包反向移植。然而,你的<片断>
元素是指 MapFragment
,这是本机API等级11版片段。
替换 MapFragment
与 SupportMapFragment
,这应该清理这个特定崩溃。
I tried to make a sample project using Google Map, but I couldn't.Help me please!
Test Device : Android 4.0.4
Error Message :
12-29 23:45:32.605: E/AndroidRuntime(9437): FATAL EXCEPTION: main
12-29 23:45:32.605: E/AndroidRuntime(9437):
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.test_googlemap/com.example.test_googlemap.MainActivity}:
android.view.InflateException: Binary XML file line #2: Error
inflating class fragment 12-29 23:45:32.605: E/AndroidRuntime(9437):
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1968)
12-29 23:45:32.605: E/AndroidRuntime(9437): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1993)
12-29 23:45:32.605: E/AndroidRuntime(9437): at
android.app.ActivityThread.access$600(ActivityThread.java:127) 12-29
23:45:32.605: E/AndroidRuntime(9437): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1159)
12-29 23:45:32.605: E/AndroidRuntime(9437): at
android.os.Handler.dispatchMessage(Handler.java:99) 12-29
23:45:32.605: E/AndroidRuntime(9437): at
android.os.Looper.loop(Looper.java:137) 12-29 23:45:32.605:
E/AndroidRuntime(9437): at
android.app.ActivityThread.main(ActivityThread.java:4507) 12-29
23:45:32.605: E/AndroidRuntime(9437): at
java.lang.reflect.Method.invokeNative(Native Method) 12-29
23:45:32.605: E/AndroidRuntime(9437): at
java.lang.reflect.Method.invoke(Method.java:511) 12-29 23:45:32.605:
E/AndroidRuntime(9437): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
12-29 23:45:32.605: E/AndroidRuntime(9437): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557) 12-29
23:45:32.605: E/AndroidRuntime(9437): at
dalvik.system.NativeStart.main(Native Method) 12-29 23:45:32.605:
E/AndroidRuntime(9437): Caused by: android.view.InflateException:
Binary XML file line #2: Error inflating class fragment 12-29
23:45:32.605: E/AndroidRuntime(9437): at
android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
12-29 23:45:32.605: E/AndroidRuntime(9437): at
android.view.LayoutInflater.inflate(LayoutInflater.java:466) 12-29
23:45:32.605: E/AndroidRuntime(9437): at
android.view.LayoutInflater.inflate(LayoutInflater.java:396) 12-29
23:45:32.605: E/AndroidRuntime(9437): at
android.view.LayoutInflater.inflate(LayoutInflater.java:352) 12-29
23:45:32.605: E/AndroidRuntime(9437): at
com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:271)
12-29 23:45:32.605: E/AndroidRuntime(9437): at
android.app.Activity.setContentView(Activity.java:1835) 12-29
23:45:32.605: E/AndroidRuntime(9437): at
com.example.test_googlemap.MainActivity.onCreate(MainActivity.java:11)
12-29 23:45:32.605: E/AndroidRuntime(9437): at
android.app.Activity.performCreate(Activity.java:4465) 12-29
23:45:32.605: E/AndroidRuntime(9437): at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
12-29 23:45:32.605: E/AndroidRuntime(9437): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1932)
12-29 23:45:32.605: E/AndroidRuntime(9437): ... 11 more 12-29
23:45:32.605: E/AndroidRuntime(9437): Caused by:
java.lang.ClassCastException: com.google.android.gms.maps.MapFragment
cannot be cast to android.support.v4.app.Fragment 12-29 23:45:32.605:
E/AndroidRuntime(9437): at
android.support.v4.app.Fragment.instantiate(Fragment.java:394) 12-29
23:45:32.605: E/AndroidRuntime(9437): at
android.support.v4.app.Fragment.instantiate(Fragment.java:369) 12-29
23:45:32.605: E/AndroidRuntime(9437): at
android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:272)
12-29 23:45:32.605: E/AndroidRuntime(9437): at
android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:669)
12-29 23:45:32.605: E/AndroidRuntime(9437): ... 20 more
Source Code :
package com.example.test_googlemap;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
activity_main.xml :
<?xml version="1.0" encoding="utf-8"?>
<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.MapFragment"/>
Manifest.xml :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test_googlemap"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="3"
android:targetSdkVersion="15" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.test_googlemap.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="I removed it!"/>
</application>
<permission
android:name="com.example.Test_GoogleMap.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.example.Test_GoogleMap.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<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"/>
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
</manifest>
You are extending FragmentActivity
, indicating that you are trying to use the Android Support package backport of fragments. However, your <fragment>
element refers to MapFragment
, which is for the native API Level 11 edition of fragments.
Replace MapFragment
with SupportMapFragment
, and that should clear up this specific crash.
这篇关于&QUOT;错误充气类片段&QUOT;与谷歌地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!