gpt4 book ai didi

java - 将文本设置为自定义对话框类

转载 作者:行者123 更新时间:2023-12-01 14:02:46 25 4
gpt4 key购买 nike

我正在尝试构建一个自定义对话框,我正在动态设置文本。这是我抛出 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()+"");

请解释一下如何将文本设置到我的对话框中。

最佳答案

首先尝试显示您的对话框,例如,在尝试查找 TextView 之前放置dialog.show。或者,或者先膨胀您的 View 并将 View 传递给对话框。您现在要做的就是尝试在 View 膨胀之前找到它。

编辑:要使用充气机,请尝试此操作,

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

}
});

关于java - 将文本设置为自定义对话框类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19214622/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com