膨胀 class 片段时出错
Error inflating class fragment
我遵循了这里的所有步骤:https://developers.google.com/maps/documentation/android/start#specify_app_settings_in_the_application_manifest
但是我在执行这段代码时仍然遇到崩溃:
setContentView(R.layout.map_activity);
该代码由位于我的 onCreate 方法中的按钮点击侦听器调用。
在按下按钮之前,一切都按预期工作,但是当我按下按钮以显示 google 地图时,我就崩溃了。
这是我的 logcat(在尝试建议后编辑)。
05-25 18:41:03.357 : W/dalvikvm(21600): threadid=1: thread exiting with uncaught exception (group=0x41655438)
05-25 18:41:03.383 : E/AndroidRuntime(21600): FATAL EXCEPTION: main
05-25 18:41:03.399 : E/AndroidRuntime(21600): android.view.InflateException: Binary XML file line #6: Error inflating class fragment
05-25 18:41:03.418 : E/AndroidRuntime(21600): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
05-25 18:41:03.436 : E/AndroidRuntime(21600): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
05-25 18:41:03.454 : E/AndroidRuntime(21600): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
05-25 18:41:03.477 : E/AndroidRuntime(21600): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
05-25 18:41:03.496 : E/AndroidRuntime(21600): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
05-25 18:41:03.519 : E/AndroidRuntime(21600): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:306)
05-25 18:41:03.538 : E/AndroidRuntime(21600): at android.app.Activity.setContentView(Activity.java:1901)
05-25 18:41:03.561 : E/AndroidRuntime(21600): at com.Actividad.Brian.actividad.run(actividad.java:2389)
05-25 18:41:03.590 : E/AndroidRuntime(21600): at com.Actividad.Brian.actividad.Google_Maps(actividad.java:2393)
05-25 18:41:03.619 : E/AndroidRuntime(21600): at com.Actividad.Brian.actividad.run(actividad.java:1066)
05-25 18:41:03.639 : E/AndroidRuntime(21600): at android.os.Handler.handleCallback(Handler.java:615)
05-25 18:41:03.664 : E/AndroidRuntime(21600): at android.os.Handler.dispatchMessage(Handler.java:92)
05-25 18:41:03.693 : E/AndroidRuntime(21600): at android.os.Looper.loop(Looper.java:137)
05-25 18:41:03.723 : E/AndroidRuntime(21600): at android.app.ActivityThread.main(ActivityThread.java:4918)
05-25 18:41:03.754 : E/AndroidRuntime(21600): at java.lang.reflect.Method.invokeNative(Native Method)
05-25 18:41:03.779 : E/AndroidRuntime(21600): at java.lang.reflect.Method.invoke(Method.java:511)
05-25 18:41:03.800 : E/AndroidRuntime(21600): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
05-25 18:41:03.824 : E/AndroidRuntime(21600): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
05-25 18:41:03.858 : E/AndroidRuntime(21600): at dalvik.system.NativeStart.main(Native Method)
05-25 18:41:03.898 : E/AndroidRuntime(21600): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.google.android.gms.maps.MapFragment: make sure class name exists, is public, and has an empty constructor that is public
05-25 18:41:03.924 : E/AndroidRuntime(21600): at android.app.Fragment.instantiate(Fragment.java:584)
05-25 18:41:03.962 : E/AndroidRuntime(21600): at android.app.Fragment.instantiate(Fragment.java:552)
05-25 18:41:04.002 : E/AndroidRuntime(21600): at android.app.Activity.onCreateView(Activity.java:4696)
05-25 18:41:04.028 : E/AndroidRuntime(21600): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
05-25 18:41:04.062 : E/AndroidRuntime(21600): ... 18 more
05-25 18:41:04.106 : E/AndroidRuntime(21600): Caused by: java.lang.ClassNotFoundException: com.google.android.gms.maps.MapFragment
05-25 18:41:04.133 : E/AndroidRuntime(21600): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
05-25 18:41:04.164 : E/AndroidRuntime(21600): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
05-25 18:41:04.198 : E/AndroidRuntime(21600): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
05-25 18:41:04.227 : E/AndroidRuntime(21600): at android.app.Fragment.instantiate(Fragment.java:574)
05-25 18:41:04.269 : E/AndroidRuntime(21600): ... 21 more
这里是map_activity.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"
android:name="com.google.android.gms.maps.MapFragment"/>
我的 activity 相当大,但这重现了行为而无需推动任何东西:
public class actividad extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.map_activity);
}
}
这是我的清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.Actividad.Brian"
android:versionName="1.01" android:installLocation="internalOnly"
android:versionCode="1">
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="22"/>
<supports-screens android:largeScreens="true" android:normalScreens="true" android:smallScreens="true" android:anyDensity="true" />
<uses-feature android:glEsVersion="0x00020000" android:required="true" />
<permission android:name="com.Actividad.Brian.permission.MAPS_RECEIVE" android:protectionLevel="signature" />
<uses-permission android:name="com.Actividad.Brian.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<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_SERVICES"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<application android:icon="@drawable/icon" android:label="Actividad" >
<meta-data android:name="com.google.android.geo.API_KEY" android:value="AIzaSyAYJTp4zzcPD8K_**INTENTIONAL**2R7Y" />
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
<activity android:windowSoftInputMode="stateHidden" android:launchMode="singleTop" android:name=".actividad" android:label="Actividad" android:screenOrientation="portrait" android:configChanges="orientation|screenSize" >
<intent-filter >
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
您是否看到我可能忽略的任何错误?
您必须为您的 activity 设置根布局,它在您的 xml 代码中保存片段。这是一个以 LinearLayout 作为根布局的示例:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.MapFragment"/>
</LinearLayout>
从您的项目中删除 android-support-v4.jar 并移动 google-play-services.jar 到 References 列表的顶部。你应该准备好了。
我遵循了这里的所有步骤:https://developers.google.com/maps/documentation/android/start#specify_app_settings_in_the_application_manifest 但是我在执行这段代码时仍然遇到崩溃:
setContentView(R.layout.map_activity);
该代码由位于我的 onCreate 方法中的按钮点击侦听器调用。
在按下按钮之前,一切都按预期工作,但是当我按下按钮以显示 google 地图时,我就崩溃了。
这是我的 logcat(在尝试建议后编辑)。
05-25 18:41:03.357 : W/dalvikvm(21600): threadid=1: thread exiting with uncaught exception (group=0x41655438)
05-25 18:41:03.383 : E/AndroidRuntime(21600): FATAL EXCEPTION: main
05-25 18:41:03.399 : E/AndroidRuntime(21600): android.view.InflateException: Binary XML file line #6: Error inflating class fragment
05-25 18:41:03.418 : E/AndroidRuntime(21600): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
05-25 18:41:03.436 : E/AndroidRuntime(21600): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
05-25 18:41:03.454 : E/AndroidRuntime(21600): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
05-25 18:41:03.477 : E/AndroidRuntime(21600): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
05-25 18:41:03.496 : E/AndroidRuntime(21600): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
05-25 18:41:03.519 : E/AndroidRuntime(21600): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:306)
05-25 18:41:03.538 : E/AndroidRuntime(21600): at android.app.Activity.setContentView(Activity.java:1901)
05-25 18:41:03.561 : E/AndroidRuntime(21600): at com.Actividad.Brian.actividad.run(actividad.java:2389)
05-25 18:41:03.590 : E/AndroidRuntime(21600): at com.Actividad.Brian.actividad.Google_Maps(actividad.java:2393)
05-25 18:41:03.619 : E/AndroidRuntime(21600): at com.Actividad.Brian.actividad.run(actividad.java:1066)
05-25 18:41:03.639 : E/AndroidRuntime(21600): at android.os.Handler.handleCallback(Handler.java:615)
05-25 18:41:03.664 : E/AndroidRuntime(21600): at android.os.Handler.dispatchMessage(Handler.java:92)
05-25 18:41:03.693 : E/AndroidRuntime(21600): at android.os.Looper.loop(Looper.java:137)
05-25 18:41:03.723 : E/AndroidRuntime(21600): at android.app.ActivityThread.main(ActivityThread.java:4918)
05-25 18:41:03.754 : E/AndroidRuntime(21600): at java.lang.reflect.Method.invokeNative(Native Method)
05-25 18:41:03.779 : E/AndroidRuntime(21600): at java.lang.reflect.Method.invoke(Method.java:511)
05-25 18:41:03.800 : E/AndroidRuntime(21600): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
05-25 18:41:03.824 : E/AndroidRuntime(21600): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
05-25 18:41:03.858 : E/AndroidRuntime(21600): at dalvik.system.NativeStart.main(Native Method)
05-25 18:41:03.898 : E/AndroidRuntime(21600): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.google.android.gms.maps.MapFragment: make sure class name exists, is public, and has an empty constructor that is public
05-25 18:41:03.924 : E/AndroidRuntime(21600): at android.app.Fragment.instantiate(Fragment.java:584)
05-25 18:41:03.962 : E/AndroidRuntime(21600): at android.app.Fragment.instantiate(Fragment.java:552)
05-25 18:41:04.002 : E/AndroidRuntime(21600): at android.app.Activity.onCreateView(Activity.java:4696)
05-25 18:41:04.028 : E/AndroidRuntime(21600): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
05-25 18:41:04.062 : E/AndroidRuntime(21600): ... 18 more
05-25 18:41:04.106 : E/AndroidRuntime(21600): Caused by: java.lang.ClassNotFoundException: com.google.android.gms.maps.MapFragment
05-25 18:41:04.133 : E/AndroidRuntime(21600): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
05-25 18:41:04.164 : E/AndroidRuntime(21600): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
05-25 18:41:04.198 : E/AndroidRuntime(21600): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
05-25 18:41:04.227 : E/AndroidRuntime(21600): at android.app.Fragment.instantiate(Fragment.java:574)
05-25 18:41:04.269 : E/AndroidRuntime(21600): ... 21 more
这里是map_activity.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"
android:name="com.google.android.gms.maps.MapFragment"/>
我的 activity 相当大,但这重现了行为而无需推动任何东西:
public class actividad extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.map_activity);
}
}
这是我的清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.Actividad.Brian"
android:versionName="1.01" android:installLocation="internalOnly"
android:versionCode="1">
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="22"/>
<supports-screens android:largeScreens="true" android:normalScreens="true" android:smallScreens="true" android:anyDensity="true" />
<uses-feature android:glEsVersion="0x00020000" android:required="true" />
<permission android:name="com.Actividad.Brian.permission.MAPS_RECEIVE" android:protectionLevel="signature" />
<uses-permission android:name="com.Actividad.Brian.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<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_SERVICES"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<application android:icon="@drawable/icon" android:label="Actividad" >
<meta-data android:name="com.google.android.geo.API_KEY" android:value="AIzaSyAYJTp4zzcPD8K_**INTENTIONAL**2R7Y" />
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
<activity android:windowSoftInputMode="stateHidden" android:launchMode="singleTop" android:name=".actividad" android:label="Actividad" android:screenOrientation="portrait" android:configChanges="orientation|screenSize" >
<intent-filter >
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
您是否看到我可能忽略的任何错误?
您必须为您的 activity 设置根布局,它在您的 xml 代码中保存片段。这是一个以 LinearLayout 作为根布局的示例:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.MapFragment"/>
</LinearLayout>
从您的项目中删除 android-support-v4.jar 并移动 google-play-services.jar 到 References 列表的顶部。你应该准备好了。