android.view.View$OnUnhandledKeyEventListener
Asked Answered
C

9

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>
Callaghan answered 21/8, 2018 at 23:22 Comment(3)
can you paste your activity source in which you are using this layout containing toolbar?Seismograph
The Layout it self isnt working correctly. When I open the xml file in res folder it isnt shown in previewCallaghan
Possible duplicate of android/view/view$onUnhandledKeyEventListener (onMeasure error)Dennard
S
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'
Struthious answered 28/9, 2018 at 10:27 Comment(7)
It was the best solution available at the time, but now there's a better one: upgrade to Android Studio 3.2.Melville
@TatiOverflow if its not solutions ,you have to tell correct solutionsLetterpress
is it a danger usage? targetsdk 28 and appcompat 27. Is it a conflict for the app?Morven
@Melville This is still happening to me with Android Studio 3.4.1Dulcedulcea
@Dulcedulcea I'm sorry to hear that, but unfortunately I don't have any other ideas.Melville
Downgrading is not an solution anymore. I'm using Android Studio 3.6.3 and still experiencing this problem.Piled
I got the same issue and it has been fixed by your solution. However, I'm using AndroidX libraries and after setting the compileSdkVersion to 27, it got the error "> Task :app:processDebugResources FAILED AGPBI: {"kind":"error","text":"Android resource linking failed" ... " unless I set the compileSdkVersion to 28 or higher. I'm new to Android Studio by the way!Balneology
G
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

Gustafsson answered 3/6, 2020 at 11:43 Comment(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.Emerick
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.Tombaugh
S
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'
Struthious answered 28/9, 2018 at 10:27 Comment(7)
It was the best solution available at the time, but now there's a better one: upgrade to Android Studio 3.2.Melville
@TatiOverflow if its not solutions ,you have to tell correct solutionsLetterpress
is it a danger usage? targetsdk 28 and appcompat 27. Is it a conflict for the app?Morven
@Melville This is still happening to me with Android Studio 3.4.1Dulcedulcea
@Dulcedulcea I'm sorry to hear that, but unfortunately I don't have any other ideas.Melville
Downgrading is not an solution anymore. I'm using Android Studio 3.6.3 and still experiencing this problem.Piled
I got the same issue and it has been fixed by your solution. However, I'm using AndroidX libraries and after setting the compileSdkVersion to 27, it got the error "> Task :app:processDebugResources FAILED AGPBI: {"kind":"error","text":"Android resource linking failed" ... " unless I set the compileSdkVersion to 28 or higher. I'm new to Android Studio by the way!Balneology
A
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.

Amado answered 25/8, 2018 at 12:39 Comment(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 OPFirecrest
Changing Theme.AppCompat.Light.DarkActionBar removed the error for my activity_main but broke my settings_activityMeat
I
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'
Inquietude answered 16/8, 2019 at 2:7 Comment(1)
A bit of explanation would be more than welcome when answering a so popular question.Platto
A
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

Addie answered 14/3, 2019 at 12:47 Comment(0)
P
4

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

Priestley answered 11/9, 2020 at 21:44 Comment(1)
Adding the suggested Gradle dependency fixed it for me. Latest version of AndroidX Core can be found here: developer.android.com/jetpack/androidx/releases/coreCanaday
P
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).

Portaltoportal answered 30/11, 2018 at 8:46 Comment(1)
Nope. I have Android Studio 3.2.1 and still see this error.Rijeka
L
0

Add multidex support library to your dependencies

com.android.support:multidex:1.0.3
Lynnet answered 3/7, 2019 at 17:58 Comment(0)
S
-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">
Silicon answered 4/6, 2019 at 11:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.