我正在使用Android Studio,当我在布局应用程序中使用textview时关闭显示错误:
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)
在我的片段类
中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代码:
<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>
这段代码与eclipse一起工作正常。我尝试在Studio工作时唯一的问题
答案 0 :(得分:12)
android:background="@drawable/offers"
我希望优惠是图片,它比屏幕大,所以用一些颜色代码替换n试试
喜欢这个
android:background="#ff0000"
希望它有效..
检查一下
android.view.InflateException: Binary XML file line #12: Error inflating class <unknown>
修改强>
删除此行
android:textAppearance="?android:attr/textAppearanceMedium"
答案 1 :(得分:1)
我清理了你的xml并添加了命名空间:
<?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>
当您使用LinearLayout时,参数如&#34; android:layout_below&#34;或者&#34; android:layout_centerHorizontal&#34;并非无效。
答案 2 :(得分:1)
我也遇到了这个错误。
我通过简单地重新创建包含.xml
的新TextView
文件解决了这个问题。
就我而言,错误是xml版本(v21 \ abc.xml)。
代码在Android版本6(marshmallow)中运行良好,而不是在Android 4.4版(kitkat)中运行时抛出错误。
答案 3 :(得分:1)
我遇到了同样的问题。
确保在任何布局中没有
tools:targetApi="lollipop"
。相反,在layout
下有两个布局,在layout-v21
文件夹下添加另一个布局。
答案 4 :(得分:0)
似乎旧版本的Android不支持这样的标签
android:background="@drawable/rounded_corner"
android:textSize="?android:textAppearanceMedium"
在这种情况下,请将其从XML文件中删除,然后尝试从Java或Kotlin代码中加载属性(最好在try ... catch或if ... else子句中)
希望有帮助
答案 5 :(得分:0)
您必须在项目的res->字体文件夹中创建另一个.xml文件。之后,调用.xml文件中的字体文件。
<font
android:font="@font/rubikbold"/>
在这里,我的项目中有一个robik粗体字体文件。
答案 6 :(得分:-1)
我遇到了类似的错误,但是我发现属性textAppearance没有正确编写。
最初是android:textSize="?android:textAppearanceMedium"
而不是android:textAppearance="?android:textAppearanceMedium"
您可以在https://developer.android.com/reference/android/widget/TextView
处了解有关TextView的更多信息。