扩展类android.support.design.widget.CoordinatorLayout和ClassNotFoundException时出错:android.support.design.R $ styleable

时间:2015-09-29 07:52:18

标签: android android-layout android-fragments

我正在开发一款具有带滑动功能的Tab的应用。我正在使用Eclipse进行开发。我已通过Property将support design library导入为Library。在运行期间,我不断收到此错误" Error inflating class android.support.design.widget.CoordinatorLayout"。

  1. 首先我添加了android support design.jar并尝试了,这是行不通的
  2. 我添加了class.jar support design library文件{J} ava build path support design library
  3. 最后,我使用Properties->Android->Add Library导入Theme.AppCompat.Light.DarkActionBar作为库,这也无效
  4. 我使用父主题为&#34; <style name="MyMaterialTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">&#34;
  5. &#34; AppCompatActivity&#34;

    1. 我正在扩展import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.widget.Button; import com.example.scrollingtab.R; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Toolbar toolbar; private Button btnSimpleTabs, btnScrollableTabs, btnIconTextTabs, btnIconTabs, btnCustomIconTextTabs; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); btnSimpleTabs = (Button) findViewById(R.id.btnSimpleTabs); btnSimpleTabs.setOnClickListener(this); } ,请参阅我的MainActivity.Java

      package com.example.scrollingtab.activity;

      FATAL EXCEPTION: main
      Process: com.example.scrollingtab, PID: 1348
      java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.scrollingtab/com.example.scrollingtab.activity.MainActivity}: android.view.InflateException: Binary XML file line #1: Error inflating class android.support.design.widget.CoordinatorLayout
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
      at android.app.ActivityThread.access$800(ActivityThread.java:151)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
      at android.os.Handler.dispatchMessage(Handler.java:102)
      at android.os.Looper.loop(Looper.java:135)
      at android.app.ActivityThread.main(ActivityThread.java:5257)
      at java.lang.reflect.Method.invoke(Native Method)
      at java.lang.reflect.Method.invoke(Method.java:372)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
      Caused by: android.view.InflateException: Binary XML file line #1: Error inflating class android.support.design.widget.CoordinatorLayout
      at android.view.LayoutInflater.createView(LayoutInflater.java:633)
      at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
      at android.view.LayoutInflater.inflate(LayoutInflater.java:482)
      at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
      at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
      at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:257)
      at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
      at com.example.scrollingtab.activity.MainActivity.onCreate(MainActivity.java:20)
      at android.app.Activity.performCreate(Activity.java:5990)
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
      Caused by: java.lang.reflect.InvocationTargetException
      at java.lang.reflect.Constructor.newInstance(Native Method)
      at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
      at android.view.LayoutInflater.createView(LayoutInflater.java:607)
      Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/design/R$styleable;
      at android.support.design.widget.CoordinatorLayout.<init>(CoordinatorLayout.java:171)
      at android.support.design.widget.CoordinatorLayout.<init>(CoordinatorLayout.java:165)
      Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.design.R$styleable" on path: DexPathList[[zip file "/data/app/com.example.scrollingtab-2/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
      at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
      Suppressed: java.lang.ClassNotFoundException: android.support.design.R$styleable
      at java.lang.Class.classForName(Native Method)
      at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
      at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
      Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available
      
    2. 以下是完整的堆栈跟踪:

      {{1}}

      我已经尝试过SO谈论和提供的大部分内容。我仍然无法追踪我的计划中的错误。

      需要帮助来解决这个问题。

      由于

5 个答案:

答案 0 :(得分:17)

对于android studio尝试添加这些依赖项

BaseAdapter

答案 1 :(得分:11)

对我来说,此错误是由于使用了某些 androidx 库而导致的,但随后在某些layout.xml文件中仍具有旧的坐标布局。

build.gradle

com.android.support:coordinatorlayout-> androidx.coordinatorlayout:coordinatorlayout:1.0.0-alpha1

如果您使用的是androidx,那么也许也是您的问题。 我还必须更新其他v7库,例如Toolbar

在此处查看androidx迁移指南:https://developer.android.com/topic/libraries/support-library/refactor

答案 2 :(得分:5)

在迁移到AndroidX时出现此错误

要解决,我做到了:

  1. ,如@Mikeumus所建议-并将其添加到模块的build.gradle依赖项中:

    实现“ androidx.coordinatorlayout:coordinatorlayout:latest.version.here”

  2. 检入Layout XML文件(您尝试使用 setContentView

  3. 启动的布局)

如果布局包含: << strong> android.support.design.widget.CoordinatorLayout ...

替换为: <androidx.coordinatorlayout.widget.CoordinatorLayout

  1. 在同一布局文件中-替换结束标记

</android.support.design.widget.CoordinatorLayout>

使用

</androidx.coordinatorlayout.widget.CoordinatorLayout>

答案 3 :(得分:3)

我找到了eclipse的解决方案

我遇到的问题是{/ 1}}文件未在gen /文件夹中的 support.design 下生成。

执行以下步骤后,生成R.Java文件。主要问题是设计库目标为22.更改为23工作。

  1. 支持设计库R.Java导入到eclipse工作区。

  2. 打开 AppCompat 设计库的\sdk\extras\android\support\design并将其定位到 23

  3. 清除 Appcompat 设计您的项目

  4. 运行应用

答案 4 :(得分:0)

确保在您的活动中设置主题AppCompat

<activity
            android:name=".newApp.screens.main.MainActivity"
            android:label="@string/title_activity_main_launch"
            android:theme="@style/AppThemes"></activity>

  <!-- Base application theme. -->
<style name="AppThemes" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">#673AB7</item>
    <item name="colorPrimaryDark">#512DA8</item>
    <item name="colorAccent">#00BCD4</item>
</style>