使用AppCompatActivity而不是Actionbar时出现运行时错误

时间:2015-08-06 07:27:54

标签: android runtime-error material-design appcompatactivity

我正在使用AppCompatActivity库来支持所有设备。在提升android-support-v7-appcompat库后,我面临问题。但我在运行期间遇到错误。我正在使用eclipse studio来编写android项目代码。任何帮助将不胜感激。

以下是我的错误,我不知道我哪里出错。

**错误**

     E/AndroidRuntime(6899): FATAL EXCEPTION: main
     E/AndroidRuntime(6899): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.drawer.sample/com.drawer.sample.MainActivity}: android.view.InflateException: Binary XML file line #19: Error inflating class android.support.v7.internal.widget.NativeActionModeAwareLayout
     E/AndroidRuntime(6899):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
     E/AndroidRuntime(6899):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
     E/AndroidRuntime(6899):    at android.app.ActivityThread.access$800(ActivityThread.java:144)
     E/AndroidRuntime(6899):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
     E/AndroidRuntime(6899):    at android.os.Handler.dispatchMessage(Handler.java:102)
     E/AndroidRuntime(6899):    at android.os.Looper.loop(Looper.java:135)
     E/AndroidRuntime(6899):    at android.app.ActivityThread.main(ActivityThread.java:5221)
     E/AndroidRuntime(6899):    at java.lang.reflect.Method.invoke(Native Method)
     E/AndroidRuntime(6899):    at java.lang.reflect.Method.invoke(Method.java:372)
     E/AndroidRuntime(6899):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
     E/AndroidRuntime(6899):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     E/AndroidRuntime(6899): Caused by: android.view.InflateException: Binary XML file line #19: Error inflating class android.support.v7.internal.widget.NativeActionModeAwareLayout
     E/AndroidRuntime(6899):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:757)
     E/AndroidRuntime(6899):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
     E/AndroidRuntime(6899):    at android.view.LayoutInflater.parseInclude(LayoutInflater.java:890)
     E/AndroidRuntime(6899):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:802)
     E/AndroidRuntime(6899):    at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
     E/AndroidRuntime(6899):    at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
     E/AndroidRuntime(6899):    at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
     E/AndroidRuntime(6899):    at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:299)
     E/AndroidRuntime(6899):    at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:246)
     E/AndroidRuntime(6899):    at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
     E/AndroidRuntime(6899):    at com.drawer.sample.MainActivity.onCreate(MainActivity.java:12)
     E/AndroidRuntime(6899):    at android.app.Activity.performCreate(Activity.java:5933)
     E/AndroidRuntime(6899):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
     E/AndroidRuntime(6899):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
     E/AndroidRuntime(6899):    ... 10 more
     E/AndroidRuntime(6899): Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.v7.internal.widget.NativeActionModeAwareLayout" on path: DexPathList[[zip file "/data/app/com.drawer.sample-2/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
     E/AndroidRuntime(6899):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
     E/AndroidRuntime(6899):    at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
     E/AndroidRuntime(6899):    at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
     E/AndroidRuntime(6899):    at android.view.LayoutInflater.createView(LayoutInflater.java:571)
     E/AndroidRuntime(6899):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
     E/AndroidRuntime(6899):    ... 23 more
     E/AndroidRuntime(6899):    Suppressed: java.lang.ClassNotFoundException: android.support.v7.internal.widget.NativeActionModeAwareLayout
     E/AndroidRuntime(6899):        at java.lang.Class.classForName(Native Method)
     E/AndroidRuntime(6899):        at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
     E/AndroidRuntime(6899):        at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
     E/AndroidRuntime(6899):        at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
     E/AndroidRuntime(6899):        ... 26 more
     E/AndroidRuntime(6899):    Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available

activity_main.xml中

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:tools="http://schemas.android.com/tools"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:orientation="vertical">
       <include 
         android:id="@+id/toolbar"
         layout="@layout/toolbar_layout"/> 
       <TextView
         android:text="@string/app_name" 
         android:layout_height="wrap_content"
         android:layout_width="wrap_content"
         android:textColor="#000000"
         android:layout_marginTop="110dp"/>
    </LinearLayout>

MainActivty.java

    package com.drawer.sample;

    import android.os.Bundle;
    import android.support.v7.widget.Toolbar;
    import android.support.v7.app.AppCompatActivity;

   public class MainActivity extends AppCompatActivity  {

     @Override
     protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);

     Toolbar mToolbar =  (Toolbar) findViewById(R.id.toolbar);
     setSupportActionBar(mToolbar);
     getSupportActionBar().setDisplayShowTitleEnabled(false);

     }
 }

Style.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <style name="AppTheme" parent="AppTheme.Base">

   </style>

   <style name="AppTheme.Base" parent = "Theme.AppCompat.Light.DarkActionBar">
     <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primaryDark</item>
    <item name="colorAccent">@color/accent</item>
</style>

4 个答案:

答案 0 :(得分:3)

即使是一个老问题,但我遇到了同样的问题,我只是通过清理重建项目来修复它

  

建设 - &GT;清洁项目

然后运行项目

答案 1 :(得分:1)

ActionBar在材料设计中用作工具栏。所以添加没有操作栏的主题并使用 以下是在xml中使用工具栏。

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:iosched="http://schemas.android.com/apk/res-auto"
style="@style/HeaderBar"
iosched:theme="@style/ActionBarThemeOverlay"
iosched:popupTheme="@style/ActionBarPopupThemeOverlay"
android:id="@+id/toolbar_actionbar"
iosched:titleTextAppearance="@style/ActionBar.TitleText"
iosched:contentInsetStart="?actionBarInsetStart"
android:layout_width="match_parent"
android:layout_height="?actionBarSize" />

答案 2 :(得分:1)

我找到了可能错误的解决方案,我必须添加implements的{​​{1}}覆盖方法。下面是我修复它的代码。

appCompatActivity

我可以添加状态栏以及操作栏。 :)

enter image description here

答案 3 :(得分:0)

您是否曾尝试在gradle文件中添加此代码示例?

   android {
         configurations {
    all*.exclude group: 'com.android.support', module: 'support-v4'
    all*.exclude group: 'com.android.support', module: 'support-annotations'
} }