我是android开发的新手。我从一些简单但第一步开始,只是显示一张地图。以下是我为显示地图执行的步骤 1) 我获得了 google api 密钥,并配置了 android.mainfest。我添加了权限:MAPS_RECEIVE、ACCESS_COARSE_LOCATION、INTERNET 和我的 google map v2 api key in element。2) 我将 google-play-services_lib 导入我的工作区。构建它,并将它的引用添加到我的项目中。3)因为我使用的是android 2.3.3(api 10),所以我使用了SupportMapFragment而不是MapFragment。这是我的布局:
<RelativeLayout 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=".ShowMapActivity" >
<fragment android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment "/>
</RelativeLayout>
这是我在 ShowMapActivity 中的代码(它是 FragmentActivity 的扩展)
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_map);
if(findViewById(R.id.map)!=null)
{
SupportMapFragment fragment = new SupportMapFragment();
getSupportFragmentManager().beginTransaction().add(R.id.map,fragment).commit();
}
}
但不幸的是,当我在模拟器中运行应用程序时,我仍然在 log cat 中收到错误:Error inflating class fragment and com.google.android.gms.maps.MapFragment: make sure class name exists, is public, and has a empty constructor that是公开的。编辑:这是我的日志猫:
05-26 13:52:25.457: E/AndroidRuntime(289): FATAL EXCEPTION: main
05-26 13:52:25.457: E/AndroidRuntime(289): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.com.vogella.android.locationapi.maps/com.vogella.android.locationapi.maps.ShowMapActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
05-26 13:52:25.457: E/AndroidRuntime(289): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
05-26 13:52:25.457: E/AndroidRuntime(289): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
05-26 13:52:25.457: E/AndroidRuntime(289): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-26 13:52:25.457: E/AndroidRuntime(289): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
05-26 13:52:25.457: E/AndroidRuntime(289): at android.os.Handler.dispatchMessage(Handler.java:99)
05-26 13:52:25.457: E/AndroidRuntime(289): at android.os.Looper.loop(Looper.java:123)
05-26 13:52:25.457: E/AndroidRuntime(289): at android.app.ActivityThread.main(ActivityThread.java:3683)
05-26 13:52:25.457: E/AndroidRuntime(289): at java.lang.reflect.Method.invokeNative(Native Method)
05-26 13:52:25.457: E/AndroidRuntime(289): at java.lang.reflect.Method.invoke(Method.java:507)
05-26 13:52:25.457: E/AndroidRuntime(289): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-26 13:52:25.457: E/AndroidRuntime(289): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-26 13:52:25.457: E/AndroidRuntime(289): at dalvik.system.NativeStart.main(Native Method)
05-26 13:52:25.457: E/AndroidRuntime(289): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
05-26 13:52:25.457: E/AndroidRuntime(289): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:587)
05-26 13:52:25.457: E/AndroidRuntime(289): at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
05-26 13:52:25.457: E/AndroidRuntime(289): at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
05-26 13:52:25.457: E/AndroidRuntime(289): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
05-26 13:52:25.457: E/AndroidRuntime(289): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
05-26 13:52:25.457: E/AndroidRuntime(289): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
05-26 13:52:25.457: E/AndroidRuntime(289): at android.app.Activity.setContentView(Activity.java:1657)
05-26 13:52:25.457: E/AndroidRuntime(289): at com.vogella.android.locationapi.maps.ShowMapActivity.onCreate(ShowMapActivity.java:22)
05-26 13:52:25.457: E/AndroidRuntime(289): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-26 13:52:25.457: E/AndroidRuntime(289): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
05-26 13:52:25.457: E/AndroidRuntime(289): ... 11 more
05-26 13:52:25.457: E/AndroidRuntime(289): Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.google.android.gms.maps.SupportMapFragment : make sure class name exists, is public, and has an empty constructor that is public
那么我该如何解决它。我遗漏了哪些步骤或做错了
谢谢你的帮助,对不起大家,因为我的英语写作能力很差