我试图从菜单选项中显示一个popupdialogue。代码是 -
public void createNewList(){
popupCreateList = new PopupWindow(popupCreateListView,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
popupCreateList.setBackgroundDrawable(new BitmapDrawable());
popupCreateList.setOutsideTouchable(true);
Log.i(TAG,"popUpListView-->>>" + popUpListView.getParent());
//((ViewGroup)popUpListView.getParent()).removeView(popUpListView); <--Commented
popupCreateList.showAtLocation(findViewById(R.id.layoutTaskManagerID), Gravity.CENTER, 0, 0); <-- ERROR
//popupCreateList.showAsDropDown(listsButton, 50, -30);
}
每当我尝试运行它时,它会给我以下logcat错误 -
12-30 14:43:00.735: E/AndroidRuntime(1385): FATAL EXCEPTION: main
12-30 14:43:00.735: E/AndroidRuntime(1385): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
12-30 14:43:00.735: E/AndroidRuntime(1385): at android.view.ViewGroup.addViewInner(ViewGroup.java:3378)
12-30 14:43:00.735: E/AndroidRuntime(1385): at android.view.ViewGroup.addView(ViewGroup.java:3249)
12-30 14:43:00.735: E/AndroidRuntime(1385): at android.view.ViewGroup.addView(ViewGroup.java:3225)
12-30 14:43:00.735: E/AndroidRuntime(1385): at android.widget.PopupWindow.preparePopup(PopupWindow.java:965)
12-30 14:43:00.735: E/AndroidRuntime(1385): at android.widget.PopupWindow.showAtLocation(PopupWindow.java:836)
12-30 14:43:00.735: E/AndroidRuntime(1385): at android.widget.PopupWindow.showAtLocation(PopupWindow.java:809)
12-30 14:43:00.735: E/AndroidRuntime(1385): at ray.kaushik.simple.task.manager.TaskManagerActivity.createNewList(TaskManagerActivity.java:340)
12-30 14:43:00.735: E/AndroidRuntime(1385): at ray.kaushik.simple.task.manager.TaskManagerActivity.onMenuItemSelected(TaskManagerActivity.java:324)
12-30 14:43:00.735: E/AndroidRuntime(1385): at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:958)
12-30 14:43:00.735: E/AndroidRuntime(1385): at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
12-30 14:43:00.735: E/AndroidRuntime(1385): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
12-30 14:43:00.735: E/AndroidRuntime(1385): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
12-30 14:43:00.735: E/AndroidRuntime(1385): at com.android.internal.view.menu.IconMenuView.invokeItem(IconMenuView.java:468)
12-30 14:43:00.735: E/AndroidRuntime(1385): at com.android.internal.view.menu.IconMenuItemView.performClick(IconMenuItemView.java:126)
12-30 14:43:00.735: E/AndroidRuntime(1385): at android.view.View$PerformClick.run(View.java:16966)
12-30 14:43:00.735: E/AndroidRuntime(1385): at android.os.Handler.handleCallback(Handler.java:615)
12-30 14:43:00.735: E/AndroidRuntime(1385): at android.os.Handler.dispatchMessage(Handler.java:92)
12-30 14:43:00.735: E/AndroidRuntime(1385): at android.os.Looper.loop(Looper.java:137)
12-30 14:43:00.735: E/AndroidRuntime(1385): at android.app.ActivityThread.main(ActivityThread.java:4745)
12-30 14:43:00.735: E/AndroidRuntime(1385): at java.lang.reflect.Method.invokeNative(Native Method)
12-30 14:43:00.735: E/AndroidRuntime(1385): at java.lang.reflect.Method.invoke(Method.java:511)
12-30 14:43:00.735: E/AndroidRuntime(1385): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-30 14:43:00.735: E/AndroidRuntime(1385): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-30 14:43:00.735: E/AndroidRuntime(1385): at dalvik.system.NativeStart.main(Native Method)
当我尝试第二次点击时,会遇到此错误。如果我取消注释注释行以删除视图仍然保持相同。可以dumone plz引导我的原因。请详细说明,因为我刚开始android开发。任何想法都相同。?谢谢你。
雷
答案 0 :(得分:0)
您是否有这样做的具体原因而不是使用Alert?警报是一个弹出对话框(您可以在其中有多个按钮)。另一种方法(如果警报不够)是创建一个具有Alert主题(在清单中设置)的整个活动,然后在其上调用startActivity()
,它看起来像一个警报,即使它是一个功能齐全的活动,它将位于活动堆栈的顶部。)
除非我不理解你,否则你试图从菜单中启动它的事实完全不是重点。你可以从任何地方开始。
答案 1 :(得分:0)
使用此工作示例代码(我编写并测试了它),并适应您的需求: 创建menu \ my_options_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/popup1"
android:title="popup1" />
</menu>
创建layout \ popup.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@android:color/background_light">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="1dp"
android:background="@android:color/darker_gray">
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="20dp">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="It's a PopupWindow" />
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<Button
android:id="@+id/dismiss"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Dismiss" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
在您的主要活动中添加以下内容:
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.my_options_menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.popup1:
if (popupCreateList!=null) return true;
LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup, null);
popupCreateList = new PopupWindow( popupView,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
popupCreateList.setBackgroundDrawable(new BitmapDrawable());
popupCreateList.setOutsideTouchable(true);
popupCreateList.showAtLocation((RelativeLayout)findViewById(R.id.main_activity_layout), Gravity.CENTER, 0, 0);
Button btnDismiss = (Button)popupView.findViewById(R.id.dismiss);
btnDismiss.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
popupCreateList.dismiss();
popupCreateList=null;
}});
return true;
default:
return super.onOptionsItemSelected(item);
}
}
更多:我建议你阅读这些文章以激励你:
http://android-er.blogspot.it/2012/03/example-of-using-popupwindow.html
http://android-ed.blogspot.it/2011/08/using-popupwindow-in-android.html