Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Navigation Drawer - Drawer Layout cannot be instantiated

I am trying to implement a Navigation Drawer in my App but I am having serious trouble with it. Right now my Problem is that I want to put my MainContent inside a Drawer Layout like this:

<android.support.v4.widget.DrawerLayout
    android:id = "@+id/drawer_layout"
    xmlns:android = "http://schemas.android.com/apk/res/android"
    android:layout_width = "match_parent"
    android:layout_height = "match_parent"
    >

    <FrameLayout
      [...]
    </FrameLayout>
</android.support.v4.widget.DrawerLayout>

But I get this error code

The following classes could not be instantiated: - android.support.v4.widget.DrawerLayout (Open Class, Show Exception)

These are the exception details:

java.lang.ClassNotFoundException: android.support.v4.widget.DrawerLayoutImpl
    at com.intellij.util.lang.UrlClassLoader.findClass(UrlClassLoader.java:148)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:642)
    at com.intellij.util.lang.UrlClassLoader._defineClass(UrlClassLoader.java:195)
    at com.intellij.util.lang.UrlClassLoader.defineClass(UrlClassLoader.java:191)
    at com.intellij.util.lang.UrlClassLoader.findClass(UrlClassLoader.java:152)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at com.android.tools.idea.rendering.RenderClassLoader.loadClassFromJar(RenderClassLoader.java:78)
    at org.jetbrains.android.uipreview.ProjectClassLoader.loadClassFromModuleOrDependency(ProjectClassLoader.java:108)
    at org.jetbrains.android.uipreview.ProjectClassLoader.load(ProjectClassLoader.java:89)
    at com.android.tools.idea.rendering.RenderClassLoader.findClass(RenderClassLoader.java:56)
    at org.jetbrains.android.uipreview.ProjectClassLoader.findClass(ProjectClassLoader.java:56)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at org.jetbrains.android.uipreview.ViewLoader.loadClass(ViewLoader.java:173)
    at org.jetbrains.android.uipreview.ViewLoader.loadView(ViewLoader.java:96)
    at com.android.tools.idea.rendering.LayoutlibCallback.loadView(LayoutlibCallback.java:172)
    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:401)
    at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:329)
    at com.android.ide.common.rendering.LayoutLibrary.createSession(LayoutLibrary.java:332)
    at com.android.tools.idea.rendering.RenderService$3.compute(RenderService.java:575)
    at com.android.tools.idea.rendering.RenderService$3.compute(RenderService.java:564)
    at com.intellij.openapi.application.impl.ApplicationImpl.runReadAction(ApplicationImpl.java:932)
    at com.android.tools.idea.rendering.RenderService.createRenderSession(RenderService.java:564)
    at com.android.tools.idea.rendering.RenderService.render(RenderService.java:691)
    at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager.doRender(AndroidLayoutPreviewToolWindowManager.java:604)
    at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager.access$1900(AndroidLayoutPreviewToolWindowManager.java:80)
    at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager$7$1.run(AndroidLayoutPreviewToolWindowManager.java:546)
    at com.intellij.openapi.progress.impl.ProgressManagerImpl$2.run(ProgressManagerImpl.java:178)
    at com.intellij.openapi.progress.ProgressManager.executeProcessUnderProgress(ProgressManager.java:209)
    at com.intellij.openapi.progress.impl.ProgressManagerImpl.executeProcessUnderProgress(ProgressManagerImpl.java:212)
    at com.intellij.openapi.progress.impl.ProgressManagerImpl.runProcess(ProgressManagerImpl.java:171)
    at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager$7.run(AndroidLayoutPreviewToolWindowManager.java:541)
    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:327)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

I follow this guide http://developer.android.com/training/implementing-navigation/nav-drawer.html But till now it does not work at all. Maybe somebody has a better tutorial on how to implement a navigation drawer properly with Android 5.0??

Thanks in advance for any help.

Sincerely John

like image 602
JRsz Avatar asked Oct 23 '14 14:10

JRsz


1 Answers

Got here with the same problem, first of all, pay attention to what Christer Nordvik commented:

Think it is a bug in the latest tools version: https://code.google.com/p/android/issues/detail?id=78090

We have the same in the designer, but when running it is fine. Worked without issues before the API level 21 release of the SDK.

As I see it you have 4 options:


Option 1: go back to Android pre 5.0 Use and compile with your code with SDK 20

Change build.gradle to the following:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 20
    buildToolsVersion "20.0.0"

    defaultConfig {
        applicationId "com.example.yourapp"
        minSdkVersion 8
        targetSdkVersion 20
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:support-v4:20.0.0'
}

change the following lines to what ever corresponds to your project:

        applicationId "com.example.yourapp"
        minSdkVersion 8

Most likely you will also need to change "res\v21\styles.xml" from:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="AppTheme" parent="android:Theme.Material.Light">
    </style>
</resources>

to:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="AppTheme" parent="android:Theme.Holo.Light">
    </style>
</resources>

As the material themes are introduced on Lollipop / Android 5.0

Sync your project with the new gradle file, wait for the syncing to finish and you are good to go, you should now see the layout preview

Sync your project with Gradle Files


Option 2: update your code in text

Code will compile and work just fine, just update your code in text, no previews. Instead do changes and try them on a emulator or an actual Android device.


Option 3: combine the first 2 options

Use option 1, do what ever you need to do to the layouts (as long as it doesn't needs nothing specific from SDK 21) and when done revert changes done to build.gradle and styles.xml so you can enjoy the new eye candy of Lollipop, just test it on an emulator or device as suggested on option 2


Option 4: wait they fix the bug

just that.


Long shot extra option:

If you have installed a stable build of Android Studio, try a Beta or Canary, who knows, may be there is a patch implemented to solve that issue on one of them (I have a stable build, so I don't know). Or try on Eclipse

like image 69
lgiusti84 Avatar answered Sep 30 '22 00:09

lgiusti84