简体   繁体   English

Android Studio 在没有 Cast 的情况下无法构建,但应用程序在 Cast 时崩溃

[英]Android Studio Fails to Build Without Cast, But App Crashes With Cast

I'm currently building an app for Android which retrieves data from Firebase.我目前正在为 Android 构建一个应用程序,它从 Firebase 检索数据。 It displays children from other children .它显示来自其他孩子的孩子。 Either it fails to build when I remove the cast or the app crashes with the cast.当我删除演员表或应用程序与演员表崩溃时,它要么无法构建。 Here is the error:这是错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{app.name/app.name.retrieve}: java.lang.ClassCastException: android.widget.ListView cannot be cast to java.util.List java.lang.RuntimeException: 无法启动活动 ComponentInfo{app.name/app.name.retrieve}: java.lang.ClassCastException: android.widget.ListView 无法转换为 java.util.List

This is my code:这是我的代码:

package app.name;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

import java.util.ArrayList;
import java.util.List;

public class datab extends AppCompatActivity {
        private ArrayList<String> child = new ArrayList<>();

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        }
        @Override
        protected void onStart() {
            super.onStart();
            setContentView(R.layout.activity_datab);
            DatabaseReference database = FirebaseDatabase.getInstance().getReference().child("child");
            final ListView view = findViewById(R.id.l);


            final ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.list_content, (List) view);
//Here is the cast
            view.setAdapter(child);

            database.addChildEventListener(new ChildEventListener() {
                @Override
                public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                    String value = dataSnapshot.getValue(String.class);
                    adapter.add(value);
                    adapter.notifyDataSetChanged();
                }

                @Override
                public void onChildChanged(DataSnapshot dataSnapshot, String s) {

                }

                @Override
                public void onChildRemoved(DataSnapshot dataSnapshot) {

                }

                @Override
                public void onChildMoved(DataSnapshot dataSnapshot, String s) {

                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });
        }
    }

How can I remove the cast without the build failing?如何在不构建失败的情况下删除演员表?

Change this:改变这个:

final ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.list_content, (List) view);

to this:对此:

final ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, child);
view.setAdapter(adapter);

Change this inside your onChildAdded(..){ :在您的onChildAdded(..){更改此内容:

  adapter.add(value);

to this:对此:

  child.add(value);

add() is a method in the ArrayList. add()是 ArrayList 中的一个方法。

Check this for more info: https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html查看更多信息: https : //docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html

you can't cast ListView to List , ArrayAdapter constructor takes Context instance and resourceId and List or array您不能将ListViewListArrayAdapter构造函数采用Context实例和resourceId以及List或数组

you should pass the list to your adapter , and set the adapter to the listview您应该将列表传递给您的适配器,并将适配器设置为列表视图

final ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1 , child);
view.setAdapter(adapter)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android Studio数据库应用程序崩溃而没有错误 - android studio database app crashes without error 工具栏不会在设备上的应用中投射阴影(Android Studio) - Toolbar doesnt cast shadow in my app on my device (Android studio) 无法将协调器布局强制转换为android.support.v7.widget.Toolbar错误导致应用崩溃 - Coordinator layout cannot be cast to android.support.v7.widget.Toolbar error crashes the app Android Studio-应用崩溃 - Android Studio - App Crashes Android Studio 应用程序崩溃 - Android Studio app crashes 应用程序崩溃是因为build.gradle Android Studio中出现错误 - App crashes because an error in build.gradle Android Studio 没有应用程序类,我会收到此错误android.app.Application无法转换为android.app.Activity - without Application class I got this error android.app.Application cannot be cast to android.app.Activity android studio构建失败,因为“无法将类&#39;java.lang.Integer&#39;的对象&#39;11&#39;强制转换为类” - android studio build failed because of “Cannot cast object '11' with class 'java.lang.Integer' to class ” 如何在Android Studio中将AppBarLayout投射到CoordinatorLayout? - How to cast AppBarLayout to CoordinatorLayout in Android Studio? 为什么 TextInputEditText 在 Android Studio 中无法转换为 TextInputLayout? - Why TextInputEditText cannot be cast to TextInputLayout in Android Studio?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM