0

我正在尝试构建一个自定义对话框,我正在动态设置文本。这是我抛出的代码NullPointerException更新代码

  listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            HomeItem homeItem = (HomeItem) adapter.getItem(position);
            final Dialog dialog = new Dialog(databaseFightCard.this, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
            WindowManager.LayoutParams lp = (dialog.getWindow().getAttributes());
            lp.dimAmount = 0.5f;
            dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);

            Window window = dialog.getWindow();
            window.setGravity(Gravity.CENTER);

            dialog.setContentView(R.layout.custom_dialog);

            LayoutInflater inflater = (LayoutInflater) databaseFightCard.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
           RelativeLayout relative = (RelativeLayout) inflater.inflate(R.layout.custom_dialog, null, false);

            Button play = (Button) findViewById(R.id.bPlay);
            Button done = (Button) findViewById(R.id.bDone);
            TextView rn = (TextView) findViewById(R.id.tvRName);
            TextView ra = (TextView) findViewById(R.id.tvRAge);
            TextView rr = (TextView) findViewById(R.id.tvRRecord);
            TextView rh = (TextView) findViewById(R.id.tvRHeight);
            TextView rw = (TextView) findViewById(R.id.tvRWeight);
            TextView rc = (TextView) findViewById(R.id.tvRCity);
            TextView re = (TextView) findViewById(R.id.tvRExp);
            TextView bn = (TextView) findViewById(R.id.tvBName);
            TextView ba = (TextView) findViewById(R.id.tvBAge);
            TextView br = (TextView) findViewById(R.id.tvBRecord);
            TextView bh = (TextView) findViewById(R.id.tvBHeight);
            TextView bw = (TextView) findViewById(R.id.tvBWeight);
            TextView bc = (TextView) findViewById(R.id.tvBCity);
            TextView be = (TextView) findViewById(R.id.tvBExp);


            rn.setText(homeItem.getHomeItemRedName()+"");
            ra.setText(homeItem.getHomeItemRedAge()+"");
            rr.setText(homeItem.getHomeItemRedRecord()+"");
            rh.setText(homeItem.getHomeItemRedHeight()+"");
            rw.setText(homeItem.getHomeItemRedWeight()+"");
            rc.setText(homeItem.getHomeItemRedCity()+"");
            re.setText(homeItem.getHomeItemRedExp()+"");
            bn.setText(homeItem.getHomeItemBlueName()+"");
            ba.setText(homeItem.getHomeItemBlueAge()+"");
            br.setText(homeItem.getHomeItemBlueRecord()+"");
            bh.setText(homeItem.getHomeItemBlueHeight()+"");
            bw.setText(homeItem.getHomeItemBlueWeight()+"");
            bc.setText(homeItem.getHomeItemBlueCity()+"");
            be.setText(homeItem.getHomeItemBlueExp()+"");
            dialog.setContentView(relative);

            play.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //add the play intent here
                    dialog.dismiss();
                }
            });

            done.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dialog.dismiss();
                }
            });
                dialog.show();

        }
    });

我得到 NullPointerException

rn.setText(homeItem.getHomeItemRedName()+"");

请解释我如何将文本设置为我的对话框。

4

1 回答 1

1

尝试首先显示您的对话框,如在尝试查找 textView 之前放置 dialog.show。要么,要么先膨胀您的视图并将视图传递给对话框。你现在正在做的是试图在它膨胀之前找到视图。

编辑:要使用充气机,试试这个,

   LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

   RelativeLayout view = (RelativeLayout)inflater.inflate(R.layout.custom_dialog, null, false);

   TextView rn = (TextView) view.findViewById(R.id.tvRName);

   dialog.setContentView(view);

编辑2:

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        HomeItem homeItem = (HomeItem) adapter.getItem(position);
        final Dialog dialog = new Dialog(databaseFightCard.this, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
        WindowManager.LayoutParams lp = (dialog.getWindow().getAttributes());
        lp.dimAmount = 0.5f;
        dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);

        Window window = dialog.getWindow();
        window.setGravity(Gravity.CENTER);

        LayoutInflater inflater = (LayoutInflater) databaseFightCard.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
       RelativeLayout relative = (RelativeLayout) inflater.inflate(R.layout.custom_dialog, null, false);

        dialog.setContentView(relative);

        Button play = (Button) relative.findViewById(R.id.bPlay);
        Button done = (Button) relative.findViewById(R.id.bDone);
        TextView rn = (TextView) relative.findViewById(R.id.tvRName);
        TextView ra = (TextView) relative.findViewById(R.id.tvRAge);
        TextView rr = (TextView) relative.findViewById(R.id.tvRRecord);
        TextView rh = (TextView) relative.findViewById(R.id.tvRHeight);
        TextView rw = (TextView) relative.findViewById(R.id.tvRWeight);
        TextView rc = (TextView) relative.findViewById(R.id.tvRCity);
        TextView re = (TextView) relative.findViewById(R.id.tvRExp);
        TextView bn = (TextView) relative.findViewById(R.id.tvBName);
        TextView ba = (TextView) relative.findViewById(R.id.tvBAge);
        TextView br = (TextView) relative.findViewById(R.id.tvBRecord);
        TextView bh = (TextView) relative.findViewById(R.id.tvBHeight);
        TextView bw = (TextView) relative.findViewById(R.id.tvBWeight);
        TextView bc = (TextView) relative.findViewById(R.id.tvBCity);
        TextView be = (TextView) relative.findViewById(R.id.tvBExp);


        rn.setText(homeItem.getHomeItemRedName()+"");
        ra.setText(homeItem.getHomeItemRedAge()+"");
        rr.setText(homeItem.getHomeItemRedRecord()+"");
        rh.setText(homeItem.getHomeItemRedHeight()+"");
        rw.setText(homeItem.getHomeItemRedWeight()+"");
        rc.setText(homeItem.getHomeItemRedCity()+"");
        re.setText(homeItem.getHomeItemRedExp()+"");
        bn.setText(homeItem.getHomeItemBlueName()+"");
        ba.setText(homeItem.getHomeItemBlueAge()+"");
        br.setText(homeItem.getHomeItemBlueRecord()+"");
        bh.setText(homeItem.getHomeItemBlueHeight()+"");
        bw.setText(homeItem.getHomeItemBlueWeight()+"");
        bc.setText(homeItem.getHomeItemBlueCity()+"");
        be.setText(homeItem.getHomeItemBlueExp()+"");
        dialog.setContentView(relative);

        play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //add the play intent here
                dialog.dismiss();
            }
        });

        done.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
            dialog.show();

    }
});
于 2013-10-07T00:55:57.557 回答