Runtime Error while using AppCompatActivity instead of Actionbar

不问归期 提交于 2020-01-05 01:38:09

问题


I am using the AppCompatActivity library to make support all the devices. After upadting the android-support-v7-appcompat library i am facing issue. But i am facing an error during run-time. I am using eclipse studio to code for android project. Any help will be appreciated.

Below is my error ,and i am getting no idea where i am going wrong.

** ERROR **

     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>


回答1:


ActionBar is used as Toolbar in material design. So add theme with no action bar and use following to use Toolbar in 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:


I found the solution of may error, i have to add the implements override methods of appCompatActivity. Below is the code by which i have fix it.

package com.drawer.sample;

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

public class MainActivity extends AppCompatActivity implements AppCompatCallback {

    @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);

    }

    @Override
    public void onSupportActionModeFinished(ActionMode mode) {
        // TODO Auto-generated method stub
        super.onSupportActionModeFinished(mode);
    }

    @Override
    public void onSupportActionModeStarted(ActionMode mode) {
        // TODO Auto-generated method stub
        super.onSupportActionModeStarted(mode);
    }
}

And i can add the statusbar as well actionbar. :)




回答3:


Have you ever try to add this code sample in your gradle file?

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


来源:https://stackoverflow.com/questions/31849430/runtime-error-while-using-appcompatactivity-instead-of-actionbar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!