我遇到一个问题,收到RuntimeException和InflateException声明二进制XML文件:错误膨胀类android.support.v7.widget.Toolbar。我创建了一个试图使用导航抽屉的项目。我使用Android 5.0创建了一个android-support-v7-appcompat库(遵循谷歌文档的指示)。在应用程序中,我正在扩展NavigationDrawerActivity。我不确定是什么原因造成的。这些库似乎设置正确,但应用程序在启动时崩溃。
MainActivity.java
public class MainActivity extends NavigationDrawerActivity {
private static final String LOGTAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState)
{
// Layout
setContentView(R.layout.activity_main);
super.onCreate(savedInstanceState);
NavigationDrawerActivity.java
public class NavigationDrawerActivity extends ActionBarActivity
{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Use ToolBar and set it as ActionBar
Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
setSupportActionBar(toolbar);
activity_main.xml中
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true" xmlns:app="http://schemas.android.com/apk/res/com.mobile_iab_ad_framework">
<!-- Added fitSystemWindows to allow NavDrawer overlay ActionBar -->
<!-- To display fragments -->
<FrameLayout
android:id="@+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- Use a Toolbar so the drawer can be displayed over ActionBar -->
<android.support.v7.widget.Toolbar
android:id="@+id/my_awesome_toolbar"
style="@style/HeaderBar"
app:theme="@style/ActionBarThemeOverlay"
app:popupTheme="@style/ActionBarPopupThemeOverlay"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</FrameLayout>
<!-- 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:background="@color/list_background"
android:choiceMode="singleChoice"
android:divider="@color/list_divider"
android:dividerHeight="0dp"
android:listSelector="@drawable/list_selector" />
</android.support.v4.widget.DrawerLayout>
LogCat日志
12-06 03:06:56.487: E/AndroidRuntime(7105): FATAL EXCEPTION: main
12-06 03:06:56.487: E/AndroidRuntime(7105): Process: com.package, PID: 7105
12-06 03:06:56.487: E/AndroidRuntime(7105): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mobile_iab_ad_framework/com.package.device_discovery.MainActivity}: android.view.InflateException: Binary XML file line #17: Error inflating class android.support.v7.widget.Toolbar
12-06 03:06:56.487: E/AndroidRuntime(7105): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
12-06 03:06:56.487: E/AndroidRuntime(7105): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
12-06 03:06:56.487: E/AndroidRuntime(7105): at android.app.ActivityThread.access$900(ActivityThread.java:161)
12-06 03:06:56.487: E/AndroidRuntime(7105): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
12-06 03:06:56.487: E/AndroidRuntime(7105): at android.os.Handler.dispatchMessage(Handler.java:102)
12-06 03:06:56.487: E/AndroidRuntime(7105): at android.os.Looper.loop(Looper.java:157)
12-06 03:06:56.487: E/AndroidRuntime(7105): at android.app.ActivityThread.main(ActivityThread.java:5356)
12-06 03:06:56.487: E/AndroidRuntime(7105): at java.lang.reflect.Method.invokeNative(Native Method)
12-06 03:06:56.487: E/AndroidRuntime(7105): at java.lang.reflect.Method.invoke(Method.java:515)
12-06 03:06:56.487: E/AndroidRuntime(7105): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
12-06 03:06:56.487: E/AndroidRuntime(7105): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
12-06 03:06:56.487: E/AndroidRuntime(7105): at dalvik.system.NativeStart.main(Native Method)
12-06 03:06:56.487: E/AndroidRuntime(7105): Caused by: android.view.InflateException: Binary XML file line #17: Error inflating class android.support.v7.widget.Toolbar
12-06 03:06:56.487: E/AndroidRuntime(7105): at android.view.LayoutInflater.createView(LayoutInflater.java:626)
12-06 03:06:56.487: E/AndroidRuntime(7105): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:702)
12-06 03:06:56.487: E/AndroidRuntime(7105): at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
12-06 03:06:56.487: E/AndroidRuntime(7105): at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
12-06 03:06:56.487: E/AndroidRuntime(7105): at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
12-06 03:06:56.487: E/AndroidRuntime(7105): at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
12-06 03:06:56.487: E/AndroidRuntime(7105): at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
12-06 03:06:56.487: E/AndroidRuntime(7105): at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:228)
12-06 03:06:56.487: E/AndroidRuntime(7105): at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
12-06 03:06:56.487: E/AndroidRuntime(7105): at com.package.device_discovery.MainActivity.onCreate(MainActivity.java:51)
12-06 03:06:56.487: E/AndroidRuntime(7105): at android.app.Activity.performCreate(Activity.java:5431)
12-06 03:06:56.487: E/AndroidRuntime(7105): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
12-06 03:06:56.487: E/AndroidRuntime(7105): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
12-06 03:06:56.487: E/AndroidRuntime(7105): ... 11 more
12-06 03:06:56.487: E/AndroidRuntime(7105): Caused by: java.lang.reflect.InvocationTargetException
12-06 03:06:56.487: E/AndroidRuntime(7105): at java.lang.reflect.Constructor.constructNative(Native Method)
12-06 03:06:56.487: E/AndroidRuntime(7105): at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
12-06 03:06:56.487: E/AndroidRuntime(7105): at android.view.LayoutInflater.createView(LayoutInflater.java:600)
12-06 03:06:56.487: E/AndroidRuntime(7105): ... 23 more
12-06 03:06:56.487: E/AndroidRuntime(7105): Caused by: java.lang.NoSuchMethodError: android.support.v4.content.ContextCompat.getDrawable
12-06 03:06:56.487: E/AndroidRuntime(7105): at android.support.v7.internal.widget.TintManager.getDrawable(TintManager.java:133)
12-06 03:06:56.487: E/AndroidRuntime(7105): at android.support.v7.internal.widget.TintTypedArray.getDrawable(TintTypedArray.java:62)
12-06 03:06:56.487: E/AndroidRuntime(7105): at android.support.v7.widget.Toolbar.<init>(Toolbar.java:249)
12-06 03:06:56.487: E/AndroidRuntime(7105): at android.support.v7.widget.Toolbar.<init>(Toolbar.java:191)
12-06 03:06:56.487: E/AndroidRuntime(7105): ... 26 more
styles.xml
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
<item name="android:actionBarStyle">@style/MyActionBar</item>
<item name="windowActionBar">false</item>
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
<item name="android:actionBarStyle">@style/MyActionBar</item>
<item name="windowActionBar">false</item>
</style>
<!-- Extends Theme.Holo.Light and removes the Action Bar -->
<style name="ThemeWithoutActionBar" parent="android:Theme.Holo.Light">
<item name="android:windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
</style>
<!-- ************************************************************** -->
<!-- Action Bar Style (Requires API 11) -->
<style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">@color/action_bar_bg</item>
<item name="android:height">60dp</item>
<item name="android:titleTextStyle">@style/MyActionBar.TitleTextStyle</item>
</style>
<!-- ************************************************************** -->
<!-- ************************************************************** -->
<!-- Action Bar Text Style (Requires API 13) -->
<style name="MyActionBar.TitleTextStyle" parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Title">
<item name="android:textColor">#FFFFFF</item>
<item name="android:textSize">20sp</item>
</style>
<!-- ************************************************************** -->
<!-- ************************************************************** -->
<!-- Toolbar Style -->
<style name="toolbarStyle" parent="Theme.AppCompat">
</style>
<style name="ActionBarThemeOverlay" parent="">
<!-- Color of Toolbar Font Color -->
<item name="android:textColorPrimary">@android:color/white</item>
<!-- Color of Toolbar Action Buttons -->
<item name="colorControlNormal">@android:color/black</item>
<item name="colorControlHighlight">@android:color/black</item>
</style>
<style name="HeaderBar">
<!-- Color of Toolbar Background Color -->
<item name="android:background">@color/action_bar_bg</item>
<item name="android:minHeight">80dp</item>
</style>
<style name="ActionBarPopupThemeOverlay" parent="ThemeOverlay.AppCompat.Light" >
<!-- Color of Toolbar Popup from Action Overflow -->
<!--
<item name="android:background">@android:color/holo_green_light</item>
<item name="android:textColor">@android:color/holo_green_dark</item>
-->
</style>
<!-- ************************************************************** -->
</resources>
答案 0 :(得分:2)
解决方案是使用最新版本的 android-support-v4,android-support-v13,android-support-v7库。
答案 1 :(得分:0)
也许这会帮助一些人
在我的情况下,我没有使用.NoActionBar主题。我只是从这个项目中删除了android前缀。
<item name="windowActionBar">false</item>
也可以使用其他项目。 另外我使用android studio和gradle shot为你
'com.android.support:appcompat-v7:22.2.0'
幸运的是,错误消失了。