代码之家  ›  专栏  ›  技术社区  ›  Tiago Sousa

在onItemLongClick中显示AlertDialog会给出一个关于父级的错误,我该怎么做?

  •  1
  • Tiago Sousa  · 技术社区  · 9 年前

    当用户显示对话框时,我正在尝试显示对话框 onItemLongClick 用于确认用户是否想要这样做,但在 adb.show() :

    指定的子级已具有父级。必须首先对孩子的父级调用removeView()

    代码如下:

    listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
    
                AlertDialog.Builder adb = new AlertDialog.Builder(getBaseContext());
                adb.setView(view);
                adb.setTitle("Title of alert dialog");
                adb.setIcon(android.R.drawable.ic_dialog_alert);
                adb.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
    
                        Toast.makeText(Menu.this, "OK", Toast.LENGTH_LONG).show();
                    } });
    
                adb.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(Menu.this, "cancel", Toast.LENGTH_LONG).show();
                        //finish();
                    } });
                adb.show();
    
                return true;
            }
        });
    

    我搜索了答案,但我没有看到关于listview或longclick的信息,所以我需要帮助:(

    补充 logcat,它给出错误的行在哪里出现 adb.show(); :

    java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
    at android.view.ViewGroup.addViewInner(ViewGroup.java:3936)
    at android.view.ViewGroup.addView(ViewGroup.java:3786)
    at android.view.ViewGroup.addView(ViewGroup.java:3758)
    at com.android.internal.app.AlertController.setupView(AlertController.java:492)
    at com.android.internal.app.AlertController.installContent(AlertController.java:236)
    at android.app.AlertDialog.onCreate(AlertDialog.java:356)
    at android.app.Dialog.dispatchOnCreate(Dialog.java:373)
    at android.app.Dialog.show(Dialog.java:274)
    at android.app.AlertDialog$Builder.show(AlertDialog.java:993)
    at pt.isec.jogodememoria.MenuEscolheNivel$2.onItemLongClick(MenuEscolheNivel.java:119)
    at android.widget.AbsListView.performLongPress(AbsListView.java:3121)
    at android.widget.AbsListView$CheckForLongPress.run(AbsListView.java:3070)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5254)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
    
    4 回复  |  直到 9 年前
        1
  •  1
  •   user3956566 user3956566    9 年前

    一个很容易的错误,这个丢失了 AlertDialog alertDialog = adb.create(); 并且不需要setView。您仍然可以创建一个单独的方法,并为每次长项目单击实例化它。

    listView.setOnItemLongClickListener(new AdapterView
                .OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
    
            createDialog(view);
            return true;
        }
    });
    

    然后为对话框创建一个单独的方法:

    public void createDialog(View view){
    
       AlertDialog.Builder adb = new AlertDialog.Builder(this);
        //adb.setView(Main.this);
        adb.setTitle("Title of alert dialog");
        adb.setIcon(android.R.drawable.ic_dialog_alert);
        adb.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
    
                Toast.makeText(getApplicationContext(), "OK", Toast.LENGTH_LONG).show();
            } });
    
        adb.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(getApplicationContext(), "cancel", Toast.LENGTH_LONG).show();
                //finish();
            } });
    
        AlertDialog alertDialog = adb.create();
        alertDialog.show();
    
    }
    

    enter image description here

        2
  •  0
  •   Jose Angel Maneiro    9 年前

    我认为这条线

    adb.setView(view);
    

    是错误的。

    因为您正在尝试将项目视图(视图)设置为您的主视图Dialog。 如果对这些行进行注释,则代码应该是有效的。

    你应该换这条线

    adb.show();
    

    通过这个

    AlertDialog dialog = adb.create();
    dialog.show();
    

    然后通过YourActivity更改上下文(getBaseContext())。this或getActivity()。

    当做

        3
  •  0
  •   Community Reversed Engineer    4 年前

    先试试这个

      listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
                @Override
                public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                    
                    showDialog(view);
        
               return true;
                }
            });
        
        
            private void showDialog(View view) {
        
                AlertDialog.Builder adb = new AlertDialog.Builder(this);
                    adb.setView(view);//try it first with this line if it doesnt work delete it 
                    adb.setTitle("Title of alert dialog");
                    adb.setIcon(android.R.drawable.ic_dialog_alert);
                    adb.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
        
                            Toast.makeText(Menu.this, "OK", Toast.LENGTH_LONG).show();
                        } });
        
                    adb.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            Toast.makeText(Menu.this, "cancel", Toast.LENGTH_LONG).show();
                            //finish();
                        } });
                    
                    adb.create().show();
            }
    

    如果没有成功,请创建一个自定义布局,其中只包含对话框的主体,然后尝试以下操作:

    listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
                    @Override
                    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                        
                        showDialog(view);
            
                   return true;
                    }
                });
    
      private void showDialog() {
            // TODO Auto-generated method stub
            LayoutInflater inflater = this.getLayoutInflater();
    
            final View customview = inflater.inflate(
                    R.layout.your_dialog_layout, null);
    
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setView(customview);
            builder.setPositiveButton("Add", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                
            });
            builder.setNegativeButton("Cancel",
                    new DialogInterface.OnClickListener() {
    
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // TODO Auto-generated method stub
    
                        }
                    });
            Dialog dialog = builder.create();
            dialog.show();
        }
    

    我希望这能帮助你:)

        4
  •  0
  •   Josbel Luna    8 年前

    这是因为您正在尝试使用一个具有父级的视图,正如您所看到的那样。最好的方法是使用所需的相同布局放大另一个视图。

    尝试使用其他布局,使用相同的内容:

    然后:

    final AlertDialog.Builder adb = new AlertDialog.Builder(getBaseContext());
    View newView = View.inflate(getBaseContext(),R.layout.my_layout,null);
    adb.setView(newView);
    adb.setTitle("Title of alert dialog");
    adb.setIcon(android.R.drawable.ic_dialog_alert);
    adb.setPositiveButton("OK", new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int which) {
        Toast.makeText(Menu.this, "OK", Toast.LENGTH_LONG).show();
      }
    });
    
    adb.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int which) {
        Toast.makeText(Menu.this, "cancel", Toast.LENGTH_LONG).show();
        //finish();
      }
    });
    
    listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
      @Override
      public boolean onItemLongClick(AdapterView < ? > parent, View view, int position, long id) {
    
        adb.show();
        return true;
      }
    });