The specified child already has a parent. You must call removeView() on the child's parent first in my chat application

被刻印的时光 ゝ 提交于 2019-12-14 02:43:20

问题


Guy's I am creating a chat application. My Text messages are uploaded successfully but I am trying to upload an image to my chat application to the server this time I have some error.

That is: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

This is my code for image upload:

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

Whats this problem. How to solve it.

This is my view Holder:

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

But I have an error on the image browsing button clicking:

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

            }
        });

回答1:


I think you are creating the application in the wrong way. You try to create a chat application means. first, pass the image picer in another activity and get it after upload image go back it same activity. Try This way.



来源:https://stackoverflow.com/questions/58117428/the-specified-child-already-has-a-parent-you-must-call-removeview-on-the-chil

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!