Java 在子线程中的自定义弹出窗口中设置onclicklistener
我需要从一个子线程中使用一个弹出窗口。环顾各个示例代码片段,我得出以下结论:Java 在子线程中的自定义弹出窗口中设置onclicklistener,java,android,Java,Android,我需要从一个子线程中使用一个弹出窗口。环顾各个示例代码片段,我得出以下结论: void completed_dialog() { dialog_action_taken = false; runOnUiThread(new Runnable() { public void run() { Button but = (Button) findViewById(R.id.pop_can); LayoutInflater inflater = (L
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);”被注释掉,那么我可以完美地看到对话框和按钮。但是,如果我保留该行,那么程序将在该对话框应该出现的位置崩溃-即,我从未看到该对话框。
我可以稍微修改一下代码吗?是您的
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);
}
这样找不到按钮。您需要查看充气布局的内部。应该考虑调试而不是注释行。