AppCompat v7工具栏“错误夸大类< unknown>". [英] AppCompat v7 Toolbar "Error inflating class <unknown>"

查看:74
本文介绍了AppCompat v7工具栏“错误夸大类< unknown>".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读SO线程此处和android博客文章后,我需要使用AppCompat V7:21中引入的新工具栏功能此处.我从博客文章中准确地复制了工具栏摘要到我的布局中:

I need to use new Toolbar feature introduced in AppCompat V7:21 after reading SO thread here and android blog post here. I exacctly copied Toolbar snippet from the blogpost to my layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar                  <- Line 8
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:minHeight="?attr/actionBarSize"
        app:theme="@style/ThemeOverlay.AppCompat.ActionBar" />

</LinearLayout>    

问题是我得到了:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.blabla.PrefActivity}: android.view.InflateException: Binary XML file line #8: Error inflating class <unknown>    

有趣的是,如果我删除此行: android:minHeight =?attr/actionBarSize"

Interestingly, if I remove this line: android:minHeight="?attr/actionBarSize"

有效.因此,问题不是由于项目依赖性而引起的.看来我只能访问诸如actionBarSize或colorPrimary

it works. So the problem is not because of project dependencies or so. It seems I only cannot access ?attr elements like actionBarSize or colorPrimary

无需说我已经添加了AppCompat依赖项.该活动是从PreferenceActivity继承的.这是我的礼物:

Not neede to say that I have already added AppCompat dependency. And the activity is inherited from PreferenceActivity. here's my gradle:

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:support-v4:21.0.3'
compile 'com.android.support:appcompat-v7:21.0.3'
}

此处

here and here people reported to solve the issue on a random basis. But my random didn't work for past two days.

这是我的活动:

public class PrefActivity extends PreferenceActivity {

private Toolbar mToolbar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ViewGroup root = (ViewGroup) findViewById(android.R.id.content);
    LinearLayout content = (LinearLayout) root.getChildAt(0);
    LinearLayout toolbarContainer = (LinearLayout) View.inflate(this, R.layout.activity_prefs, null);

    root.removeAllViews();
    toolbarContainer.addView(content);
    root.addView(toolbarContainer);

    mToolbar = (Toolbar) toolbarContainer.findViewById(R.id.toolbar);
    selectResource();
    mToolbar.setNavigationIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
    mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    });
}

推荐答案

使用属性(?attr/)意味着使用当前上下文主题中的值.您需要确保您的主题具有?attr/actionBarSize样式的价值.您可以从@ style/Theme.AppCompat使用或继承主题.或者只是手动设置值.

Using of attributes (?attr/) means using of values from theme of current Context. You need to be sure that your theme has value for ?attr/actionBarSize attibute. You can use or inherit your theme from @style/Theme.AppCompat. Or just set value manually.

这篇关于AppCompat v7工具栏“错误夸大类&lt; unknown&gt;".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆