首页 > 解决方案 > 指定的孩子已经有一个父母。在我的聊天应用程序中,您必须首先在孩子的父母上调用 removeView()

问题描述

伙计,我正在创建一个聊天应用程序。我的短信已成功上传,但这次我尝试将图像上传到我的聊天应用程序到服务器,但出现了一些错误。

即: java.lang.IllegalStateException: 指定的孩子已经有一个父母。您必须首先在孩子的父母上调用 removeView()。

这是我的图片上传代码:

 final String filename = "" + m[0];
            String s1 = filename;
            String f_name = s1.substring(7);
            String type = "image";
            DatabaseReference dbs = FirebaseDatabase.getInstance().getReference();
            Date today = new Date();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a");
            String dateToStr = format.format(today);
            ChatMessagepojo chatmsg = new ChatMessagepojo(profileimageurl, dateToStr, type, id, name, f_name);
            dbs.child("chats").push().setValue(chatmsg);
            alertDialogimage.dismiss();
            alertDialog.dismiss();

这是什么问题。如何解决它。

这是我的观点持有人:

package com.blood4pet.app.Adaptor;

import android.app.DownloadManager;
import android.content.Context;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.blood4pet.app.R;
import com.blood4pet.app.pojo.ChatMessagepojo;
import com.bumptech.glide.Glide;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;

import java.util.List;

import static android.os.Environment.DIRECTORY_DOWNLOADS;
import static androidx.constraintlayout.widget.Constraints.TAG;

public class ChatsList extends RecyclerView.Adapter<ChatsList.ViewHolder> {

    private static final int MSG_TYPE_LEFT = 0;
    private static final int MSG_TYPE_RIGHT = 1;
    private static final int MSG_IMAGE_TYPE_RIGHT = 2;
    private static final int MSG_IMAGE_TYPE_LEFT = 3;
    private Context mContext;
    private List<ChatMessagepojo> mChat;
    private String name;
    String type;

    FirebaseUser firebaseUser;
    StorageReference storageReference;
    StorageReference ref;


    public ChatsList(Context mContext, List<ChatMessagepojo> mChat, String name, String type) {
        this.mContext = mContext;
        this.mChat = mChat;
        this.name = name;
        this.type = type;
    }

    @NonNull
    @Override
    public ChatsList.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        if (viewType == MSG_IMAGE_TYPE_RIGHT) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.chat_item_image_right, parent, false);
            return new ChatsList.ViewHolder(view);
        }
        if (viewType == MSG_IMAGE_TYPE_LEFT) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.chat_item_image_left, parent, false);
            return new ChatsList.ViewHolder(view);
        }
        if (viewType == MSG_TYPE_RIGHT) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.chat_item_right, parent, false);
            return new ChatsList.ViewHolder(view);
        } else {
            View view = LayoutInflater.from(mContext).inflate(R.layout.chat_item_left, parent, false);
            return new ChatsList.ViewHolder(view);
        }

    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
        firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
        ChatMessagepojo chat = mChat.get(position);
        if (mChat.get(position).getSid().equals(firebaseUser.getUid()) && chat.getType().equals("image")) {
            Glide.with(mContext).load(chat.getMessage()).into(holder.loadedimage);
            holder.my_name.setText(chat.getName());
        } else if (mChat.get(position).getSid().equals(firebaseUser.getUid()) && chat.getType().equals("text")) {
            holder.show_message.setText(chat.getMessage());
            holder.my_name.setText(chat.getName());
        } else if (chat.getType().equals("image")) {
            Glide.with(mContext).load(chat.getMessage()).into(holder.loadedimage);
            holder.my_name.setText(chat.getName());

            holder.download_image_file.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int selectedposition = position;
                    ChatMessagepojo productid = mChat.get(position);
                    String message = productid.getMessage();
                    String filename = productid.getImagename();
                    downloadimage(message, filename);
                }
            });

        } else if (chat.getType().equals("text")) {
            holder.show_message.setText(chat.getMessage());
            holder.my_name.setText(chat.getName());
        }
    }

    private void downloadimage(String message, String file) {
        Log.d(TAG, "image url: " + message);
        storageReference = FirebaseStorage.getInstance().getReference().child("chat");
        final String filename = file;
        Log.d(TAG, "Original File Name: " + filename);
        ref = storageReference.child(filename + ".jpg");

        Log.d(TAG, "download Url: " + ref.getDownloadUrl());

        ref.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
            @Override
            public void onSuccess(Uri uri) {
                String url = uri.toString();

                download(mContext, filename, ".jpg", DIRECTORY_DOWNLOADS, url);
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {

            }
        });
//        download();
    }

    public void download(Context context, String filename, String file_extension, String destinationDirectory, String url) {
        DownloadManager downloadManager = (DownloadManager) mContext.getSystemService(Context.DOWNLOAD_SERVICE);
        Uri uri = Uri.parse(url);
        DownloadManager.Request request = new DownloadManager.Request(uri);
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_ONLY_COMPLETION);
        request.setDestinationInExternalFilesDir(context, destinationDirectory, filename + file_extension);
        Long refeerence = downloadManager.enqueue(request);
    }

    @Override
    public int getItemCount() {
        return mChat.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public TextView show_message;
        public TextView my_name;
        public ImageView loadedimage, download_image_file;


        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            loadedimage = (ImageView) itemView.findViewById(R.id.show_image);
            download_image_file = (ImageView) itemView.findViewById(R.id.download_image_file);
            show_message = (TextView) itemView.findViewById(R.id.show_message);
            my_name = (TextView) itemView.findViewById(R.id.my_name);

        }
    }

    @Override
    public int getItemViewType(int position) {
        firebaseUser = FirebaseAuth.getInstance().getCurrentUser();

        ChatMessagepojo chat = mChat.get(position);

        String type = chat.getType();

        if (mChat.get(position).getSid().equals(firebaseUser.getUid()) && type.equals("text")) {
            return MSG_TYPE_RIGHT;
        } else if (mChat.get(position).getSid().equals(firebaseUser.getUid()) && type.equals("image")) {
            return MSG_IMAGE_TYPE_RIGHT;
        } else if (type.equals("image")) {
            return MSG_IMAGE_TYPE_LEFT;
        } else {
            return MSG_TYPE_LEFT;
        }
    }
}

但我在点击图片浏览按钮时出现错误:

btn_attach = (ImageButton) findViewById(R.id.btn_attach);
 btn_attach.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                alertDialog = alert.create();
                alertDialog.setTitle("Select File Type");
//                alertDialog.getWindow().setGravity(Gravity.BOTTOM);
                alertDialog.show();

            }
        });

标签: androidchat

解决方案


我认为您以错误的方式创建应用程序。你尝试创建一个聊天应用程序的手段。首先,在另一个活动中传递图像picer,并在上传图像后返回相同的活动。试试这个方法。


推荐阅读