2

I am trying to show a popupdialogue from menuoptions. Code being -

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

    }

Whenever I try to run it it given me following logcat error -

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)

This error is faced when I try to click second time. If i uncomment the commented line to remove view still it remains same. Can dumone plz guide me with reason for this.Please elaborate as I m new to android development. Any Idea for same.?Thanks in Advance.

Ray

RayKaushik
  • 301
  • 5
  • 17

2 Answers2

0

Is there a specific reason you're doing it this way rather than using Alert? Alert is a popup dialog (which you can have multiple buttons in). The other approach (if Alert isn't enough) is to create an entire activity that has the Alert theme (set in the manifest) so then you call startActivity() on it and it will look like an alert, even as it is a fully functional activity which will live at the top of the activity stack).

Unless I'm not understanding you, the fact that you're trying to start it from the menu is entirely besides the point. You can start it from anywhere.

Yevgeny Simkin
  • 27,946
  • 39
  • 137
  • 236
  • I need some input from user in the pop up dialogue used. can you plz provide link or further details on using a new activity to get some input or act as pop up dialogue – RayKaushik Dec 30 '12 at 11:15
0

Use this working sample code (i wrote and tested it), and adapt to your needs: create menu\my_options_menu.xml

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

create 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>

In your main activity add the following:

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

More: i suggest to you to read these articles in order to inspire you:

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

Community
  • 1
  • 1
Gaucho
  • 1,328
  • 1
  • 17
  • 32