android.view.InflateException,错误膨胀类片段

时间:2016-10-03 19:46:28

标签: android android-layout android-fragments nullpointerexception

我知道这是重复,但无法找到解决方案。这是我的Map XML文件

activity_map

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.test.healthapp.MapActivity" />
<fragment
    android:id="@+id/fragment_bottom"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"

    />
</RelativeLayout>

这是我发生错误的MapActivity(在setContentView上)代码段: MapActivity正在扩展FragmentActivity

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_map);

我的Logcat

 FATAL EXCEPTION: main
                                                                         Process: com.example.ather.healthapp, PID: 10185
                                                                         java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ather.healthapp/com.example.ather.healthapp.MapActivity}: android.view.InflateException: Binary XML file line #14: Binary XML file line #14: Error inflating class fragment
                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                             at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                             at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                             at android.os.Looper.loop(Looper.java:148)
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                             at java.lang.reflect.Method.invoke(Native Method)
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                          Caused by: android.view.InflateException: Binary XML file line #14: Binary XML file line #14: Error inflating class fragment
                                                                             at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
                                                                             at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
                                                                             at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
                                                                             at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:393)
                                                                             at android.app.Activity.setContentView(Activity.java:2172)
                                                                             at com.example.ather.healthapp.MapActivity.onCreate(MapActivity.java:97)
                                                                             at android.app.Activity.performCreate(Activity.java:6251)
                                                                             at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                             at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                             at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                             at android.os.Looper.loop(Looper.java:148) 
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                             at java.lang.reflect.Method.invoke(Native Method) 
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                                                                          Caused by: android.view.InflateException: Binary XML file line #14: Error inflating class fragment
                                                                             at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:782)
                                                                             at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
                                                                             at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
                                                                             at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
                                                                             at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
                                                                             at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
                                                                             at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
                                                                             at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:393) 
                                                                             at android.app.Activity.setContentView(Activity.java:2172) 
                                                                             at com.example.ather.healthapp.MapActivity.onCreate(MapActivity.java:97) 
                                                                             at android.app.Activity.performCreate(Activity.java:6251) 
                                                                             at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                             at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                             at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                             at android.os.Looper.loop(Looper.java:148) 
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                             at java.lang.reflect.Method.invoke(Native Method) 
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                                                                          Caused by: java.lang.NullPointerException
                                                                             at java.lang.VMClassLoader.findLoadedClass(Native Method)
                                                                             at java.lang.ClassLoader.findLoadedClass(ClassLoader.java:362)
                                                                             at java.lang.ClassLoader.loadClass(ClassLoader.java:499)
                                                                             at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
                                                                             at android.support.v4.app.Fragment.isSupportFragmentClass(Fragment.java:466)
                                                                             at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2379)
                                                                             at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:120)
                                                                             at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:376)
                                                                             at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:33)
                                                                             at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:75)
                                                                             at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:754)
                                                                             at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 
                                                                             at android.view.LayoutInflater.rInflate(LayoutInflater.java:835) 
                                                                             at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
                                                                             at android.view.LayoutInflater.inflate(LayoutInflater.java:515) 
                                                                             at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
                                                                             at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
                                                                             at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:393) 
                                                                             at android.app.Activity.setContentView(Activity.java:2172) 
                                                                             at com.example.ather.healthapp.MapActivity.onCreate(MapActivity.java:97) 
                                                                             at android.app.Activity.performCreate(Activity.java:6251) 
                                                                             at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 

非常感谢您的帮助。 我试图将activity_map的布局更改为LinearLayout,RelativeLayout和Framelayout,但没有任何效果。尝试将android:name或class添加到activity_map第15行的片段中。

2 个答案:

答案 0 :(得分:0)

您是否在Manifest.xml文件中声明了这些内容?

<application>元素中声明它,您的代码应该有效。

<meta-data
    android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />

 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

答案 1 :(得分:0)

问题是你的第二个片段标签没有定义片段类,然后启动NullPointerException,因为Android框架需要在那里加载一个类。您应该删除该标记(如果您不需要它),添加要加载的片段类,或者如果要在其中动态添加片段,请使用动态片段加载方法。请检查此guide of the official docs