PopUpWindow Android异常

时间:2013-02-02 10:15:49

标签: android popupwindow

我想在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时

1 个答案:

答案 0 :(得分: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();

}