错误:Android刷卡呼叫按钮出现问题



我正试图使刷卡,它给出以下错误。请帮助。我对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返回null。尝试使用R.id.orderHistory_card而不是R.id.history_sheet来查找linearCardLayout。从长远来看,我建议研究ViewBinding,以尽量减少视图周围的样板代码,并避免此类问题。

最新更新