Android Studio表示“java.lang.String无法强制转换为java. util. Map ...”并不断崩溃[已关闭]

2sbarzqh  于 2023-02-02  发布在  Android
关注(0)|答案(1)|浏览(211)

已关闭。此问题需要超过focused。当前不接受答案。
**想要改进此问题吗?**更新此问题,使其仅关注editing this post的一个问题。

3天前关闭。
Improve this question
我目前正在开发一个android应用程序,它可以让你输入城市、州和国家的名称,这些名称将被保存到firebase数据库并显示在谷歌Map上。(https://www.youtube.com/watch?v=R-7paUUXQIw&list=PL1lAE2C19gZNpc-y1FgkQECrYLXkOw7f7&index=11)教程,直到视频播放13分钟之前,一切都很好。当运行我的应用程序时,它不断崩溃,我附上了logcat的图片。在视频中,应用程序工作正常。目标是将街道,州和国家对象放入Map中,以便它们可以在列表视图中显示。
在我的代码中:

DatabaseReference databaseReference;
    FirebaseDatabase firebaseDatabase;
    List<Places> placesList = new ArrayList<>();
    Places places = new Places();
    ListView listView;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_list, container, false);
        listView = view.findViewById(R.id.listview);
        firebaseDatabase = FirebaseDatabase.getInstance("https://bingobuch-48b81-default-rtdb.firebaseio.com/");
        databaseReference = firebaseDatabase.getReference("placesinfo");
        databaseReference.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
                Map<String, String> map = (Map<String, String>) snapshot.getValue();
                places.setStreetAddress(map.get("streetAddress"));
                places.setState(map.get("streetAddress"));
                places.setCountry(map.get("streetAddress"));
                places.setImage(map.get("streetAddress"));

                placesList.add(places);

                MyAdapter myAdapter = new MyAdapter(getActivity(), placesList);
                listView.setAdapter(myAdapter);
            }


            @Override
            public void onChildChanged(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {

            }

            @Override
            public void onChildRemoved(@NonNull DataSnapshot snapshot) {

            }

            @Override
            public void onChildMoved(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {

            }

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

            }

        });

        return view;
    }```

It tells my on the line with:

map =(Map〈String,String〉)快照.getValue();

that "Unchecked cast: 'java.lang.Object' to 'java.util.Map<java.lang.String,java.lang.String>' ".

I have tried to gentrify My class as the program suggested but it changed nothing. I can't find any solutions online or in the original video.

I am quite new to mobile development so any help would be appreciated.
v8wbuo2f

v8wbuo2f1#

首先,你没有提到你的数据结构是什么样子的。根据这里描述的文档https://firebase.google.com/docs/database/android/lists-of-data
getValue()应返回数据架构。
所以应该是这样的:

databaseReference.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
            Place place = dataSnapshot.getValue(Place.class);
            places.setStreetAddress(place.streetAddress);
            places.setState(place.state);
            places.setCountry(place.country);
            places.setImage(place.image);

            placesList.add(places);

            MyAdapter myAdapter = new MyAdapter(getActivity(), placesList);
            listView.setAdapter(myAdapter);
        }

    });

其中Place是数据类。

相关问题