有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java从Firebase检索数据时,导致应用程序停止的错误是什么?

这是一个使用Firebase数据库的应用程序

我已经在firebase中添加了所有数据,现在我需要检索它并使用listview显示

我试图从firebase获取并显示应用程序中的数据,但应用程序每次都会停止

看一下屏幕截图

enter image description here

这是国家车型类别

国家。爪哇

public class Country {

private String name;
private String total;
private String newCases;
private String totalDeaths;
private String newDeaths;
private String totalRecovered;
private String activeCases;
private String seriousCases;

public Country() {
}

public Country(String name, String total, String newCases, String totalDeaths, String newDeaths, String totalRecovered, String activeCases, String seriousCases) {
    this.name = name;
    this.total = total;
    this.newCases = newCases;
    this.totalDeaths = totalDeaths;
    this.newDeaths = newDeaths;
    this.totalRecovered = totalRecovered;
    this.activeCases = activeCases;
    this.seriousCases = seriousCases;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getTotal() {
    return total;
}

public void setTotal(String total) {
    this.total = total;
}

public String getNewCases() {
    return newCases;
}

public void setNewCases(String newCases) {
    this.newCases = newCases;
}

public String getTotalDeaths() {
    return totalDeaths;
}

public void setTotalDeaths(String totalDeaths) {
    this.totalDeaths = totalDeaths;
}

public String getNewDeaths() {
    return newDeaths;
}

public void setNewDeaths(String newDeaths) {
    this.newDeaths = newDeaths;
}

public String getTotalRecovered() {
    return totalRecovered;
}

public void setTotalRecovered(String totalRecovered) {
    this.totalRecovered = totalRecovered;
}

public String getActiveCases() {
    return activeCases;
}

public void setActiveCases(String activeCases) {
    this.activeCases = activeCases;
}

public String getSeriousCases() {
    return seriousCases;
}

public void setSeriousCases(String seriousCases) {
    this.seriousCases = seriousCases;
}
}

这是活动课程

国家/地区列表java

public class Country_List extends AppCompatActivity {

ListView listView;
FirebaseDatabase firebaseDatabase;
DatabaseReference reff;
ArrayList<String> countries;
ArrayAdapter<String> adapter;
Country country;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_country__list);
    listView = (ListView) findViewById(R.id.listView);
    country = new Country();
    firebaseDatabase = FirebaseDatabase.getInstance();
    reff = firebaseDatabase.getReference().child("country");
    countries = new ArrayList<>();
    adapter = new ArrayAdapter<>(Country_List.this, R.layout.country_info, R.id.country_info_list, countries);

    reff.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for (DataSnapshot ds: dataSnapshot.getChildren()){
                country = ds.getValue(Country.class);
                countries.add("Country Name:" + country.getName().toString() + "\n" + "Total Cases:" + country.getTotal().toString() + "\n" + "New Cases:" + country.getNewCases().toString() + "\n" + "Total Deaths:" + country.getTotalDeaths().toString() + "\n" + "New Deaths:" + country.getNewCases().toString() + "Total Recovered:" + country.getTotalRecovered().toString() + "Active Cases:" + country.getActiveCases().toString() + "\n" + "Serious Cases:" + country.getSeriousCases().toString());
            }
            listView.setAdapter(adapter);
        }

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

        }
    });
}

堆栈跟踪

com.google.firebase.database.DatabaseException: Failed to convert value of type java.lang.Long to String
    at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertString(com.google.firebase:firebase-database@@19.2.1:425)
    at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(com.google.firebase:firebase-database@@19.2.1:216)
    at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToType(com.google.firebase:firebase-database@@19.2.1:178)
    at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.access$100(com.google.firebase:firebase-database@@19.2.1:47)
    at com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.deserialize(com.google.firebase:firebase-database@@19.2.1:592)
    at com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.deserialize(com.google.firebase:firebase-database@@19.2.1:562)
    at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertBean(com.google.firebase:firebase-database@@19.2.1:432)
    at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(com.google.firebase:firebase-database@@19.2.1:231)
    at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToCustomClass(com.google.firebase:firebase-database@@19.2.1:79)
    at com.google.firebase.database.DataSnapshot.getValue(com.google.firebase:firebase-database@@19.2.1:203)
    at com.example.covid_19explorer.Country_List$1.onDataChange(Country_List.java:40)
    at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@19.2.1:75)
    at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@19.2.1:63)
    at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@19.2.1:55)
    at 安卓.os.Handler.handleCallback(Handler.java:789)
    at 安卓.os.Handler.dispatchMessage(Handler.java:98)
    at 安卓.os.Looper.loop(Looper.java:164)
    at 安卓.app.ActivityThread.main(ActivityThread.java:6541)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.安卓.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
    at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:767)

共 (1) 个答案

  1. # 1 楼答案

    要解决这个问题,请将Country类中的所有属性更改为long类型,但name除外,它应该保持为字符串。请同时更换所有的二传手和接球手。你的班级应该是这样的:

    public class Country {
        private String name;
        private long total, newCases, totalDeaths, newDeaths, totalRecovered, activeCases, seriousCases;
    
        public Country() {}
    
        public Country(String name, long total, long newCases, long totalDeaths, long newDeaths, long totalRecovered, long activeCases, long seriousCases) {
            this.name = name;
            this.total = total;
            this.newCases = newCases;
            this.totalDeaths = totalDeaths;
            this.newDeaths = newDeaths;
            this.totalRecovered = totalRecovered;
            this.activeCases = activeCases;
            this.seriousCases = seriousCases;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public long getTotal() {
            return total;
        }
    
        public void setTotal(long total) {
            this.total = total;
        }
    
        public long getNewCases() {
            return newCases;
        }
    
        public void setNewCases(long newCases) {
            this.newCases = newCases;
        }
    
        public long getTotalDeaths() {
            return totalDeaths;
        }
    
        public void setTotalDeaths(long totalDeaths) {
            this.totalDeaths = totalDeaths;
        }
    
        public long getNewDeaths() {
            return newDeaths;
        }
    
        public void setNewDeaths(long newDeaths) {
            this.newDeaths = newDeaths;
        }
    
        public long getTotalRecovered() {
            return totalRecovered;
        }
    
        public void setTotalRecovered(long totalRecovered) {
            this.totalRecovered = totalRecovered;
        }
    
        public long getActiveCases() {
            return activeCases;
        }
    
        public void setActiveCases(long activeCases) {
            this.activeCases = activeCases;
        }
    
        public long getSeriousCases() {
            return seriousCases;
        }
    
        public void setSeriousCases(long seriousCases) {
            this.seriousCases = seriousCases;
        }
    }
    

    您还需要做一件事,那就是这次将数据库中newCases属性的类型更改为long类型,因为它现在是一个字符串。不建议在数据库中添加加号(+),您应该以编程方式添加它