如何在Android应用程序中实现BottomSheet?

时间:2019-07-28 18:46:49

标签: android-studio kotlin

我正在使用Material Design在我的应用程序内部实现一个底页。运行应用程序时,它崩溃并返回此错误:

2019-07-28 13:28:29.774 7520-7520/com.thompson.bottomsheetdemo E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.thompson.bottomsheetdemo, PID: 7520
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.thompson.bottomsheetdemo/com.thompson.bottomsheetdemo.MainActivity}: android.view.InflateException: Binary XML file line #37 in com.thompson.bottomsheetdemo:layout/activity_main: Could not inflate Behavior subclass com.google.android.material.bottomsheet.BottomSheetBehavior
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3268)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3407)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7343)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:933)
     Caused by: android.view.InflateException: Binary XML file line #37 in com.thompson.bottomsheetdemo:layout/activity_main: Could not inflate Behavior subclass com.google.android.material.bottomsheet.BottomSheetBehavior
     Caused by: java.lang.RuntimeException: Could not inflate Behavior subclass com.google.android.material.bottomsheet.BottomSheetBehavior
        at androidx.coordinatorlayout.widget.CoordinatorLayout.parseBehavior(CoordinatorLayout.java:632)
        at androidx.coordinatorlayout.widget.CoordinatorLayout$LayoutParams.<init>(CoordinatorLayout.java:2863)
        at androidx.coordinatorlayout.widget.CoordinatorLayout.generateLayoutParams(CoordinatorLayout.java:1714)
        at androidx.coordinatorlayout.widget.CoordinatorLayout.generateLayoutParams(CoordinatorLayout.java:112)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:1125)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1084)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:682)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:534)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:481)
        at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:545)
        at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
        at com.thompson.bottomsheetdemo.MainActivity.onCreate(MainActivity.kt:18)
        at android.app.Activity.performCreate(Activity.java:7802)
        at android.app.Activity.performCreate(Activity.java:7791)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3243)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3407)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7343)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:933)
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.newInstance0(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
        at androidx.coordinatorlayout.widget.CoordinatorLayout.parseBehavior(CoordinatorLayout.java:630)
2019-07-28 13:28:29.775 7520-7520/com.thompson.bottomsheetdemo E/AndroidRuntime:    ... 26 more
    Caused by: java.lang.NoSuchMethodError: No static method metafactory(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite; in class Ljava/lang/invoke/LambdaMetafactory; or its super classes (declaration of 'java.lang.invoke.LambdaMetafactory' appears in /apex/com.android.runtime/javalib/core-oj.jar)
        at com.google.android.material.bottomsheet.BottomSheetBehavior.createShapeValueAnimator(BottomSheetBehavior.java:1052)
        at com.google.android.material.bottomsheet.BottomSheetBehavior.<init>(BottomSheetBehavior.java:255)
            ... 29 more

我认为此错误是由于未定义layout_behavior正确性引起的,但是我将layout_behavior定义为:

app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"

在MaterialTextView组件内部。该组件是我要用于BottomSheet的组件。

以下是我的代码:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:gravity="center_horizontal">

        <com.google.android.material.button.MaterialButton
                android:id="@+id/button_expand"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Expand"/>
        <com.google.android.material.button.MaterialButton
                android:id="@+id/button_collapse"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Collapse"/>
        <com.google.android.material.textview.MaterialTextView
                android:id="@+id/text_view_state"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="State"
                android:textSize="30sp"
                android:textColor="@android:color/black"
                android:layout_marginTop="16dp"/>

    </LinearLayout>

    <com.google.android.material.textview.MaterialTextView
            android:id="@+id/bottom_sheet"
            android:layout_width="match_parent"
            android:layout_height="350dp"
            android:padding="16dp"
            android:textSize="16sp"
            android:text="This is a bottom sheet\n\nLorem ipsum dolor sit amet, consectetuer adipiscing elit.
             Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes,
              nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim.
               Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut,
             imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt.
              Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula,
               porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus.
                Phasellus viverra nulla ut metus varius laoreet."
            app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"
            app:behavior_hideable="true"
            app:behavior_peekHeight="55dp"
            >


    </com.google.android.material.textview.MaterialTextView>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

MainActivity.kt

package com.thompson.bottomsheetdemo

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.button.MaterialButton
import com.google.android.material.textview.MaterialTextView

class MainActivity : AppCompatActivity() {

    private lateinit var bottomSheetBehavior: BottomSheetBehavior<MaterialTextView>
    private lateinit var mTextViewState: MaterialTextView


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val bottomSheet = findViewById<MaterialTextView>(R.id.bottom_sheet)

        bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet)
        mTextViewState = findViewById(R.id.text_view_state)

        val buttonExpand = findViewById<MaterialButton>(R.id.button_expand)
        val buttonCollapse = findViewById<MaterialButton>(R.id.button_collapse)

        buttonExpand.setOnClickListener{
            bottomSheetBehavior.state = BottomSheetBehavior.STATE_EXPANDED
        }

        buttonCollapse.setOnClickListener{
            bottomSheetBehavior.state = BottomSheetBehavior.STATE_COLLAPSED
        }

        bottomSheetBehavior.setBottomSheetCallback(object: BottomSheetBehavior.BottomSheetCallback(){
            override fun onStateChanged(bottomSheet: View, newState: Int) {
                when(newState){
                    BottomSheetBehavior.STATE_COLLAPSED -> mTextViewState.text = "Collapsed"

                    BottomSheetBehavior.STATE_EXPANDED -> mTextViewState.text = "Expanded"

                    BottomSheetBehavior.STATE_DRAGGING -> mTextViewState.text = "Dragging"

                    BottomSheetBehavior.STATE_HALF_EXPANDED -> mTextViewState.text = "Half Expanded"

                    BottomSheetBehavior.STATE_HIDDEN -> mTextViewState.text = "Hidden"

                    BottomSheetBehavior.STATE_SETTLING -> mTextViewState.text = "Settling"

                }
            }

            override fun onSlide(bottomSheet: View, slideOffset: Float) {

            }
        })
    }
}

Build.gradle

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.0"
    defaultConfig {
        applicationId "com.thompson.bottomsheetdemo"
        minSdkVersion 28
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation "com.google.android.material:material:1.1.0-alpha08"
    implementation 'androidx.core:core-ktx:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

我先感谢您的帮助!

0 个答案:

没有答案