66

I am new to Android Studio and I don't get why my toolbar isn't shown as described by https://developer.android.com/training/appbar/setting-up I know there are already some other questions like mine on stackoverflow but they don't work at my project. Therefore I would be very thankful for fixing this issue. Screenshot:

Screenshot

java.lang.ClassNotFoundException: android.view.View$OnUnhandledKeyEventListener
at org.jetbrains.android.uipreview.ModuleClassLoader.load(ModuleClassLoader.java:180)
at com.android.tools.idea.rendering.RenderClassLoader.findClass(RenderClassLoader.java:61)
at org.jetbrains.android.uipreview.ModuleClassLoader.findClass(ModuleClassLoader.java:118)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at org.jetbrains.android.uipreview.ModuleClassLoader.loadClass(ModuleClassLoader.java:213)
at android.support.v7.widget.ViewUtils.isLayoutRtl(ViewUtils.java:58)
at android.support.v7.widget.Toolbar.onMeasure_Original(Toolbar.java:1578)
at android.support.v7.widget.Toolbar.onMeasure(Toolbar.java)
at android.view.View.measure_Original(View.java:22071)
at android.view.View_Delegate.measure(View_Delegate.java:80)
at android.view.View.measure(View.java:22035)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6602)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure_Original(View.java:22071)
at android.view.View_Delegate.measure(View_Delegate.java:80)
at android.view.View.measure(View.java:22035)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
at android.view.View.measure_Original(View.java:22071)
at android.view.View_Delegate.measure(View_Delegate.java:80)
at android.view.View.measure(View.java:22035)
at com.android.layoutlib.bridge.impl.RenderSessionImpl.measureView(RenderSessionImpl.java:590)
at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:343)
at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:384)
at com.android.tools.idea.layoutlib.LayoutLibrary.createSession(LayoutLibrary.java:193)
at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:544)
at com.android.tools.idea.rendering.RenderTask.lambda$inflate$3(RenderTask.java:678)
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)

toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimaryDark">

</android.support.v7.widget.Toolbar>

build.gradle (app level)

apply plugin: 'com.android.application'
android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.david.gamebase"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0-rc01'
    implementation 'com.android.support.constraint:constraint-layout:1.1.2'
    implementation 'com.google.firebase:firebase-auth:11.6.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

apply plugin: 'com.google.gms.google-services'

style.xml:

   <resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

</resources>

## toolbar.xml ##

    <?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimaryDark">

</android.support.v7.widget.Toolbar>
3
16

If using Android X and androidx.core:core and/or androidx.appcompat:appcompat, update androidx.core:core to 1.4.0-alpha01 and/or androidx.appcompat:appcompat to 1.3.0-alpha01

2
  • This worked for me. In my case I was using MaterialToolbar, which seemed to be the one causing the issue. I am only using androidx.appcompat:appcompat, and I updated from 1.1.0 to 1.3.0-alpha01 as mentioned. The issue was solved after syncing the project.
    – FraK
    Jul 27 '20 at 20:57
  • 1
    Updating to 'com.google.android.material:material:1.4.0' worked for me. I was with version 1.3.0, and that version was causing the errors. In this update of material, they have updated androix.core from 1.2.0 to 1.5.0. Jul 5 '21 at 5:57
15

Make the changes in your build.gradle (app level) to downgrade to version 27 as below. Then sync and build project. Hopefully it will fix the issue..

compileSdkVersion 27

targetSdkVersion 27

implementation 'com.android.support:appcompat-v7:27.1.1'
7
  • 4
    It was the best solution available at the time, but now there's a better one: upgrade to Android Studio 3.2.
    – mhsmith
    Nov 16 '18 at 13:37
  • 2
    @TatiOverflow if its not solutions ,you have to tell correct solutions
    – sasikumar
    Mar 13 '19 at 11:26
  • 3
    @mhsmith This is still happening to me with Android Studio 3.4.1
    – Akaisteph7
    Aug 7 '19 at 16:11
  • 1
    @Akaisteph7 I'm sorry to hear that, but unfortunately I don't have any other ideas.
    – mhsmith
    Aug 7 '19 at 20:16
  • 3
    Downgrading is not an solution anymore. I'm using Android Studio 3.6.3 and still experiencing this problem. May 9 '20 at 10:08
7

As mentioned by documentation, first, your Activity should be extend from AppCompatActivity().

Second, in your project styles, Base.Theme.AppCompat.Light.DarkActionBar is set which means it does have a DarkActionBar but you already have a Toolbar in your layout. So, simply, change:

Base.Theme.AppCompat.Light.DarkActionBar

to:

Theme.AppCompat.Light.NoActionBar

Then, (in java-kotlin side), setup the Toolbar:

// Note that the Toolbar defined in the layout has the id "my_toolbar"
    setSupportActionBar(findViewById(R.id.my_toolbar))

And give the Toolbar an id.

<android.support.v7.widget.Toolbar
   android:id="@+id/my_toolbar"
   android:layout_width="match_parent"
   android:layout_height="?attr/actionBarSize"
   android:background="?attr/colorPrimary"
   android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
   app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

Then you should be good to go.

2
  • 2
    the note about changing DarkActionBar to NoActionBar got the UI previewer working for me targeting API 28 and using androidx packages. There was no toolbar in play as far as I know (brand new project generated by AS 3.0.1), but the exception was the rather bizarre "java.lang.ClassNotFoundException: android.view.View$OnUnhandledKeyEventListener" mentioned by OP
    – CCJ
    Oct 10 '18 at 17:05
  • Changing Theme.AppCompat.Light.DarkActionBar removed the error for my activity_main but broke my settings_activity Jan 9 '20 at 2:16
7

https://github.com/DonaldDu/FixUnhandledEvent

android.view.View$OnUnhandledKeyEventListener is add in api28.

If runtime device below 28, can't find the class, but run without fatal error.

We can inject the class in debug mode to ignore error message.

implementation 'com.android.support:appcompat-v7:28.0.0'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
//inject class when api<28 && debug==true
debugImplementation 'com.github.DonaldDu:FixUnhandledEvent:1.0'
1
  • 6
    A bit of explanation would be more than welcome when answering a so popular question.
    – Léa Gris
    Aug 16 '19 at 2:27
4

It's not an error but a warning it can be ignored by the addition of the following code in my build.gradle (app level) the file works in my case. Then sync and build the project. Hopefully, it fixes the issue.

configurations.all {
  resolutionStrategy.eachDependency { DependencyResolveDetails details ->
    def requested = details.requested
    if (requested.group == "com.android.support") {
        if (!requested.name.startsWith("multidex")) {
            details.useVersion "26.+"
        }
    }
  }
}

Reference to the gist

3

Downgrading to sdk 27 as one of the other post suggested came with a new set of problems for me. see: Android - resource linking failed / failed linking references

Adding an entry to my build.gradle (app level) solved the issue for me.

dependencies {
    ...

    implementation 'androidx.core:core:1.5.0-alpha05'
    ...
}

Here's where I got the idea to try this: https://github.com/android/sunflower/issues/295#issuecomment-649630057

1
0

There's problem with library ~-v7:28.0.0. There are two solutions, your can either download compile and targetSdkVersion to 27 or upgrade Android Studio to latest version (3.2.x higher).

1
  • 24
    Nope. I have Android Studio 3.2.1 and still see this error.
    – Gene S
    Dec 22 '18 at 7:48
0

Add multidex support library to your dependencies

com.android.support:multidex:1.0.3
-1

You can fix it easily,

implementation 'com.android.support:appcompat-v7:28.0.0-alpha1'

and Open style.xml under values and change to

style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.