13

I am using Android Studio, when i use textview in my layout application is closing showing error:

android.view.InflateException: Binary XML file line #26: Error inflating class android.widget.TextView
            at android.view.LayoutInflater.createView(LayoutInflater.java:633)
            at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)
            at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
            at com.SubFragment.onCreateView(SubFragment.java:20)
            at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
            at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:486)
            at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
            at android.support.v4.view.ViewPager.populate(ViewPager.java:1073)
            at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
            at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1441)
            at android.view.View.measure(View.java:17430)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:875)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
            at android.view.View.measure(View.java:17430)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
            at android.view.View.measure(View.java:17430)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
            at android.view.View.measure(View.java:17430)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
            at android.view.View.measure(View.java:17430)
            at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:851)
            at android.view.View.measure(View.java:17430)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
            at android.view.View.measure(View.java:17430)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
            at android.view.View.measure(View.java:17430)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
            at android.view.View.measure(View.java:17430)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
            at android.view.View.measure(View.java:17430)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2560)
            at android.view.View.measure(View.java:17430)

and here in my Fragment class

public class SubFragment extends Fragment{


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_homescreen, container, false);// getting error here


        return rootView;
    }


}

XML code:

<LinearLayout
        android:layout_margin="10dp"
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:gravity="center_vertical|center_horizontal"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@drawable/offers">


        <ImageView
            android:id="@+id/imageButtonOffers"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:src="@drawable/offers_icon" />

        <TextView
            android:id="@+id/textViewOffers"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/imageButtonOffers"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="5dp"
            android:gravity="center_horizontal"
            android:text="Offer"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@color/white"
            android:textSize="15sp" />


    </LinearLayout>

this code is working fine with eclipse. only problem when i try to work in Studio

Nama
  • 288
  • 1
  • 5
  • 22
  • can u pls post xml and all ? – user1140237 Feb 09 '15 at 08:16
  • double check your xml file, where you declare your textview, this means you have made a spelling mistake somewhere. Probably forgot a character or something. – Sbonelo Feb 09 '15 at 08:19
  • @user1140237 added xml – Nama Feb 09 '15 at 08:19
  • @SboneloMbhamali, I tested this, the same code working in eclipse, i have postred my xml code for reference – Nama Feb 09 '15 at 08:23
  • you xml i checked its correct (considering root view is with correct namespace) once clean and build app(make project). or post the code for the fragment transaction related – user1140237 Feb 09 '15 at 08:24
  • Since your root layout is vertical, I dont think you need the layout_below attribute of your textview, try fixing that and see what happenss. – Sbonelo Feb 09 '15 at 08:25
  • 3
    You are missing the **namespace** declaration. Add `xmlns:android="http://schemas.android.com/apk/res/android"` to the LinearLayout. – Phantômaxx Feb 09 '15 at 08:34

8 Answers8

14
android:background="@drawable/offers"

i hope offers is image and its larger than the screen so replace by some color code n try

Like this

android:background="#ff0000"

hope it works..

check this too

android.view.InflateException: Binary XML file line #12: Error inflating class <unknown>

edit:

remove this line

android:textAppearance="?android:attr/textAppearanceMedium"
Community
  • 1
  • 1
M S Gadag
  • 2,057
  • 1
  • 12
  • 15
1

I cleaned up your xml and added the namespace:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_margin="10dp"
    android:layout_weight="1"
    android:background="@drawable/offers"
    android:gravity="center_vertical|center_horizontal"
    android:orientation="vertical" >

<ImageView
    android:id="@+id/imageButtonOffers"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/offers_icon" />

<TextView
    android:id="@+id/textViewOffers"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    android:gravity="center_horizontal"
    android:text="Offer"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="@color/white"
    android:textSize="15sp" />

</LinearLayout>

As you are using a LinearLayout, parameters like "android:layout_below" or "android:layout_centerHorizontal" are not invalid.

Juanjo Vega
  • 1,410
  • 1
  • 12
  • 20
  • first i tried with the Relative layout, so i have used layout_below params.. and this is a child of my main xml, so as of my knowledge, there is no need of adding namespace again for child – Nama Feb 09 '15 at 09:25
  • The, you should paste your entire xml layout. It's confusing otherwise. – Juanjo Vega Feb 09 '15 at 11:59
1

I also encountered this error.

I solved it by simply recreating new .xml file containing TextView. In my case, the error was the xml version(v21\abc.xml). code run well in android version 6(marshmallow) rather than it throws error while run in android version 4.4(kitkat).

MWiesner
  • 8,868
  • 11
  • 36
  • 70
Omkar
  • 3,040
  • 1
  • 22
  • 42
1

I was Faced with the same problem. Make sure you Do not have tools:targetApi="lollipop" in any of your Layouts.Instead, have two Layouts one under layout add the other under layout-v21 folder.

E_K
  • 2,159
  • 23
  • 39
  • 1
    When I had this problem, all I did to fix it was remove the android:drawableLeft statement. It turns out that assigning an image to an AutoCompleteTextView on pre-Lollipop devices causes the problem. On my Lollipop phone, it works fine. – FractalBob Jun 08 '17 at 21:57
0

It seems old versions of Android do not support tags like,

android:background="@drawable/rounded_corner"
android:textSize="?android:textAppearanceMedium"

In that case, remove it from the XML file, and try to load the attributes from the Java or Kotlin code (preferably inside a try...catch or an if...else clause)

Hope it helps

Ashwin Balani
  • 745
  • 7
  • 19
0

You have to create another .xml file in the res-> font folder of your project. After that call the font file in the .xml file.

<font
    android:font="@font/rubikbold"/>

Here, I have a robik bold font file in my project.

0

Check your theme parent. If mismatch happens between theme parent and style parent you get the attribute missing or failed to resolve error. In my case I was using a theme of parent :

Theme.AppCompat.DayNight.NoActionBar

But in my style I used parent as :

TextAppearance.AppCompat.Widget.PopupMenu.Small

Since they are different it causes failed to resolve symbol error. Hope this might help..

Willey Hute
  • 939
  • 13
  • 18
-1

I encountered similar error but I found out the attribute textAppearance was not properly written.

initially android:textSize="?android:textAppearanceMedium" instead of android:textAppearance="?android:textAppearanceMedium"

you can read more about TextView here https://developer.android.com/reference/android/widget/TextView

Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
trustidkid
  • 577
  • 4
  • 6