Dynamic inflating gives me a nullexception

2020-01-20 02:56发布

问题:

I'm making a layout with a LinearLayout. This layout will have another layout inflated inside. Currently, I'm getting problems (nullexceptions) with that. My inflating code is the next:

private void addLayoutInvoices() {

        LinearLayout ll_invoices = new LinearLayout(getActivity());
        ll_invoices.setGravity(Gravity.CENTER);
        ll_invoices.setOrientation(LinearLayout.VERTICAL);

        LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

        ll_invoices.setLayoutParams(llp);

        ...

        if(mas.size() > 0) {

            for (int i = 0; i < mas.size(); i++) {

                if(mas.get(i).getStatus() == 1){

                    View newGratLayout = LayoutInflater.from(getActivity()).inflate(R.layout.albaran_invoices_row, ll_invoices_layer, false);

                    newGratLayout.setId(i + 8000);

                    RelativeLayout rl_albaran_invoices_row = (RelativeLayout) newGratLayout.findViewById(R.id.rl_albaran_invoices_row);
                    final LinearLayout ll_details = (LinearLayout) newGratLayout.findViewById(R.id.ll_details);

                    ...

                    rl_albaran_invoices_row.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (!rowClicked) {
                                ll_details.setVisibility(View.VISIBLE);
                                rowClicked = true;
                            } else {
                                ll_details.setVisibility(View.GONE);
                                rowClicked = false;
                            }
                        }
                    });

                    ll_invoices.addView(newGratLayout);
                }

            }

            ll_invoices_layer.addView(ll_invoices);

            if(existsPending)
                bt_resend_pending.setVisibility(View.VISIBLE);
        }



    }

albaran_invoices_row.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/rl_albaran_invoices_row"
    android:scrollIndicators="none">

    ...

</RelativeLayout>

The logcat shows an error NullPointerException in the line rl_albaran_invoices_row.setOnClickListener.... What I'm doing wrong?

NOTE: I'm doing this from a Fragment.

回答1:

seems like "newGratLayout" view is null. Debug the code,so that you will get the idea.



回答2:

I've change the type of view to resolve the problem:

I had...

//View newGratLayout = LayoutInflater.from(getActivity()).inflate(R.layout.albaran_invoices_row, ll_invoices_layer, false);

And now I had...

RelativeLayout newGratLayout = (RelativeLayout)getActivity().getLayoutInflater().inflate(R.layout.albaran_invoices_row, null); //rl_albaran_invoices_row

Thank for the people how try to help me.