gpt4 book ai didi

java - 应用程序在 Android Studio 中崩溃。 getMenuInflater().inflate(R.menu.activity_main, 菜单);不存在?

转载 作者:行者123 更新时间:2023-12-01 17:17:27 29 4
gpt4 key购买 nike

Bluetooth Application Starter Code

大家好!我正在做一个项目,需要访问Android的蓝牙API。我正在 Android Studio 中编码,并按照上面的链接开始。从网站复制代码后,Main Activity 出现错误。 getMenuInflater().inflate(R.menu.activity_main, menu);由于某种原因,菜单不存在。为了解决这个问题,我创建了一个名为 menu 的新 xml 文件并添加了以下代码

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item
android:id="@+id/action_settings"
android:enabled="false"
android:orderInCategory="100"
android:title="Settings"
android:visible="true"
app:showAsAction="never" />

</menu>

然后,我将主 Activity 中的内容更改为 getMenuInflater().inflate(R.menu.menu, menu); 并且错误消失了。不幸的是,无论如何,应用程序都崩溃了,我不知道如何解决这个问题。我尝试使用模拟器和实际的 Android 设备进行测试。有人有什么想法吗?这是 logcat 中的错误

2020-04-21 18:59:53.748 30843-30843/? I/art: Late-enabling -Xcheck:jni
2020-04-21 18:59:53.773 30843-30849/? I/art: Debugger is no longer active
2020-04-21 18:59:53.773 30843-30849/? I/art: Starting a blocking GC Instrumentation
2020-04-21 18:59:53.810 30843-30843/? W/System: ClassLoader referenced unknown path: /data/app/com.example.shareaudio-2/lib/arm
2020-04-21 18:59:53.845 30843-30843/? D/AndroidRuntime: Shutting down VM
2020-04-21 18:59:53.847 30843-30843/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.shareaudio, PID: 30843
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.shareaudio/com.example.shareaudio.MainActivity}: android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class android.support.constraint.ConstraintLayout
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2678)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2743)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1490)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6165)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)
Caused by: android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class android.support.constraint.ConstraintLayout
Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class android.support.constraint.ConstraintLayout
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.constraint.ConstraintLayout" on path: DexPathList[[zip file "/data/app/com.example.shareaudio-2/base.apk"],nativeLibraryDirectories=[/data/app/com.example.shareaudio-2/lib/arm, /system/lib, /vendor/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at android.view.LayoutInflater.createView(LayoutInflater.java:609)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:787)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:412)
at android.app.Activity.setContentView(Activity.java:2418)
at com.example.shareaudio.MainActivity.onCreate(MainActivity.java:24)
at android.app.Activity.performCreate(Activity.java:6687)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1140)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2631)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2743)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1490)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6165)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)

最佳答案

在我看来,ConstraintLayout 支持库尚未添加到类路径中。

在您的 build.gradle 中尝试添加这些依赖项(可能使用较新的版本):

    implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:design:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'

关于java - 应用程序在 Android Studio 中崩溃。 getMenuInflater().inflate(R.menu.activity_main, 菜单);不存在?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61355057/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com