0
votes

Hi Guys please recommend a solution for me. when preparing an alert dialogue, see the error i get E/AndroidRuntime: FATAL EXCEPTION: main Process: com.techlinemobile.foodbasket, PID: 22724 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference at com.techlinemobile.foodbasket.ItemDetails.showAddedToCartDialogue(ItemDetails.java:162) at com.techlinemobile.foodbasket.ItemDetails.access$300(ItemDetails.java:25) at com.techlinemobile.foodbasket.ItemDetails$1.onClick(ItemDetails.java:131) at android.view.View.performClick(View.java:6291) at android.view.View$PerformClick.run(View.java:24931) at android.os.Handler.handleCallback(Handler.java:808) at android.os.Handler.dispatchMessage(Handler.java:101) at android.os.Looper.loop(Looper.java:166) at android.app.ActivityThread.main(ActivityThread.java:7529) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)

see the method

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

}

see the layout

<?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>
2

2 Answers

2
votes

use

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

instead of

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

and create dailoge at the end like this.

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

Move the line alertDialog.show() to right under builder.create(). Or you can use builder.show() method which will create and show the dialog immediately.

The error reported that you set listener on null object. Your dialog is not visible so that findViewById return null. Variables buttonContinue and buttonViewCart refer to null value.

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


    }
});