0

我想在 android 中显示一个 popupWindow 我做了以下操作:

public class MyPopUp extends PopupWindow   {

    private View view;
    private Context context;

    public MyPopUp (Context context)
       {
        super(context);
       this.context = context;
    View myView = new myView(context);
    this.setContentView(myView );
        setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
    }

    public void showPopUp(){

        view = new View(context);
        this.showAtLocation(view, Gravity.CENTER, 0, 0);

    }
     }

这条线上有一个例外

this.showAtLocation(view, Gravity.CENTER, 0, 0);

02-02 09:13:31.456: ERROR/AndroidRuntime(458): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.popup.mypopup/com.popup.mypopup.Android_2_Activity}: java.lang.NullPointerException

当调用 showPopUp 时

4

1 回答 1

0

尝试这个

public MyPopUp (Context context){

    final Dialog dialog = new Dialog(context);
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog.setContentView(R.layout.custome_dialog);

            Button dialogCancel = (Button) dialog
                    .findViewById(R.id.btnCancelPassword);
            Button dialogSubmit = (Button) dialog
                    .findViewById(R.id.btnSubmitPassword);
            dialogSubmit.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {

                    dialog.dismiss();
                }
            });
            dialogCancel.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
            dialog.show();

}
于 2013-02-02T10:26:26.737 回答