gpt4 book ai didi

java - 每当我的 Android AlertDialog 加载、应用程序崩溃时,如何修复错误

转载 作者:行者123 更新时间:2023-12-01 19:33:45 25 4
gpt4 key购买 nike

大家好,请为我推荐一个解决方案。 准备警报对话时,请参阅我收到的错误E/AndroidRuntime:致命异常:main 进程:com.techlinemobile.foodbasket,PID:22724 java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)” 在 com.techlinemobile.foodbasket.ItemDetails.showAddedToCartDialogue(ItemDetails.java:162) 在 com.techlinemobile.foodbasket.ItemDetails.access$300(ItemDetails.java:25) 在 com.techlinemobile.foodbasket.ItemDetails$1.onClick(ItemDetails.java:131) 在 android.view.View.performClick(View.java:6291) 在 android.view.View$PerformClick.run(View.java:24931) 在 android.os.Handler.handleCallback(Handler.java:808) 在 android.os.Handler.dispatchMessage(Handler.java:101) 在 android.os.Looper.loop(Looper.java:166) 在 android.app.ActivityThread.main(ActivityThread.java:7529) 在 java.lang.reflect.Method.invoke( native 方法) 在 com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)

查看方法

私有(private)无效showAddedToCartDialogue(){

//before inflating the custom alert dialog layout, we will get the current activity viewgroup
ViewGroup viewGroup = findViewById(android.R.id.content);

//then we will inflate the custom alert dialog xml that we created
View dialogView = LayoutInflater.from(this).inflate(R.layout.added_to_cart_dialog, viewGroup, false);

//Now we need an AlertDialog.Builder object
AlertDialog.Builder builder = new AlertDialog.Builder(this);

//setting the view of the builder to our custom view that we already inflated
builder.setView(dialogView);

//finally creating the alert dialog and displaying it
AlertDialog alertDialog = builder.create();
final Button buttonContinue = (Button)alertDialog.getWindow().findViewById(R.id.buttonContinue);
final Button buttonViewCart = (Button)alertDialog.getWindow().findViewById(R.id.buttonViewCart);

buttonContinue.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "before adding card");
//--------------------------------
//load data to cart
Intent it = new Intent(ItemDetails.this, MainActivity.class);
startActivity(it);
}
});

buttonViewCart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "before adding card");
//--------------------------------
//load data to cart

Intent it = new Intent(ItemDetails.this, ShoppingCartActivity.class);
startActivity(it);


}
});
alertDialog.show();

}

查看布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="@color/colorPrimary">

<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerInParent="true"
android:background="@drawable/ic_success" />

</RelativeLayout>

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

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/added_to_cart"
android:textAlignment="center"
android:textAppearance="@style/TextAppearance.AppCompat.Headline" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="@string/successfully_added_long"
android:textAlignment="center"
android:textAppearance="@style/TextAppearance.AppCompat.Medium" />

<Button
android:id="@+id/buttonContinue"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="15dp"
android:background="@drawable/button_background"
android:text="@string/continue_shopping"
android:textColor="@color/colorPrimary" />

<Button
android:id="@+id/buttonViewCart"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="15dp"
android:background="@drawable/button_background"
android:text="@string/view_cart"
android:textColor="@color/colorPrimary" />

</LinearLayout>

</LinearLayout>

最佳答案

使用

Button buttonContinue = dialogView.findViewById(R.id.buttonContinue);
Button buttonViewCart = dialogView.findViewById(R.id.buttonViewCart);

而不是

Button buttonContinue = (Button)alertDialog.getWindow().findViewById(R.id.buttonContinue);

并在最后创建对话,如下所示。

    alertDialog= alertDialogBuilder.create();
alertDialog.show();
alertDialog.getWindow().setLayout(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

关于java - 每当我的 Android AlertDialog 加载、应用程序崩溃时,如何修复错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58583223/

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