膨胀类片段时出错

时间:2015-05-25 23:55:11

标签: java android android-fragments android-maps-v2

我按照此处的所有步骤操作:https://developers.google.com/maps/documentation/android/start#specify_app_settings_in_the_application_manifest  但是在执行此代码时我仍然遇到崩溃:

setContentView(R.layout.map_activity);

代码由位于我的onCreate方法中的按钮单击侦听器调用。

在按下按钮之前,一切都按预期工作,但只要按下按钮显示谷歌地图,我就会崩溃。

这是我的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$12.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$4.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"/> 

我的活动相当大,但这无需推送任何内容即可重现行为:

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>

你看到我可能忽略的任何错误吗?

2 个答案:

答案 0 :(得分:2)

您必须为活动设置根布局,该布局将片段保存在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>

答案 1 :(得分:1)

从项目中删除 android-support-v4.jar ,然后将 google-play-services.jar 移至参考列表列表的顶部。你应该阅读本文。