Could not initialize class android.support.design.

2019-01-19 10:08发布

I am trying to use the CoordinatorLayout from the new Android design library. I added the package to gradle; then I try to use the layout I get this error:

java.lang.NoClassDefFoundError: Could not initialize class android.support.design.widget.CoordinatorLayout
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at org.jetbrains.android.uipreview.ViewLoader.createNewInstance(ViewLoader.java:413)
at org.jetbrains.android.uipreview.ViewLoader.loadView(ViewLoader.java:105)
at com.android.tools.idea.rendering.LayoutlibCallback.loadView(LayoutlibCallback.java:177)
at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:207)
at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:132)
at android.view.LayoutInflater.inflate(LayoutInflater.java:482)
at android.view.LayoutInflater.inflate(LayoutInflater.java:385)
at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:400)
at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:332)
at com.android.ide.common.rendering.LayoutLibrary.createSession(LayoutLibrary.java:350)
at com.android.tools.idea.rendering.RenderTask$2.compute(RenderTask.java:497)
at com.android.tools.idea.rendering.RenderTask$2.compute(RenderTask.java:485)
at com.intellij.openapi.application.impl.ApplicationImpl.runReadAction(ApplicationImpl.java:894)
at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:485)
at com.android.tools.idea.rendering.RenderTask.render(RenderTask.java:590)
at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager.doRender(AndroidLayoutPreviewToolWindowManager.java:644)
at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager.access$1700(AndroidLayoutPreviewToolWindowManager.java:79)
at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager$7$1.run(AndroidLayoutPreviewToolWindowManager.java:586)
at com.intellij.openapi.progress.impl.CoreProgressManager$2.run(CoreProgressManager.java:152)
at com.intellij.openapi.progress.impl.CoreProgressManager.registerIndicatorAndRun(CoreProgressManager.java:452)
at com.intellij.openapi.progress.impl.CoreProgressManager.executeProcessUnderProgress(CoreProgressManager.java:402)
at com.intellij.openapi.progress.impl.ProgressManagerImpl.executeProcessUnderProgress(ProgressManagerImpl.java:54)
at com.intellij.openapi.progress.impl.CoreProgressManager.runProcess(CoreProgressManager.java:137)
at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager$7.run(AndroidLayoutPreviewToolWindowManager.java:581)
at com.intellij.util.ui.update.MergingUpdateQueue.execute(MergingUpdateQueue.java:320)
at com.intellij.util.ui.update.MergingUpdateQueue.execute(MergingUpdateQueue.java:310)
at com.intellij.util.ui.update.MergingUpdateQueue$2.run(MergingUpdateQueue.java:254)
at com.intellij.util.ui.update.MergingUpdateQueue.flush(MergingUpdateQueue.java:269)
at com.intellij.util.ui.update.MergingUpdateQueue.flush(MergingUpdateQueue.java:227)
at com.intellij.util.ui.update.MergingUpdateQueue.run(MergingUpdateQueue.java:217)
at com.intellij.util.concurrency.QueueProcessor.runSafely(QueueProcessor.java:238)
at com.intellij.util.Alarm$Request$1.run(Alarm.java:351)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:439)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
at java.lang.Thread.run(Thread.java:695)

Anybody have a fix for this? Thank you.

8条回答
放我归山
2楼-- · 2019-01-19 10:31

Try to add these dependencies

compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-01-19 10:34

Edit your code into build.gradle file as below.

compileSdkVersion 24

buildToolsVersion "24.0.2"

compile 'com.android.support:appcompat-v7:24.2.1'

compile 'com.android.support:design:24.2.1'

testCompile 'junit:junit:4.12'

查看更多
放荡不羁爱自由
4楼-- · 2019-01-19 10:37
  1. First update android studio to latest version (beta) 1.3 beta version. [Using menu option 'Check for updates' from android studio]

  2. Render your preview using Android M preview.

enter image description here

If you are not able to find the beta update change android studio settings(Updates), then update that patch and render with M preview

enter image description here

查看更多
来,给爷笑一个
5楼-- · 2019-01-19 10:50

may be dependency error. dependencies must be like

dependencies {

    compile 'com.android.support:recyclerview-v7:22.2.0'
    compile 'com.android.support:appcompat-v7:22.2.0'
    compile 'com.android.support:design:22.2.0'
}
查看更多
等我变得足够好
6楼-- · 2019-01-19 10:51

To display a sketch you need to delete a line

<tools:showIn="@layout/activity_main"> 

from main RelativeLayout

查看更多
兄弟一词,经得起流年.
7楼-- · 2019-01-19 10:52

I ran into it using the latest Mac OS X, latest SDK and latest Android Studio. All of them are "stable" versions. Shame on Google testers.

How I fixed it:

  1. From menu: Tools > Android > SDK manager
  2. In the SDK Manager, check Android 5.0.1 (API 21). Perhaps any other will do.
  3. Click "Show package details" at the bottom. Under Android 5.0.1, also check "Google APIs" and "Intel x86 Atom System Image" (why not? if we'll develop for the 21th API)
  4. Click OK and wait for the components to download.
  5. Like already mentioned in other answers, choose to render the layout with it:

Choose to render layout with the Android API level 21

Problem solved!

查看更多
登录 后发表回答