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>
Ellen Spertus
  • 6,576
  • 9
  • 50
  • 101
David Schmidt
  • 758
  • 1
  • 5
  • 7
  • can you paste your activity source in which you are using this layout containing toolbar? – Qasim Aug 22 '18 at 01:05
  • The Layout it self isnt working correctly. When I open the xml file in res folder it isnt shown in preview – David Schmidt Aug 22 '18 at 09:11
  • Possible duplicate of [android/view/view$onUnhandledKeyEventListener (onMeasure error)](https://stackoverflow.com/questions/51305964/android-view-viewonunhandledkeyeventlistener-onmeasure-error) – user1506104 Jul 13 '19 at 11:17

9 Answers9

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

jcesarmobile
  • 51,328
  • 11
  • 132
  • 176
  • 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. – lubrum Jul 05 '21 at 05: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'
ʍѳђઽ૯ท
  • 16,646
  • 7
  • 53
  • 108
ArindamC
  • 268
  • 1
  • 5
  • 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
  • is it a danger usage? targetsdk 28 and appcompat 27. Is it a conflict for the app? – zakjma Jul 24 '19 at 13:18
  • 3
    @mhsmith This is still happening to me with Android Studio 3.4.1 – Akaisteph7 Aug 07 '19 at 16:11
  • 1
    @Akaisteph7 I'm sorry to hear that, but unfortunately I don't have any other ideas. – mhsmith Aug 07 '19 at 20:16
  • 3
    Downgrading is not an solution anymore. I'm using Android Studio 3.6.3 and still experiencing this problem. – Anbuselvan Rocky May 09 '20 at 10:08
  • 1
    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! – Nguyen Ha Minh Jun 27 '20 at 03:57
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.

ʍѳђઽ૯ท
  • 16,646
  • 7
  • 53
  • 108
  • 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` – Elijah Mock Jan 09 '20 at 02: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'
Donald Du
  • 71
  • 1
  • 2
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

Pratap Sharma
  • 2,633
  • 2
  • 18
  • 32
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

likejudo
  • 3,396
  • 6
  • 52
  • 107
CrashCodes
  • 3,237
  • 12
  • 38
  • 42
  • 1
    Adding the suggested Gradle dependency fixed it for me. Latest version of AndroidX Core can be found here: https://developer.android.com/jetpack/androidx/releases/core – dazed Feb 02 '21 at 12:06
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).

Junior Frogie
  • 313
  • 1
  • 3
  • 16
0

Add multidex support library to your dependencies

com.android.support:multidex:1.0.3
Daniel
  • 1,432
  • 1
  • 16
  • 31
-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">
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103