简体   繁体   中英

App crashes in Android Studio. getMenuInflater().inflate(R.menu.activity_main, menu); doesn't exist?

Bluetooth Application Starter Code

Hi guys. I am working on a project that needs to access Android's bluetooth API. I'm coding in Android Studio and followed the link above to get started, After copying the code from the website. there was an error in Main Activity. getMenuInflater().inflate(R.menu.activity_main, menu); For some reason, menu does not exist. To combat this, I created a new xml file called menu and added this code

<?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>

I then changed what I had in Main Activity to getMenuInflater().inflate(R.menu.menu, menu); and the error went away. Unfortunately, the app has crashed either way and I'm not sure how to resolve this issue. I tried using an emulator and an actual android device to test. Does anyone have any ideas? Here is the error in the 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)

It sounds to me like the ConstraintLayout Support Library hasn't been added to the classpath.

In your build.gradle try adding these dependencies (may use newer versions):

    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'

Seems like you haven't defined the Dependency for the Constraint layout. Please add this to your build.gradle file and try again: implementation "androidx.constraintlayout:constraintlayout:2.0.0-beta4"

Note: You should be using the androidx libraries not the support one as others have suggested.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM