Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/225.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android弹出窗口关闭问题_Android - Fatal编程技术网

Android弹出窗口关闭问题

Android弹出窗口关闭问题,android,Android,我正在尝试一个弹出窗口的示例,面临2个问题 1) 弹出窗口未关闭 2) showAtLocation正在提供空指针异常 public void onShowPopup(View v){ RelativeLayout relativeLayout = (RelativeLayout)findViewById(R.id.mainActivity); LayoutInflater layoutInflater = (LayoutInflater)getSystemService(Con

我正在尝试一个弹出窗口的示例,面临2个问题 1) 弹出窗口未关闭 2) showAtLocation正在提供空指针异常

public void onShowPopup(View v){
    RelativeLayout relativeLayout = (RelativeLayout)findViewById(R.id.mainActivity);
    LayoutInflater layoutInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View inflatedView = layoutInflater.inflate(R.layout.informationpopup, relativeLayout);

    final PopupWindow popWindow = new PopupWindow(getBaseContext());
    popWindow.setContentView(inflatedView);

    popWindow.setBackgroundDrawable(new BitmapDrawable());
    popWindow.setFocusable(true);
    //popWindow.setOutsideTouchable(true);


    //popWindow.showAtLocation(inflatedView, Gravity.NO_GRAVITY, 0, 0);

    // Getting a reference to Close button, and close the popup when clicked.
    Button close = (Button) findViewById(R.id.close);
    close.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast t = Toast.makeText(getBaseContext(), "Close", Toast.LENGTH_LONG);
            t.show();
            popWindow.dismiss();        
        }
    });
}

你充气的方式是错误的。另外,showAtLocation视图应该是显示弹出窗口的父视图

给你,这应该有用

   public void onShowPopup(View v){
    LayoutInflater layoutInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View inflatedView = layoutInflater.inflate(R.layout.informationpopup, null,false);

    final PopupWindow popWindow = new PopupWindow(getBaseContext());
    popWindow.setContentView(inflatedView);

    popWindow.setBackgroundDrawable(new BitmapDrawable());
    popWindow.setFocusable(true);
    popWindow.setOutsideTouchable(true);

    // Getting a reference to Close button, and close the popup when clicked.
    Button close = (Button) inflatedView.findViewById(R.id.close);
    close.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast t = Toast.makeText(getBaseContext(), "Close", Toast.LENGTH_LONG);
            t.show();
            popWindow.dismiss();
        }
    });
    popWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
    popWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
    popWindow.showAtLocation(v, Gravity.NO_GRAVITY, v.getLeft(),v.getBottom());
}

谢谢这确实奏效了。我看到你对代码做了一些调整。你能解释一下吗。我的理解是,我们需要膨胀到给定的视图,而不是空视图。另外,为什么需要对onclick侦听器进行更改?下面关于inflate的博客说不要使用null[link]