动态膨胀给我一个nullexception [英] Dynamic inflating gives me a nullexception

查看:104
本文介绍了动态膨胀给我一个nullexception的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用LinearLayout进行布局.此布局将在内部填充另一个布局.目前,我遇到了问题(nullexceptions).下一个是我的膨胀代码:

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

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>

logcat在 rl_albaran_invoices_row.setOnClickListener ... 行中显示错误NullPointerException.我在做什么错了?

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.

推荐答案

我更改了视图类型以解决此问题:

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

我有...

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

现在我有...

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.

这篇关于动态膨胀给我一个nullexception的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆