我需要在子线程中使用弹出窗口。看一下示例代码的各种片段,我得到了以下内容:
void completed_dialog()
{
dialog_action_taken = false;
runOnUiThread(new Runnable()
{
public void run()
{
Button but = (Button) findViewById(R.id.pop_can);
LayoutInflater inflater = (LayoutInflater)
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final PopupWindow pw = new PopupWindow(inflater.inflate(R.layout.endofgame,
null, false), 300, 300, true);
pw.setBackgroundDrawable(new BitmapDrawable());
OnClickListener cancel_button_click_listener = new OnClickListener()
{
public void onClick(View v)
{
pw.dismiss();
}
};
but.setOnClickListener(cancel_button_click_listener);
pw.showAtLocation(game_frame_layout, Gravity.CENTER, 0, 0);
}
}
}
如果行“but.setOnClickListener(cancel_button_click_listener);”被注释掉然后我完全看到了对话框和按钮。但是如果我离开该行,则程序在对话框应该出现的位置崩溃 - 即我从未看到对话框。 我可以通过一个小的修改来使代码工作吗?
答案 0 :(得分:3)
你的findViewById()
找不到按钮。你需要让它看起来像这样的布局
public void run()
{
LayoutInflater inflater = (LayoutInflater)
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.endofgame, null, false);
Button but = (Button) layout.findViewById(R.id.pop_can);
final PopupWindow pw = new PopupWindow(layout, 300, 300, true);
pw.setBackgroundDrawable(new BitmapDrawable());
OnClickListener cancel_button_click_listener = new OnClickListener()
{
public void onClick(View v)
{
pw.dismiss();
}
};
but.setOnClickListener(cancel_button_click_listener);
pw.showAtLocation(game_frame_layout, Gravity.CENTER, 0, 0);
}