2012-12-30 177 views
2

我想從menuoptions中顯示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開發。任何想法相同。?感謝提前。

Ray

回答

0

使用此工作示例代碼(我寫和測試它),並根據需要進行修改: 創建菜單\ my_options_menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:id="@+id/popup1" 
android:title="popup1" /> 
</menu> 

創建佈局\ 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); 
    } 
} 

更多:我建議你u到爲了閱讀這些文章,激勵你:

http://android-er.blogspot.it/2012/03/example-of-using-popupwindow.html

http://android-ed.blogspot.it/2011/08/using-popupwindow-in-android.html

PopupWindow in android

+1

thnx兄弟..!我會試一下 ..!! – RayKaushik

0

是否有一個特定的原因,你這樣做而不是使用Alert?警報是一個彈出對話框(你可以有多個按鈕)。另一種方法(如果Alert不夠)是創建一個具有Alert主題(在清單中設置)的整個活動,然後您可以調用startActivity(),它看起來像一個警報,即使它是一個完整的功能活動將位於活動堆棧頂部)。

除非我不理解你,否則你試圖從菜單中啓動它的事實完全是重點。你可以從任何地方開始。

+0

我需要用戶輸入的一些中使用的彈出對話。你可以提供鏈接或進一步的細節使用新的活動來獲得一些輸入或作爲彈出對話 – RayKaushik