Error: Android Swipe-up Card call button is giving the problem

Error: Android Swipe-up Card call button is giving the problem

我正在尝试制作向上滑动的卡片,但出现以下错误。请帮忙。我对 Android 编程还很陌生。

尝试后,我得出的结论是,如果我注释掉 java 文件中的所有代码,那么卡片就可以正常工作,但即查看卡片的按钮就是问题所在。 ..我猜。

MainActivity

public class MainActivity extends AppCompatActivity implements adapterCard.ListItemClickListener {

    private BottomSheetBehavior historyCardBehavior;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LinearLayout linearCardLayout = findViewById(R.id.history_sheet);
        historyCardBehavior = BottomSheetBehavior.from(linearCardLayout);

        Button historyButton = findViewById(R.id.history_button);

        historyButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                historyCardBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
            }
        });
    }
}

mainActivity.xml

<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <Button
        android:id="@+id/history_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:layout_marginStart="20dp"
        android:layout_marginTop="100dp"
        android:text="@string/history" />


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white"
        android:orientation="horizontal">

        <ScrollView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/white">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:padding="20dp">



            </LinearLayout>


        </ScrollView>

    </LinearLayout>

    <include
        android:id="@+id/orderHistory_card"
        layout="@layout/history_sheet" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

history_sheet.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/history_sheet"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/transparent"
    android:orientation="vertical"
    app:behavior_hideable="true"
    app:behavior_peekHeight="80dp"
    app:layout_behavior="@string/bottom_sheet_behavior">

    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:cardBackgroundColor="@android:color/white"
        app:cardCornerRadius="20dp"
        app:cardElevation="20dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:padding="15dp">

            <View
                android:layout_width="50dp"
                android:layout_height="8dp"
                android:layout_gravity="center"
                android:layout_marginBottom="10dp"
                android:background="#70000000"
                android:outlineProvider="background"/>

        </LinearLayout>

    </androidx.cardview.widget.CardView>
</LinearLayout>

错误

2021-01-30 00:10:35.274 8461-8461/com.iwantjob.oneresto E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.iwantjob.oneresto, PID: 8461
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.iwantjob.oneresto/com.iwantjob.oneresto.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewGroup$LayoutParams android.view.View.getLayoutParams()' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewGroup$LayoutParams android.view.View.getLayoutParams()' on a null object reference
        at com.google.android.material.bottomsheet.BottomSheetBehavior.from(BottomSheetBehavior.java:1632)
        at com.iwantjob.oneresto.MainActivity.onCreate(MainActivity.java:35)
        at android.app.Activity.performCreate(Activity.java:7136)
        at android.app.Activity.performCreate(Activity.java:7127)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:193) 
        at android.app.ActivityThread.main(ActivityThread.java:6669) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 
2021-01-30 00:10:35.299 8461-8461/com.iwantjob.oneresto I/Process: Sending signal. PID: 8461 SIG: 9

如果有人能提供帮助,那将非常有帮助。 提前致谢。

app:layout_behavior="@string/bottom_sheet_behavior" 行在这里最吸引我的眼球。

@string/bottom_sheet_behavior 在您的项目中定义的是什么?应将布局行为属性分配给 "com.google.android.material.bottomsheet.BottomSheetBehavior"

此外,从调用堆栈看来,您的 findViewById returns 为空。尝试使用 R.id.orderHistory_card 而不是 R.id.history_sheet 进行 linearCardLayout 查找。从长远来看,我建议研究 ViewBinding 以尽量减少围绕视图的样板代码并避免此类问题。