Android 工具栏中的错误

Error in Android Toolbar

我在 android 中使用工具栏,它在棒棒糖版本中工作正常,但不幸的是它在旧版本中不起作用。

工具栏xml:

 <?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar                                                               xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
local:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
local:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

Java class:

public class MainAct extends AppCompatActivity{
Toolbar mtoolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
     setContentView(R.layout.act_main);
    mtoolbar=(Toolbar)findViewById(R.id.toolbar);
    setSupportActionBar(mtoolbar);

}

样式:

<resources>
<style name="MyMaterialTheme" parent="MyMaterialTheme.Base">
</style>

<style name="MyMaterialTheme.Base" parent="Theme.AppCompat.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>
</resources>

Gradle:

    apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
    applicationId "nidhinkumar.reccs"
    minSdkVersion 15
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}  
  }dependencies {
      compile fileTree(dir: 'libs', include: ['*.jar'])
      compile 'com.android.support:appcompat-v7:24.0.0-alpha1'
    }

我已尝试将样式更改为无操作栏,但该应用程序仍无法在旧设备上运行,并且出现以下错误。

java.lang.RuntimeException: Unable to start activity ComponentInfo{nidhinkumar.reccs/nidhinkumar.reccs.MainAct}: android.view.InflateException: Binary XML file line #2: Error inflating class android.support.v7.widget.Toolbar
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
        at android.app.ActivityThread.access0(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
        at android.os.Handler.dispatchMessage(Handler.java:110)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:5299)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class android.support.v7.widget.Toolbar
        at android.view.LayoutInflater.createView(LayoutInflater.java:620)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
        at android.view.LayoutInflater.parseInclude(LayoutInflater.java:816)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:745)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
        at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:267)
        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:130)
        at nidhinkumar.reccs.MainAct.onCreate(MainAct.java:15)
        at android.app.Activity.performCreate(Activity.java:5286)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)

在您的 xml 文件中添加此代码:

<include
android:id="@+id/tool_bar"
layout="@layout/actionbar_main"></include>

改变这个:

android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"

收件人:

android:minHeight="@dimen/actionBarSize"
android:background="@color/colorPrimary"
Try this not so sure may be because of your compileSdkVersion is higher,
change compile 'com.android.support:appcompat-v7:24.0.0-alpha1' to

compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:support-v4:23.1.1'
compile 'com.android.support:design:23.1.1'

if this does not work, change your build.gradle(Project) classPath 
to a lower varsion(say 1.3.0 or 1.3.1)
compile 'com.android.support:design:23.1.1'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:support-v4:23.1.1'
  • 这些库提供新功能的向后兼容版本, 提供框架中未包含的有用 UI 元素。所以 对于低于 Lollipop 的设备,这些功能不包括在内。所以这些 支持库使 Lollipop 以下版本成为可能。