我已经搜索了很多解决方法。但没有什么是有用的。我觉得这个问题没有解决方案。想通过上传我的问题给它最后一次尝试。
activity_main.xml中
<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=".MainActivity" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment"/>
</RelativeLayout>
MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
的Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.androidmapsv2"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="22" />
<permission
android:name="com.example.androidmapsv2.permission.MAPS_RECEIVE"
android:protectionLevel="signature" >
</permission>
<uses-permission android:name="com.example.androidmapsv2.permission.MAPS_RECEIVE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<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" >
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyBD9fsiMd_G9Pzeq2Eqas2FwJjzMGbSOnA" />
<activity
android:name="com.example.androidmapsv2.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>
记录错误
04-08 14:35:19.799: E/AndroidRuntime(27534): FATAL EXCEPTION: main
04-08 14:35:19.799: E/AndroidRuntime(27534): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.androidmapsv2/com.example.androidmapsv2.MainActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
04-08 14:35:19.799: E/AndroidRuntime(27534): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
04-08 14:35:19.799: E/AndroidRuntime(27534): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
04-08 14:35:19.799: E/AndroidRuntime(27534): at android.app.ActivityThread.access$600(ActivityThread.java:127)
04-08 14:35:19.799: E/AndroidRuntime(27534): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
04-08 14:35:19.799: E/AndroidRuntime(27534): at android.os.Handler.dispatchMessage(Handler.java:99)
04-08 14:35:19.799: E/AndroidRuntime(27534): at android.os.Looper.loop(Looper.java:137)
04-08 14:35:19.799: E/AndroidRuntime(27534): at android.app.ActivityThread.main(ActivityThread.java:4441)
04-08 14:35:19.799: E/AndroidRuntime(27534): at java.lang.reflect.Method.invokeNative(Native Method)
04-08 14:35:19.799: E/AndroidRuntime(27534): at java.lang.reflect.Method.invoke(Method.java:511)
04-08 14:35:19.799: E/AndroidRuntime(27534): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-08 14:35:19.799: E/AndroidRuntime(27534): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-08 14:35:19.799: E/AndroidRuntime(27534): at dalvik.system.NativeStart.main(Native Method)
04-08 14:35:19.799: E/AndroidRuntime(27534): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
04-08 14:35:19.799: E/AndroidRuntime(27534): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
04-08 14:35:19.799: E/AndroidRuntime(27534): at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
04-08 14:35:19.799: E/AndroidRuntime(27534): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
04-08 14:35:19.799: E/AndroidRuntime(27534): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
04-08 14:35:19.799: E/AndroidRuntime(27534): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
04-08 14:35:19.799: E/AndroidRuntime(27534): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
04-08 14:35:19.799: E/AndroidRuntime(27534): at android.app.Activity.setContentView(Activity.java:1835)
04-08 14:35:19.799: E/AndroidRuntime(27534): at com.example.androidmapsv2.MainActivity.onCreate(MainActivity.java:11)
04-08 14:35:19.799: E/AndroidRuntime(27534): at android.app.Activity.performCreate(Activity.java:4465)
04-08 14:35:19.799: E/AndroidRuntime(27534): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
04-08 14:35:19.799: E/AndroidRuntime(27534): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
04-08 14:35:19.799: E/AndroidRuntime(27534): ... 11 more
04-08 14:35:19.799: E/AndroidRuntime(27534): 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
04-08 14:35:19.799: E/AndroidRuntime(27534): at android.app.Fragment.instantiate(Fragment.java:581)
04-08 14:35:19.799: E/AndroidRuntime(27534): at android.app.Fragment.instantiate(Fragment.java:549)
04-08 14:35:19.799: E/AndroidRuntime(27534): at android.app.Activity.onCreateView(Activity.java:4235)
04-08 14:35:19.799: E/AndroidRuntime(27534): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:673)
04-08 14:35:19.799: E/AndroidRuntime(27534): ... 21 more
04-08 14:35:19.799: E/AndroidRuntime(27534): Caused by: java.lang.ClassNotFoundException: com.google.android.gms.maps.SupportMapFragment
04-08 14:35:19.799: E/AndroidRuntime(27534): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
04-08 14:35:19.799: E/AndroidRuntime(27534): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
04-08 14:35:19.799: E/AndroidRuntime(27534): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
04-08 14:35:19.799: E/AndroidRuntime(27534): at android.app.Fragment.instantiate(Fragment.java:571)
04-08 14:35:19.799: E/AndroidRuntime(27534): ... 24 more
google-play-services_lib 也已添加到项目属性中。
答案 0 :(得分:0)
尝试使用FrameLayout(id:map_container)并调用它:
private void displayMap() {
Log.d(TAG, "displayMap");
rootView.findViewById(R.id.map_container).setVisibility(View.VISIBLE);
mMapFragment = SupportMapFragment.newInstance();
FragmentTransaction fragmentTransaction =
mContext.getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.map_container, mMapFragment);
fragmentTransaction.commit();
mMapFragment.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
initMap(googleMap);
}
});
}
答案 1 :(得分:0)
最后我解决了这个问题!! :d
我将 ATD (从23.0.6降至22.3.0)和 SDK 的评级降级,并且完美运行...
也许这是一些兼容性问题..