我对这个问题很生气!它已经被讨论过很多次,但似乎没有什么对我有用。我只是想在我的活动中显示地图。这是我的文件:
MapTest.java
package it.michele.pedometer;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class MapTest extends FragmentActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_layout);
}
}
map_layout.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.SupportMapFragment"/><br>
显现
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="it.michele.pedometer"
android:installLocation="auto"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<permission
android:name="it.michele.pedometer.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="it.michele.pedometer.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<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.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@drawable/walking_man"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="it.michele.pedometer.Pedometer"
android:label="@string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="it.michele.pedometer.MapTest"
android:label="asd"
android:screenOrientation="portrait" >
</activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="myKey..." />
</application>
</manifest>
我在具有 API 16 的模拟器上进行了尝试,这是 LogCat:
09-11 20:33:41.319: E/AndroidRuntime(1017): FATAL EXCEPTION: main
09-11 20:33:41.319: E/AndroidRuntime(1017): java.lang.RuntimeException: Unable to start activity ComponentInfo{it.michele.pedometer/it.michele.pedometer.MapTest}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
09-11 20:33:41.319: E/AndroidRuntime(1017): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
09-11 20:33:41.319: E/AndroidRuntime(1017): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
09-11 20:33:41.319: E/AndroidRuntime(1017): at android.app.ActivityThread.access$600(ActivityThread.java:130)
09-11 20:33:41.319: E/AndroidRuntime(1017): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
09-11 20:33:41.319: E/AndroidRuntime(1017): at android.os.Handler.dispatchMessage(Handler.java:99)
09-11 20:33:41.319: E/AndroidRuntime(1017): at android.os.Looper.loop(Looper.java:137)
09-11 20:33:41.319: E/AndroidRuntime(1017): at android.app.ActivityThread.main(ActivityThread.java:4745)
09-11 20:33:41.319: E/AndroidRuntime(1017): at java.lang.reflect.Method.invokeNative(Native Method)
09-11 20:33:41.319: E/AndroidRuntime(1017): at java.lang.reflect.Method.invoke(Method.java:511)
09-11 20:33:41.319: E/AndroidRuntime(1017): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-11 20:33:41.319: E/AndroidRuntime(1017): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-11 20:33:41.319: E/AndroidRuntime(1017): at dalvik.system.NativeStart.main(Native Method)
09-11 20:33:41.319: E/AndroidRuntime(1017): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
09-11 20:33:41.319: E/AndroidRuntime(1017): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
09-11 20:33:41.319: E/AndroidRuntime(1017): at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
09-11 20:33:41.319: E/AndroidRuntime(1017): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
09-11 20:33:41.319: E/AndroidRuntime(1017): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
09-11 20:33:41.319: E/AndroidRuntime(1017): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256)
09-11 20:33:41.319: E/AndroidRuntime(1017): at android.app.Activity.setContentView(Activity.java:1867)
09-11 20:33:41.319: E/AndroidRuntime(1017): at it.michele.pedometer.MapTest.onCreate(MapTest.java:12)
09-11 20:33:41.319: E/AndroidRuntime(1017): at android.app.Activity.performCreate(Activity.java:5008)
09-11 20:33:41.319: E/AndroidRuntime(1017): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
09-11 20:33:41.319: E/AndroidRuntime(1017): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
09-11 20:33:41.319: E/AndroidRuntime(1017): ... 11 more
09-11 20:33:41.319: E/AndroidRuntime(1017): Caused by: android.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
09-11 20:33:41.319: E/AndroidRuntime(1017): at android.app.Fragment.instantiate(Fragment.java:584)
09-11 20:33:41.319: E/AndroidRuntime(1017): at android.app.Fragment.instantiate(Fragment.java:552)
09-11 20:33:41.319: E/AndroidRuntime(1017): at android.app.Activity.onCreateView(Activity.java:4656)
09-11 20:33:41.319: E/AndroidRuntime(1017): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
09-11 20:33:41.319: E/AndroidRuntime(1017): ... 20 more
09-11 20:33:41.319: E/AndroidRuntime(1017): Caused by: java.lang.ClassNotFoundException: com.google.android.gms.maps.SupportMapFragment
09-11 20:33:41.319: E/AndroidRuntime(1017): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
09-11 20:33:41.319: E/AndroidRuntime(1017): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
09-11 20:33:41.319: E/AndroidRuntime(1017): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
09-11 20:33:41.319: E/AndroidRuntime(1017): at android.app.Fragment.instantiate(Fragment.java:574)
09-11 20:33:41.319: E/AndroidRuntime(1017): ... 23 more
关于如何解决问题的任何想法?谢谢!
编辑
我做了这个更改:
我将 google-play-services.jar 引用为外部 jar
map_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
MapTest.java
package it.michele.pedometer;
import android.app.Activity;
import android.os.Bundle;
public class MapTest extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_layout);
}
}
日志猫
09-11 20:56:32.180: E/AndroidRuntime(1358): FATAL EXCEPTION: main
09-11 20:56:32.180: E/AndroidRuntime(1358): java.lang.NoClassDefFoundError: com.google.android.gms.R$styleable
09-11 20:56:32.180: E/AndroidRuntime(1358): at com.google.android.gms.maps.GoogleMapOptions.createFromAttributes(Unknown Source)
09-11 20:56:32.180: E/AndroidRuntime(1358): at com.google.android.gms.maps.MapFragment.onInflate(Unknown Source)
09-11 20:56:32.180: E/AndroidRuntime(1358): at android.app.Activity.onCreateView(Activity.java:4663)
09-11 20:56:32.180: E/AndroidRuntime(1358): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
09-11 20:56:32.180: E/AndroidRuntime(1358): at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
09-11 20:56:32.180: E/AndroidRuntime(1358): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
09-11 20:56:32.180: E/AndroidRuntime(1358): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
09-11 20:56:32.180: E/AndroidRuntime(1358): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256)
09-11 20:56:32.180: E/AndroidRuntime(1358): at android.app.Activity.setContentView(Activity.java:1867)
09-11 20:56:32.180: E/AndroidRuntime(1358): at it.michele.pedometer.MapTest.onCreate(MapTest.java:12)
09-11 20:56:32.180: E/AndroidRuntime(1358): at android.app.Activity.performCreate(Activity.java:5008)
09-11 20:56:32.180: E/AndroidRuntime(1358): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
09-11 20:56:32.180: E/AndroidRuntime(1358): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
09-11 20:56:32.180: E/AndroidRuntime(1358): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
09-11 20:56:32.180: E/AndroidRuntime(1358): at android.app.ActivityThread.access$600(ActivityThread.java:130)
09-11 20:56:32.180: E/AndroidRuntime(1358): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
09-11 20:56:32.180: E/AndroidRuntime(1358): at android.os.Handler.dispatchMessage(Handler.java:99)
09-11 20:56:32.180: E/AndroidRuntime(1358): at android.os.Looper.loop(Looper.java:137)
09-11 20:56:32.180: E/AndroidRuntime(1358): at android.app.ActivityThread.main(ActivityThread.java:4745)
09-11 20:56:32.180: E/AndroidRuntime(1358): at java.lang.reflect.Method.invokeNative(Native Method)
09-11 20:56:32.180: E/AndroidRuntime(1358): at java.lang.reflect.Method.invoke(Method.java:511)
09-11 20:56:32.180: E/AndroidRuntime(1358): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-11 20:56:32.180: E/AndroidRuntime(1358): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-11 20:56:32.180: E/AndroidRuntime(1358): at dalvik.system.NativeStart.main(Native Method)
我不知道该怎么办...