0

I have built an overview of an order with a ReyclerView. Below you can see the code for the adapter class. My problem only occurs in the following place, if the user clicks on an element of ReyclerView, a popup with an expanded view should appear.

This popup also occurs. Unfortunately, as soon as I want to populate the TextView with the order code, the app supports and says null Attempt to invoke virtual method'void android.widget.TextView.setText (java.lang.CharSequence)' on a null object reference.

How can I avoid this null error so that it all works?

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public class UserBestellAdapter extends RecyclerView.Adapter<UserBestellAdapter.ViewHolder> {

    ArrayList<Bestellung> bestellung;
    Context mContext;
    Dialog epicDialog;

    public UserBestellAdapter(Context context, ArrayList<Bestellung> list) {
        mContext = context;
        bestellung = list;
        epicDialog = new Dialog(mContext);
    }

    @NonNull
    @Override
    public UserBestellAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.adapter_bestell, parent, false);
        UserBestellAdapter.ViewHolder viewHolder = new UserBestellAdapter.ViewHolder(view);


        return viewHolder;
    }
    @NonNull

    @Override
    public void onBindViewHolder(@NonNull UserBestellAdapter.ViewHolder holder, int position) {
      //Gesamtpreis:   holder.item_betrag.setText(String.valueOf(bestellung.get(position).getBetrag()));
      // Datum:   holder.item_datum.setText(bestellung.get(position).getDatum());
        holder.item_items.setText(bestellung.get(position).getProdukte());
        //holder.item_code.setText(bestellung.get(position).getBestellnummer());
        String bestellid =bestellung.get(position).getBestellnummer() + "";
        holder.item_code.setText(bestellid);
        holder.item_betrag.setText(Double.toString(bestellung.get(position).getSumme()));
        holder.item_datum.setText(bestellung.get(position).getDatum());
        holder.layout_user_bestellung.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                TextView order_overview_number = epicDialog.findViewById(R.id.order_overview_number);

                order_overview_number.setText();
                epicDialog.setContentView(R.layout.order_popup_waiting);
                epicDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                Button btn_order_overview_finish = (Button) epicDialog.findViewById(R.id.btn_order_overview_finish);
                /*
                btn_order_overview_finish.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        epicDialog.dismiss();
                    }

                });*/
                epicDialog.show();

            }
        });

    }

    @Override
    public int getItemCount()  {
        return bestellung.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private TextView item_items, item_betrag, item_datum, item_code;
        private ConstraintLayout layout_user_bestellung;



        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            item_items = itemView.findViewById(R.id.items);
            item_betrag = itemView.findViewById(R.id.betrag);
            item_datum = itemView.findViewById(R.id.datum);
            item_code = itemView.findViewById(R.id.code);
            layout_user_bestellung = itemView.findViewById(R.id.layout_user_bestellung);

        }
    }
}

1 Answers1

0

Your Dialog is initialized with new Dialog(context) but in your onClick Method you expect that your epicDialog has a TextVew with the id order_overview_number. This will always return null and thus order_overview_number.setText() thows a NPE.

To reference a view of a Dialog the Dialog needs a layout which contains that view, similar to fragments and activities.

This may have further information for you: How to create a Custom Dialog box in android?

https://developer.android.com/guide/topics/ui/dialogs

Traendy
  • 1,423
  • 15
  • 17
  • Thanks for the answer! Unfortunately, I don't get smart from the links. Do you have any help –  Jun 20 '20 at 10:59