我的代码有什么错误,它没有显示弹出框(Android)?
单击弹出按钮时,它不会显示弹出框,并且会强制关闭应用程序。在这里,我包含了我的本机 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在膨胀
popup.xml
并将父项定义为(ViewGroup) findViewById(R.id.ButtonPopup)
。在您膨胀布局之前,不会创建此 ViewGroup。只有在setContentView
中设置布局后,findViewById
才会起作用。尝试用这个充气 -
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. andfindViewById
will work only after you set the layout insetContentView
.Try inflating using this -
@babu:如果您想为您想要的布局实现自定义弹出窗口,请使用以下命令:
它对我有很大帮助,希望也对你有帮助。
看这个演示:
@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: