Android:弹出窗口和关闭按钮不起作用

发布于 2024-12-22 09:31:11 字数 1802 浏览 0 评论 0原文

我有:

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

    @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(); 中)而不是 popUp.xml 时,一切正常。 使用非 main.xml 中的按钮有什么问题

I have:

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

    @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();    

                }
       }); 

and I get force close here

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();

              }
    });
        }


    } 

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雪花飘飘的天空 2024-12-29 09:31:11

我认为问题就在这里:

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

它正在主布局内查找按钮视图,而不是在弹出布局内查找。所以 ok_button 可能为空。

尝试从构造函数中移出 inflate 调用,如下所示:

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);

I think that the problem is here:

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

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);

and then:

ok_button = (Button) v.findViewById(R.id.ok_button);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文