android.view.InflateException:二进制XML文件行#19:错误膨胀类android.support.v7.widget.ContentFrameLayout

时间:2017-03-09 11:29:32

标签: android xml

我想通过编程方式更改标题栏文字和背景颜色。这就是为什么我使用了以下代码:

public class SplashScreen extends AppCompatActivity{
       protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.activity_splash);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.customtitlebar);
        TextView customTitleText = (TextView) findViewById(R.id.customtitlebar);
        customTitleText.setText("Whatever you want in title");
         }
}

我的customtitlebar.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:background="@color/titletextcolor">

    <ImageView 
        android:layout_width="25px" 
        android:layout_height="25px"
        android:src="@drawable/ic_launcher"> 
    </ImageView>

    <TextView 
        android:id="@+id/customtitlebar"
        android:layout_width="wrap_content" 
        android:layout_height="fill_parent"
        android:text="" 
        android:textColor="@color/titletextcolor" 
        android:textStyle="bold"
        android:background="@color/titlebackgroundcolor" 
        android:padding="3px" />
</LinearLayout>

但是当我想运行项目时,我遇到了这个例外:

03-09 17:21:52.181: E/ActivityThread(15403): RuntimeExceptionjava.lang.RuntimeException: Unable to start activity ComponentInfo{com.asif.gmaillogin/com.asif.gmaillogin.SplashScreen}: android.view.InflateException: Binary XML file line #19: Error inflating class android.support.v7.widget.ContentFrameLayout

如何解决此异常?请帮我 。

2 个答案:

答案 0 :(得分:0)

请参阅xml文件的第19行。 TextView android:text="" attr为空,如果你想让它没有文字删除这个attr

<TextView 
    android:id="@+id/customtitlebar"
    android:layout_width="wrap_content" 
    android:layout_height="fill_parent"
    android:textColor="@color/titletextcolor" 
    android:textStyle="bold"
    android:background="@color/titlebackgroundcolor" 
    android:padding="3px" />

答案 1 :(得分:0)

此行必须是问题

requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

您必须在requestWindowFeature()之前和super之前致电setContentView()

您的代码就像 -

public class SplashScreen extends AppCompatActivity{


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.customtitlebar);
        TextView customTitleText = (TextView) findViewById(R.id.customtitlebar);
        customTitleText.setText("Whatever you want in title");
    }
}

P / s,需要用户getWindow().setFeatureInt();方法。请参阅此SO link了解详情。