How to fix Error inflating class null

2019-02-25 03:21发布

问题:

I am trying to make a scrollable tab but when I set a view in my fragment layout the app starts giving error as Error inflating class null.It was working whenthe view is not added but when I add the view in the layout it gives me error. I added the class also as given here but it is not working .How I can fix this here is my fragment code

<FrameLayout 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="com.example.neelay.anotherbeatbox.Fragment.Songs"
android:background="@color/colorPrimaryDark"
class = "com.example.neelay.anotherbeatbox.Fragment.Songs">

<!-- TODO: Update blank fragment layout -->
<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="@string/hello_blank_fragment" />
<view
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#fff"
android:layout_marginTop="150dp"/>
</FrameLayout>

Fragment package name is package com.example.neelay.anotherbeatbox.Fragment; fragment name songs .How I can fix this ? Error

E/AndroidRuntime: FATAL EXCEPTION: main
              android.view.InflateException: Binary XML file line #14: Error inflating class null
                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
                  at com.example.neelay.anotherbeatbox.Fragment.Songs.onCreateView(Songs.java:27)
                  at android.support.v4.app.Fragment.performCreateView(Fragment.java:2074)
                  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
                  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1286)
                  at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:758)
                  at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1632)
                  at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:637)
                  at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:143)
                  at com.antonyt.infiniteviewpager.InfinitePagerAdapter.finishUpdate(InfinitePagerAdapter.java:67)
                  at android.support.v4.view.ViewPager.populate(ViewPager.java:1237)
                  at android.support.v4.view.ViewPager.populate(ViewPager.java:1085)
                  at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1611)
                  at android.view.View.measure(View.java:15356)
                  at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:622)
                  at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:404)
                  at android.view.View.measure(View.java:15356)
                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4928)
                  at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:671)
                  at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:738)
                  at android.view.View.measure(View.java:15356)
                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4928)
                  at android.widget.FrameLayout.onMeasure(FrameLayout.java:315)
                  at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
                  at android.view.View.measure(View.java:15356)
                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4928)
                  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1457)
                  at android.widget.LinearLayout.measureVertical(LinearLayout.java:723)
                  at android.widget.LinearLayout.onMeasure(LinearLayout.java:583)
                  at android.view.View.measure(View.java:15356)
                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4928)
                  at android.widget.FrameLayout.onMeasure(FrameLayout.java:315)
                  at android.view.View.measure(View.java:15356)
                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4928)
                  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1457)
                  at android.widget.LinearLayout.measureVertical(LinearLayout.java:723)
                  at android.widget.LinearLayout.onMeasure(LinearLayout.java:583)
                  at android.view.View.measure(View.java:15356)
                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4928)
                  at android.widget.FrameLayout.onMeasure(FrameLayout.java:315)
                  at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2148)
                  at android.view.View.measure(View.java:15356)
                  at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1858)
                  at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1110)
                  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1283)
                  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1008)
                  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4222)
                  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
                  at android.view.Choreographer.doCallbacks(Choreographer.java:555)
                  at android.view.Choreographer.doFrame(Choreographer.java:525)
                  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
                  at android.os.Handler.handleCallback(Handler.java:615)
                at android.os.Handler.dispatchMessage(Handler.java:92

D/dalvikvm: GC_CONCURRENT freed 343K, 10% free 6423K/7111K, paused 12ms+16ms, total 53ms

My fragment class

package com.example.neelay.anotherbeatbox.Fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

 import com.example.neelay.anotherbeatbox.R;


public class Songs extends Fragment {


public Songs() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_songs, container, false);
}

}

回答1:

Replace

<view

with

<View

View is an actual widget you can inflate and seems to fit your intended purpose. view is a special tag and with it you can inflate view classes by a name defined in attributes, and you have not defined such class name (it comes up as null in inflater).



回答2:

You need to define the proper package name for the view class. By looking at the package name defined in your class you can define it in your layout xml like this

<com.example.neelay.anotherbeatbox.Fragment.Songs
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="#fff"
    android:layout_marginTop="150dp"/>