【问题标题】:android.view.InflateException: Binary XML file line #7: Error inflating class Toolbarandroid.view.InflateException: Binary XML file line #7: Error inflating class Toolbar
【发布时间】:2014-12-15 06:55:24
【问题描述】:

我正在尝试使用android.support.v7 库来使用ToolBar(棒棒糖小部件)。

但在运行应用程序时出现错误。

android.view.InflateException: Binary XML file line #7: Error inflating class Toolbar    

我的主要目标是使用toolbar 获得navigation drawer。

这是我正在使用的布局文件:

   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/frame_container1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >


<Toolbar android:id="@+id/toolbar"            <------ line #7
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/status" ></Toolbar>

<android.support.v4.widget.DrawerLayout 
    android:id="@+id/drawer_layout"
    android:layout_below="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF" >

    <!-- Framelayout to display Fragments -->
    <RelativeLayout
        android:id="@+id/frame_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </RelativeLayout>
    <!-- Listview to display slider menu -->

    <ListView
        android:id="@+id/list_slidermenu"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:dividerHeight=".02dp"        
        android:background="#000000"/>

</android.support.v4.widget.DrawerLayout>

        </RelativeLayout>  

我正在使用以下代码:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, new String[]{""});
        //mDrawerList.setAdapter(adapter);
         t=(Toolbar) findViewById(R.id.toolbar);

        //getActionBar().setDisplayHomeAsUpEnabled(true);
        //getActionBar().setHomeButtonEnabled(true);


        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                R.string.app_name, // nav drawer open - description for accessibility
                R.string.app_name // nav drawer close - description for accessibility
        ) {
            public void onDrawerClosed(View view) {

                // calling onPrepareOptionsMenu() to show action bar icons
                //  invalidateOptionsMenu();
            super.onDrawerClosed(view);
            }

            public void onDrawerOpened(View drawerView) {


                super.onDrawerClosed(drawerView);
            }
        };
        mDrawerLayout.setDrawerListener(mDrawerToggle);
    }

请帮忙。

【问题讨论】:

    标签: android android-5.0-lollipop inflate-exception


    【解决方案1】:

    使用

    <android.support.v7.widget.Toolbar  />
    

    而不是

    <Toolbar  />
    

    【讨论】:

    • 我得到了完全相同的错误,但我的布局中确实定义了&lt;android.support.v7.widget.Toolbar /&gt;。还有其他想法吗?
    • 没关系。似乎现在正在工作。我做了两件事;我不确定是哪一个修复了它:1)我更新了我的libs 文件夹中的android-support-v13.jar 库(我使用的那个仍然来自以前的SDK)。 2) 我将 XML 从:&lt;android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="56dp" /&gt; 更改为:&lt;android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="56dp" &gt; &lt;/android.support.v7.widget.Toolbar&gt;
    • 也有这个问题,过了一段时间才意识到我只是忘记让MyActivity扩展ActionBarActivity而不是Activity。可能会帮助某人
    猜你喜欢
    • 2012-03-04
    • 2016-07-11
    • 1970-01-01
    • 2016-11-03
    • 2020-06-27
    • 1970-01-01
    • 2015-08-21
    • 2012-02-09
    • 2013-06-25
    相关资源
    最近更新 更多