Android的:弹出窗口和按钮关闭不工作 [英] Android: popUp and button to dismiss doesnt work

查看:183
本文介绍了Android的:弹出窗口和按钮关闭不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有:

popUp.xml

popUp.xml

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"

            ....
    <Button
        android:id="@+id/ok_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ok" 
        android:gravity="left"
        />
</LinearLayout>

main.java

main.java

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ListView listView = (ListView)findViewById(R.id.list);
    listView.setOnItemClickListener(new OnItemClickListener()
                {
                public void onItemClick(AdapterView<?> parent, View v,
                int position, long id)
                {
                    popUp();    

                }
       }); 

和我得到强制关闭此处

public void popUp(){
            LayoutInflater inflater = (LayoutInflater) project.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final PopupWindow pw = new PopupWindow(inflater.inflate(R.layout.popup,null, false),300,400,true);
            ok_button = (Button) findViewById(R.id.ok_button);
            pw.showAtLocation(findViewById(R.id.list), Gravity.BOTTOM, 0,10);
            ok_button.setOnClickListener(new OnClickListener() { //here I get FC
                @Override
               public void onClick(View v) {

                 pw.dismiss();

              }
    });
        }


    } 

当我使用按钮(在弹出();)从main.xml中,而不是popUp.xml一切工作。
什么是错的使用从按钮没有的main.xml

When I use button (in popUp();) from main.xml instead of popUp.xml everything is working. What is wrong with using the button from not main.xml

推荐答案

我觉得现在的问题是在这里:

I think that the problem is here:

ok_button = (Button) findViewById(R.id.ok_button);

它正在寻找的主要布局而不是弹出的布局里面找里面的按钮来查看。所以大概ok_button为null。

it is looking for the button view inside the main layout instead of looking inside the popup layout. So probably ok_button is null.

尝试将充气呼叫从构造函数中搬出来,像这样的:

Try to move out the inflate call from the constructor, like this:

View v = inflater.inflate(R.layout.popup,null, false);
final PopupWindow pw = new PopupWindow(v,300,400,true);

和则:

ok_button = (Button) v.findViewById(R.id.ok_button);

这篇关于Android的:弹出窗口和按钮关闭不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆