如何在我的Android AlertDialog加载时解决错误,应用程序崩溃

问题描述 投票:0回答:2

大家好,请为我推荐一个解决方案。在准备警报对话框时,看到我得到的错误E / AndroidRuntime:致命异常:main流程:com.techlinemobile.foodbasket,PID:22724java.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(本机方法)在com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:245)在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)

查看方法

private void 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>
java android
2个回答
1
投票

alertDialog.show()行移到builder.create()下方。或者,您可以使用builder.show()方法立即创建并显示对话框。

错误报告您在空对象上设置了侦听器。您的对话框不可见,因此findViewById返回null。变量buttonContinuebuttonViewCart指向空值。

//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();

alertDialog.show(); <---------- show dialog here

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);


    }
});

1
投票

使用

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

代替

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

并像这样最后创建dailoge。

    alertDialog= alertDialogBuilder.create();
    alertDialog.show();
    alertDialog.getWindow().setLayout(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
© www.soinside.com 2019 - 2024. All rights reserved.