onCreate里面不允许直接显示PopupWindow,你可以开启一个线程延迟启动:
new Thread(){
          public void run(){
                    try{
                           Thread.sleep(100);
                    }catch(Exception e){}
                   handler.sendEmptyMessage(0);
          }
}.start();在handler里面显示popupWindow即可

解决方案 »

  1.   

    pw.showAtLocation(parent, Gravity.CENTER, 0, 0);
    这里的parent不能空啊,必须有依附的view  , popupWindows才能显示
      

  2.   

    1L说得对,popupwindow不允许在oncreate里面显示,你可以让他启动完了 通过某种方式再显示,按钮触发,线程触发之类的。
      

  3.   


    为啥我改了还是不行呢?看看我的程序吧
    login.xml<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/Login"
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/loginbg"></LinearLayout>public class LoginActivity extends Activity {
    private Handler handler; @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login);
    new Thread() {
    public void run() {
    try {
    Thread.sleep(1000);
    } catch (Exception e) {
    }
    handler.sendEmptyMessage(0);
    }
    }.start();
    } public void showPopupWindow(Context context, View parent) {
    LayoutInflater inflater = (LayoutInflater) context
    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final View vPopupWindow = inflater.inflate(R.layout.logindlg, null,
    false);
    final PopupWindow pw = new PopupWindow(vPopupWindow, 300, 300, true); // OK按钮及其处理事件
    Button btnOK = (Button) vPopupWindow.findViewById(R.id.BtnOK);
    btnOK.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
    // 设置文本框内容
    EditText edtUsername = (EditText) vPopupWindow
    .findViewById(R.id.username_edit);
    edtUsername.setText("username");
    EditText edtPassword = (EditText) vPopupWindow
    .findViewById(R.id.password_edit);
    edtPassword.setText("password");
    }
    }); // Cancel按钮及其处理事件
    Button btnCancel = (Button) vPopupWindow.findViewById(R.id.BtnCancel);
    btnCancel.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
    pw.dismiss();// 关闭
    }
    });
    // 显示popupWindow对话框
    pw.showAtLocation(parent, Gravity.CENTER, 0, 0);
    } class myHandler extends Handler { @Override
    public void handleMessage(Message msg) {
    showPopupWindow(LoginActivity.this,
    LoginActivity.this.findViewById(R.id.Login));
    }
    }
    }哪里还有问题?
      

  4.   

    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.login);
            Button btn = new Button(this);
            this.addContentView(btn, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            btn.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
    // TODO Auto-generated method stub
    showPopupWindow(MainActivity.this,v);
    }
    });
        }试试
      

  5.   

    thanks ,one more quesetion
    android   如何让dialog里面的edit控件点击后才获得焦点
      

  6.   

    thanks ,one more quesetion
    android   如何让dialog里面的edit控件点击后才获得焦点
    在你的布局文件里,EditText 的父层加个属性 
    android:focusable="true" 
    android:focusableInTouchMode="true"