android.view.View $ OnUnhandledKeyEventListener

时间:2018-08-21 23:22:44

标签: android android-toolbar keyevent unhandled

我是Android Studio的新手,我不明白为什么https://developer.android.com/training/appbar/setting-up中没有显示我的工具栏 我知道在stackoverflow上已经存在其他一些问题,例如我的问题,但是它们在我的项目中不起作用。因此,我非常感谢您解决此问题。截图:

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(应用程序级别)

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>

9 个答案:

答案 0 :(得分:11)

如果使用Android X和androidx.core:core和/或androidx.appcompat:appcompat,请将androidx.core:core更新为1.4.0-alpha01和/或将androidx.appcompat:appcompat更新为1.3.0-alpha01

答案 1 :(得分:10)

在您的 build.gradle(应用级别)中进行更改,以将其降级到以下版本27。然后同步并构建项目。希望它将解决该问题。

compileSdkVersion 27

targetSdkVersion 27

implementation 'com.android.support:appcompat-v7:27.1.1'

答案 2 :(得分:5)

documentation所述,首先,您的Activity应该从AppCompatActivity()扩展。

第二,在您的项目styles中,已设置Base.Theme.AppCompat.Light.DarkActionBar,这意味着它确实有一个DarkActionBar,但是您在Toolbar中已经有一个layout。因此,只需更改:

Base.Theme.AppCompat.Light.DarkActionBar

收件人:

Theme.AppCompat.Light.NoActionBar

然后(在java-kotlin端),设置Toolbar

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

并给Toolbar一个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"/>

那你应该很好。

答案 3 :(得分:1)

这不是错误,但是警告可以通过在我的情况下在文件的 build.gradle (应用程序级别)中添加以下代码来忽略。然后同步并构建项目。希望它可以解决该问题。

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

答案 4 :(得分:1)

  

https://github.com/DonaldDu/FixUnhandledEvent

在api28中添加了

android.view.View $ OnUnhandledKeyEventListener。

如果运行时设备低于28,则找不到该类,但运行时没有致命错误。

我们可以在调试模式下注入该类,以忽略错误消息。

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'

答案 5 :(得分:1)

降级至 sdk 27​​ ,因为另一篇文章建议给我带来一系列新问题。 参见:Android - resource linking failed / failed linking references

在我的build.gradle(应用程序级别)中添加一个条目为我解决了这个问题。

dependencies {
    ...

    implementation 'androidx.core:core:1.4.0-alpha01'
    ...
}

在这里,我有了尝试的想法: https://github.com/android/sunflower/issues/295#issuecomment-649630057

答案 6 :(得分:0)

库〜-v7:28.0.0存在问题。有两种解决方案,您可以将compile和targetSdkVersion下载到27,也可以将Android Studio升级到最新版本(3.2.x更高)。

答案 7 :(得分:0)

将multidex支持库添加到您的依赖项中

com.android.support:multidex:1.0.3

答案 8 :(得分:-1)

您可以轻松修复它,

conn = mysql.connector.connect(host='127.0.0.1',user=username, passwd=password,db=database_name)

,然后在值下打开style.xml并更改为

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