【问题标题】:How do I fix an Error whenever my Android AlertDialog loads, the application crashes每当我的 Android AlertDialog 加载时,如何修复错误,应用程序崩溃
【发布时间】:2019-10-27 20:46:28
【问题描述】:

大家好,请为我推荐一个解决方案。 准备警报对话时,请查看我得到的错误 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(本机方法) 在 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


【解决方案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);

【讨论】:

  • 很高兴为您提供帮助 :)
【解决方案2】:

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


    }
});

【讨论】:

    猜你喜欢
    • 2023-01-27
    • 1970-01-01
    • 1970-01-01
    • 2021-11-27
    • 2021-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多