gpt4 book ai didi

android - 指定的子项已有父项。您必须先在我的聊天应用程序中对 child 的 parent 调用 removeView()

转载 作者:行者123 更新时间:2023-11-29 22:48:51 28 4
gpt4 key购买 nike

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

即:java.lang.IllegalStateException: 指定的 child 已经有一个 parent 。您必须先对 child 的 parent 调用 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();

}
});

最佳答案

我认为您以错误的方式创建了应用程序。您尝试创建一个聊天应用程序的方式。首先,在另一个 Activity 中传递 image picer,并在上传图片返回同一个 Activity 后获取它。尝试这种方式。

关于android - 指定的子项已有父项。您必须先在我的聊天应用程序中对 child 的 parent 调用 removeView(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58117428/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com