我的代码有什么错误,它没有显示弹出框(Android)?

发布于 2025-01-05 09:43:31 字数 908 浏览 2 评论 0原文

单击弹出按钮时,它不会显示弹出框,并且会强制关闭应用程序。在这里,我包含了我的本机 Android 应用程序的代码(xml 和 java)。

popup.xml

<Button
    android:id="@+id/ButtonPopup"
    android:layout_marginTop="20dip"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="onButtonInPopup"
    android:text="Dismiss this PopupWindow">
</Button>

Java代码

public void onButtonPopup (View target) {
       // Make a View from our XML file
       LayoutInflater inflater = (LayoutInflater)
             this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
       View layout = inflater.inflate(R.layout.popup,(ViewGroup) findViewById(R.id.ButtonPopup));

       m_pw = new PopupWindow( layout,  350,  250,  true);
       m_pw.showAtLocation(layout, Gravity.CENTER, 0, 0);
    }

While clicking the popup button it's not showing popup box and it's force close the application. Here i included my code (xml and java) for my native android application.

popup.xml

<Button
    android:id="@+id/ButtonPopup"
    android:layout_marginTop="20dip"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="onButtonInPopup"
    android:text="Dismiss this PopupWindow">
</Button>

Java code

public void onButtonPopup (View target) {
       // Make a View from our XML file
       LayoutInflater inflater = (LayoutInflater)
             this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
       View layout = inflater.inflate(R.layout.popup,(ViewGroup) findViewById(R.id.ButtonPopup));

       m_pw = new PopupWindow( layout,  350,  250,  true);
       m_pw.showAtLocation(layout, Gravity.CENTER, 0, 0);
    }

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

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

发布评论

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

评论(2

半城柳色半声笛 2025-01-12 09:43:31

您正在膨胀 popup.xml 并将父项定义为 (ViewGroup) findViewById(R.id.ButtonPopup)。在您膨胀布局之前,不会创建此 ViewGroup。只有在 setContentView 中设置布局后,findViewById 才会起作用。
尝试用这个充气 -

View layout = inflater.inflate(R.layout.popup, null);

You are inflating popup.xml and defined the parent as (ViewGroup) findViewById(R.id.ButtonPopup). This ViewGroup will not create until you inflate the layout. and findViewById will work only after you set the layout in setContentView.
Try inflating using this -

View layout = inflater.inflate(R.layout.popup, null);
旧人九事 2025-01-12 09:43:31

@babu:如果您想为您想要的布局实现自定义弹出窗口,请使用以下命令:
它对我有很大帮助,希望也对你有帮助。

看这个演示:

    public class ExPopup extends Activity {

    Dialog myDialog;
    Button myButton;

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

        myButton = (Button) findViewById(R.id.ClkBtn);

        myButton.setOnClickListener(new OnClickListener() {          
        @Override
            public void onClick(View v) {
                myDialog = new Dialog(ExPopup.this,R.style.CustomDialogTheme);
                myDialog.setContentView(R.layout.mydialog);

                //requestWindowFeature(Window.FEATURE_NO_TITLE);
                //myDialog.setTitle("My Dialog");
                myDialog.setCancelable(true);
//                Button button = (Button) myDialog.findViewById(R.id.Btn1);
//                button.setOnClickListener(new OnClickListener() {
//                @Override
//                    public void onClick(View v) {
//                  myDialog.dismiss();
//                    }
//                });
//    
                myDialog.show();
            }
        });
    }


 }

@babu: If you want to implement the Custom popup for your desire layout then use this:
Its help me lot and hope also help you.

See this demo:

    public class ExPopup extends Activity {

    Dialog myDialog;
    Button myButton;

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

        myButton = (Button) findViewById(R.id.ClkBtn);

        myButton.setOnClickListener(new OnClickListener() {          
        @Override
            public void onClick(View v) {
                myDialog = new Dialog(ExPopup.this,R.style.CustomDialogTheme);
                myDialog.setContentView(R.layout.mydialog);

                //requestWindowFeature(Window.FEATURE_NO_TITLE);
                //myDialog.setTitle("My Dialog");
                myDialog.setCancelable(true);
//                Button button = (Button) myDialog.findViewById(R.id.Btn1);
//                button.setOnClickListener(new OnClickListener() {
//                @Override
//                    public void onClick(View v) {
//                  myDialog.dismiss();
//                    }
//                });
//    
                myDialog.show();
            }
        });
    }


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