Songnae
507
2021-02-24 15:41:07
1
89

안드로이드 dialog질문 있습니다 ㅠㅠㅠ


프래그먼트 안에서 dialog메시지를 띄워서 ok를 누르면 내용을 수정하는 기능을 구현 하고싶습니다.

MyApplication이라는 클래스를 만들어 applicationContext를 가져오고 dialog컨텍스트에 집어넣었습니다.

근데 이런 오류가 뜹니다 ㅠㅠㅠ

E/AndroidRuntime: FATAL EXCEPTION: main

    Process: kr.co.hanbit.chatapp, PID: 18486

    android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?

코드는 아래와 같습니다.


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

    ArrayList<Photo> photoItems;
    RequestManager requestManager;
    Context context;
    HashSet<String> nameSet;
    DatabaseReference databaseReference;
    String chatroomName;
    Context mainContext = MyApplication.ApplicationContext();

    public Fragment2CustomAdapter(ArrayList<Photo> photoItems, RequestManager requestManager, Context context, HashSet<String> nameSet, String chatroomName) {
        this.photoItems = photoItems;
        this.requestManager = requestManager;
        this.context = context;
        this.nameSet = nameSet;
        this.chatroomName = chatroomName;

        Log.d("Fragment2", "phtoItems 사이즈 = " + photoItems.size());
        Log.d("Fragment2", "phtoItems(0) = " + photoItems.get(0).getTitle());
    }

    @NonNull
    @Override
    public Fragment2CustomAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View itemView = inflater.inflate(R.layout.item_gallery, parent, false);
        Log.d("Fragment2", "Fragment2CustomAdapter onCreateViewHolder()호출");
        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull Fragment2CustomAdapter.ViewHolder holder, int position) {

        holder.item_gallery_title.setText(photoItems.get(position).getTitle());
        holder.item_gallery_time.setText(photoItems.get(position).getTime());

        Bitmap bitmap = StringToBitMap(photoItems.get(position).getImage());
        holder.item_gallery_photo.setImageBitmap(bitmap);

        holder.item_gallery_photo.setOnLongClickListener(new View.OnLongClickListener() {//이미지 길게 클릭시 삭제기능
            @Override
            public boolean onLongClick(View v) {



                //데이터 삭제
                databaseReference = FirebaseDatabase.getInstance().getReference();
                databaseReference.child("ChatApp").child("ChatRoom").child(chatroomName).child("Gallery").child("Photo").addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot snapshot) {
                        for(DataSnapshot dataSnapshot : snapshot.getChildren()){
                            Photo photo = dataSnapshot.getValue(Photo.class);
                            if(photo.getTitle().equals(photoItems.get(position).getTitle())){
                                Log.d("Fragment2", "photo.getTitle() = " + photo.getTitle());
                                dataSnapshot.getRef().removeValue();//삭제됨
                                break;
                            }
                        }
                        photoItems.remove(position);
                        notifyDataSetChanged();
                    }

                    @Override
                    public void onCancelled(@NonNull DatabaseError error) {

                    }
                });
                return false;
            }
        });

        holder.item_gallery_photo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Dialog dialog = new Dialog(mainContext, R.style.Theme_AppCompat_DayNight);
                dialog.setContentView(R.layout.dialog_update);

                EditText dialog_editText = dialog.findViewById(R.id.dialog_editText);
                Button dialog_ok = dialog.findViewById(R.id.dialog_ok);
                Button dialog_cancel = dialog.findViewById(R.id.dialog_cancel);

                dialog_ok.setOnClickListener(new View.OnClickListener() {//ok버튼 누르면 수정
                    @Override
                    public void onClick(View v) {
                        String updateTitle = dialog_editText.getText().toString();
                        String updateTime = new SimpleDateFormat("yy/MM/dd HH:mm").format(new Date());
                        holder.item_gallery_title.setText(updateTitle);
                        holder.item_gallery_time.setText(updateTime);

                        databaseReference = FirebaseDatabase.getInstance().getReference();
                        databaseReference.child("ChatApp").child("ChatRoom").child(chatroomName).child("Gallery").child("Photo").addListenerForSingleValueEvent(new ValueEventListener() {
                            @Override
                            public void onDataChange(@NonNull DataSnapshot snapshot) {
                                for(DataSnapshot dataSnapshot : snapshot.getChildren()){
                                    Photo photo = dataSnapshot.getValue(Photo.class);
                                    if(photo.getTitle().equals(photoItems.get(position).getTitle())){
                                        Log.d("Fragment2", "photo.getTitle() = " + photo.getTitle());
                                        dataSnapshot.getRef().child("title").setValue(updateTitle);
                                        dataSnapshot.getRef().child("time").setValue(updateTime);
                                        break;
                                    }
                                }
                                notifyDataSetChanged();
                            }

                            @Override
                            public void onCancelled(@NonNull DatabaseError error) {

                            }
                        });

                    }
                });
                dialog.show();

            }
        });

    }

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

    public void addItem(Photo item){
        photoItems.add(item);
        notifyItemChanged(0);

    }

    public Bitmap StringToBitMap(String encodedString){
        try {
            byte [] encodeByte= Base64.decode(encodedString,Base64.DEFAULT);
            Bitmap bitmap= BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
            return bitmap;
        } catch(Exception e) {
            e.getMessage();
            return null;
        }
    }


    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView item_gallery_photo;
        TextView item_gallery_title;
        TextView item_gallery_time;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            item_gallery_photo = itemView.findViewById(R.id.item_gallery_photo);
            item_gallery_title = itemView.findViewById(R.id.item_gallery_title);
            item_gallery_time = itemView.findViewById(R.id.item_gallery_time);
        }
    }



}


0
  • 답변 1

  • 이프얼스와일포
    225
    2021-02-24 16:41:13

    applicationContext 말고 인자로 넘어온 context 넣어보세요 다이어로그는 보면 상항 액티비티 context가 필요 하더군요

  • 로그인을 하시면 답변을 등록할 수 있습니다.