Java 创建活动时显示弹出窗口失败
我试图在创建方法的活动上显示弹出窗口,但似乎什么也没有发生。。没有例外,没有弹出窗口 下面是代码Java 创建活动时显示弹出窗口失败,java,android,android-popupwindow,Java,Android,Android Popupwindow,我试图在创建方法的活动上显示弹出窗口,但似乎什么也没有发生。。没有例外,没有弹出窗口 下面是代码 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); btnClear = (Button)findViewById(R.id.btnClear); txtPass
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
btnClear = (Button)findViewById(R.id.btnClear);
txtPassword = (TextView) findViewById(R.id.txtPassword);
txtPassword.setOnTouchListener(otl);
btnClear.setVisibility(View.GONE);
objDBHelper = DBHelper.getInstance(getApplicationContext());
SQLiteDatabase db = objDBHelper.getWritableDatabase();
long driverProfileCount = objDBHelper.getProfilesCount(db);
initiatePasswordPopupWindow(); // show pop up when no data is in table
}
private void initiatePasswordPopupWindow() {
try {
//We need to get the instance of the LayoutInflater, use the context of this activity
LayoutInflater inflater = (LayoutInflater) Login.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//Inflate the view from a predefined XML layout
final View layout = inflater.inflate(R.layout.setting_password_popup,
(ViewGroup) findViewById(R.id.setting_password_popup_element));
// create a 300px width and 470px height PopupWindow
pw = new PopupWindow(layout, 600, 720, true);
// display the popup in the center
layout.post(new Runnable() {
public void run() {
pw.showAtLocation(layout, Gravity.CENTER, 0, 0);
Button cancelButton = (Button) layout.findViewById(R.id.end_data_send_button);
cancelButton.setOnClickListener(cancel_button_click_listener);
}
});
/* TextView mResultText = (TextView) layout.findViewById(R.id.server_status_text);*/
} catch (Exception e) {
e.printStackTrace();
}
}
我缺少什么?您尝试使用带有视图的post弹出窗口布局,但该布局不会启动其消息队列。尝试使用已在活动中的视图,例如根视图-更改layout.postnew Runnable{to findviewbyandroid.R.id.content:
你确定driverProfileCount实际上等于0吗?你确定driverProfileCount==0吗?显示完整的代码,因为我们无法判断driverProfileCount是否为零。driverProfileCount为0。我已经调试了代码,它将进入initiatePasswordPopupWindow函数。为什么不使用AlertDialog
findViewById(android.R.id.content).post(new Runnable() {
public void run() {
pw.showAtLocation(layout, Gravity.CENTER, 0, 0);
Button cancelButton = (Button) layout.findViewById(R.id.end_data_send_button);
cancelButton.setOnClickListener(cancel_button_click_listener);
}
});