【问题标题】:Android Inflater/View ExceptionAndroid Inflater/查看异常
【发布时间】:2018-09-17 14:13:59
【问题描述】:

我正在尝试向 Profile.java 中的自定义 AlertDialog 添加布局(profile_upload) 第一次它工作正常。但是当我关闭 AlertDialog 并再次单击按钮时它会崩溃 sayng java.lang.IllegalStateException:指定的孩子已经有一个父母。您必须先在孩子的父母上调用 removeView() Profile.java

package com.example.accer.sportsgr;


import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Base64;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;

public class Profile extends AppCompatActivity implements View.OnClickListener {

    ImageButton profile_btn;
  AlertDialog.Builder alert;
    Bitmap bitmap;
    TextView tx_img_name;
    Button btn_prfil_upld,btn_pic_choose;
    ImageView img_prfl;
    View v;
    LayoutInflater inflater;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile);


        profile_btn=(ImageButton)findViewById(R.id.user_profile_photo);


         alert=new AlertDialog.Builder(this);
        inflater=Profile.this.getLayoutInflater();
        if (v == null) {
            v = inflater.inflate(R.layout.profile_upload, null, false);
        } else {
            ((ViewGroup) v.getParent()).removeView(v);
        }
        alert.setView(v);


        btn_prfil_upld=(Button)v.findViewById(R.id.btnUpload_profile_pic);
        btn_pic_choose=(Button)v.findViewById(R.id.btn_choose_profile_pic);
        img_prfl=(ImageView)v.findViewById(R.id.img_profile_upload);
        tx_img_name=(TextView)v.findViewById(R.id.tx_img_name);
        btn_prfil_upld.setOnClickListener(this);
        btn_pic_choose.setOnClickListener(this);


        profile_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                alert.setTitle("Update Profile Picture");
                alert.show();

            }
        });


    }

    private void showFileChooser() {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, "Select Picture"), 1);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == RESULT_OK && data != null && data.getData() != null)
        {
            Uri filepath=data.getData();
            try {
                bitmap= MediaStore.Images.Media.getBitmap(getContentResolver(),filepath);
                tx_img_name.setText(filepath.toString());
                img_prfl.setImageBitmap(bitmap);
            } catch (IOException e) {
                Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    public void onClick(View view) {
        int id=view.getId();
        switch (id)
        {
            case R.id.btn_choose_profile_pic :
                    showFileChooser();
                    break;
            case R.id.btnUpload_profile_pic:
                break;
        }
    }
}

profile_upload.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">


    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Choose Image"
        android:id="@+id/btn_choose_profile_pic" />


    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:id="@+id/img_profile_upload" />


   <TextView
       android:id="@+id/tx_img_name"
       android:textAlignment="center"
       android:gravity="center"
       android:textSize="20dp"
       android:layout_width="match_parent"
       android:layout_height="wrap_content" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Upload Image"
        android:id="@+id/btnUpload_profile_pic" />


</LinearLayout>

activity_profile.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/header_cover_image"
        android:layout_width="match_parent"
        android:layout_height="75dp"
        android:scaleType="centerCrop"
        android:src="@drawable/navigation_header_image"/>

    <ImageButton
        android:id="@+id/user_profile_photo"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:layout_below="@+id/header_cover_image"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="-60dp"
        android:background="@drawable/circular_imageview"
        android:elevation="5dp"
        android:padding="20dp"
        android:scaleType="centerCrop"
        android:clickable="true"/>
    <!-- android:src="@drawable/profile"-->

    <RelativeLayout
        android:id="@+id/profile_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/header_cover_image"
        android:background="#ebca0707"
        android:elevation="4dp"
        android:paddingBottom="24dp">


        <ImageView
            android:id="@+id/add_friend"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="25dp"
            android:src="@drawable/ic_action_user_add"
            android:layout_toLeftOf="@+id/drop_down_option_menu"/>

        <ImageView
            android:id="@+id/drop_down_option_menu"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_alignParentRight="true"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="16dp"
            android:src="@drawable/ic_action_overflow"/>

        <TextView
            android:id="@+id/user_profile_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="76dp"
            android:text="Name"
            android:textColor="#fff"
            android:textSize="24sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/user_profile_short_bio"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/user_profile_name"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="12dp"
            android:text="Email"
            android:textColor="#fff"
            android:textSize="14sp" />
    </RelativeLayout>

</RelativeLayout>

错误日志:

2-17 16:17:47.395 13465-13465/com.example.accer.sportsgr E/AndroidRuntime: FATAL EXCEPTION: main
                                                                            Process: com.example.accer.sportsgr, PID: 13465
                                                                            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:4454)
                                                                                at android.view.ViewGroup.addView(ViewGroup.java:4295)
                                                                                at android.view.ViewGroup.addView(ViewGroup.java:4267)
                                                                                at android.support.v7.app.AlertController.setupCustomContent(AlertController.java:613)
                                                                                at android.support.v7.app.AlertController.setupView(AlertController.java:452)
                                                                                at android.support.v7.app.AlertController.installContent(AlertController.java:215)
                                                                                at android.support.v7.app.AlertDialog.onCreate(AlertDialog.java:258)
                                                                                at android.app.Dialog.dispatchOnCreate(Dialog.java:389)
                                                                                at android.app.Dialog.show(Dialog.java:293)
                                                                                at android.support.v7.app.AlertDialog$Builder.show(AlertDialog.java:956)
                                                                                at com.example.accer.sportsgr.Profile$1.onClick(Profile.java:98)
                                                                                at android.view.View.performClick(View.java:5610)
                                                                                at android.view.View$PerformClick.run(View.java:22265)
                                                                                at android.os.Handler.handleCallback(Handler.java:751)
                                                                                at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                at android.os.Looper.loop(Looper.java:154)
                                                                                at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

【问题讨论】:

  • 显示activity_profile.xml的内容?

标签: android


【解决方案1】:
alertDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {



            }
        });

删除这里的所有视图,当你按下导航返回按钮时它会调用

【讨论】:

  • 我的回答对你有帮助还是你自己想出来的?
  • 是的,我听从了您的建议。再次感谢您。
  • 你会在我的答案左侧看到一个勾号...在投票计数器下方
【解决方案2】:

或者,您可以通过 AlertDialog.Builder.create() 而不是 AlertDialog.Builder 实例来保留 AlertDialog 实例。
如果您使用自定义视图,则会创建对话框,然后每次 AlertDialog.Builder.show() 都会将自定义视图附加到对话框窗口。

public class Profile extends AppCompatActivity implements View.OnClickListener {
    AlertDialog dialog;
    ImageButton profile_btn;
    TextView tx_img_name;
    Button btn_prfil_upld, btn_pic_choose;
    ImageView img_prfl;
    View v;
    LayoutInflater inflater;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile);

        profile_btn = (ImageButton) findViewById(R.id.user_profile_photo);
        profile_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(dialog == null) {
                    dialog = getBuilder().create();
                }
                dialog.setTitle("Update Profile Picture");
                dialog.show();

            }
        });
    }

    @NonNull
    private AlertDialog.Builder getBuilder() {
        AlertDialog.Builder alert = new AlertDialog.Builder(this);
        inflater = Profile.this.getLayoutInflater();
        if (v == null) {
            v = inflater.inflate(R.layout.profile_upload, null, false);
        } else {
            ((ViewGroup) v.getParent()).removeView(v);
        }
        alert.setView(v);


        btn_prfil_upld = (Button) v.findViewById(R.id.btnUpload_profile_pic);
        btn_pic_choose = (Button) v.findViewById(R.id.btn_choose_profile_pic);
        img_prfl = (ImageView) v.findViewById(R.id.img_profile_upload);
        tx_img_name = (TextView) v.findViewById(R.id.tx_img_name);
        btn_prfil_upld.setOnClickListener(this);
        btn_pic_choose.setOnClickListener(this);
        return alert;
    }

    public void onClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.btn_choose_profile_pic:
                break;
            case R.id.btnUpload_profile_pic:
                break;
        }
    }
}

【讨论】:

  • 对不起,请您解释一下有关 AlertDialog 、 AlertDialog.builder 、 Dialog 的更多信息。
  • 或者一个链接就可以了。我用谷歌搜索,但找不到满意的答案。
  • 我修改了你的部分代码。希望能给你一些帮助。
  • 帮助的tanq。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-10
  • 2016-08-27
  • 2023-03-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多