我使用Firebase数据库创建了实时测验应用程序,但是当我从数据库中检索整数值时,它会显示错误。
com.google.firebase.database.DatabaseException: Failed to convert value of type java.lang.Long to String
at com.google.android.gms.internal.zzepg.zzb(Unknown Source)
at com.google.android.gms.internal.zzepg.zza(Unknown Source)
at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
at com.ejobbox.ejobbox.SimpleQuiz$6.onDataChange(SimpleQuiz.java:164)
at com.google.android.gms.internal.zzejp.zza(Unknown Source)
at com.google.android.gms.internal.zzelk.zza(Unknown Source)
at com.google.android.gms.internal.zzelq.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:742)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:5523)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)
在com.ejobbox.ejobbox.SimpleQuiz $ 6.onDataChange(SimpleQuiz.java:164)
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();
DatabaseReference gOption1=mDatabase.child(mQuestionNumber+"/choice1");
gOption1.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String Option1=String.valueOf(dataSnapshot.getValue(String.class).toString());
mButtonChoice1.setText(Option1);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
在这个应用程序中我无法定义特定的数据类型,因为数据类型数据类型字段值将是字符串或整数想要在数据检索后转换它,所以请让我知道我该怎么做。
我使用String.valueOf()来转换数据,但它不起作用所以请帮助我如何将数据转换为字符串。
String Option1=String.valueOf(dataSnapshot.getValue(String.class).toString());
SimpleQuiz.Java
package com.ejobbox.ejobbox;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
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 com.google.firebase.database.ValueEventListener;
public class SimpleQuiz extends AppCompatActivity {
private TextView mScoreView;
private TextView mQuestionView;
private Button mButtonChoice1;
private Button mButtonChoice2;
private Button mButtonChoice3;
private Button mButtonChoice4;
private String mAnswer;
private int mScore = 0;
private int mQuestionNumber = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_quiz);
mScoreView = (TextView)findViewById(R.id.score);
mQuestionView = (TextView)findViewById(R.id.question);
mButtonChoice1 = (Button)findViewById(R.id.choice1);
mButtonChoice2 = (Button)findViewById(R.id.choice2);
mButtonChoice3 = (Button)findViewById(R.id.choice3);
mButtonChoice4 = (Button)findViewById(R.id.choice4);
updateQuestion();
//Start of Button Listener for Button1
mButtonChoice1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
//My logic for Button goes in here
if (mButtonChoice1.getText().equals(mAnswer)){
mScore = mScore + 1;
updateScore(mScore);
updateQuestion();
//This line of code is optiona
Toast.makeText(SimpleQuiz.this, "correct", Toast.LENGTH_SHORT).show();
mButtonChoice1.setBackgroundColor(Color.parseColor("#29b200"));
}else {
mButtonChoice1.setBackgroundColor(Color.parseColor("#c10000"));
Toast.makeText(SimpleQuiz.this, "wrong", Toast.LENGTH_SHORT).show();
updateQuestion();
}
}
});
//End of Button Listener for Button1
//Start of Button Listener for Button2
mButtonChoice2.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
//My logic for Button goes in here
if (mButtonChoice2.getText().equals(mAnswer)){
mScore = mScore + 1;
updateScore(mScore);
updateQuestion();
//This line of code is optiona
mButtonChoice2.setBackgroundColor(Color.parseColor("#29b200"));
Toast.makeText(SimpleQuiz.this, "correct", Toast.LENGTH_SHORT).show();
}else {
mButtonChoice2.setBackgroundColor(Color.parseColor("#c10000"));
Toast.makeText(SimpleQuiz.this, "wrong", Toast.LENGTH_SHORT).show();
updateQuestion();
}
}
});
//End of Button Listener for Button2
//Start of Button Listener for Button3
mButtonChoice3.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
//My logic for Button goes in here
if (mButtonChoice3.getText().equals(mAnswer)){
mScore = mScore + 1;
updateScore(mScore);
updateQuestion();
//This line of code is optiona
mButtonChoice3.setBackgroundColor(Color.parseColor("#29b200"));
Toast.makeText(SimpleQuiz.this, "correct", Toast.LENGTH_SHORT).show();
}else {
mButtonChoice3.setBackgroundColor(Color.parseColor("#c10000"));
Toast.makeText(SimpleQuiz.this, "wrong", Toast.LENGTH_SHORT).show();
updateQuestion();
}
}
});
//End of Button Listener for Button3
mButtonChoice4.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
//My logic for Button goes in here
if (mButtonChoice4.getText().equals(mAnswer)){
mScore = mScore + 1;
updateScore(mScore);
updateQuestion();
//This line of code is optiona
mButtonChoice4.setBackgroundColor(Color.parseColor("#29b200"));
Toast.makeText(SimpleQuiz.this, "correct", Toast.LENGTH_SHORT).show();
}else {
mButtonChoice1.setBackgroundColor(Color.parseColor("#c10000"));
Toast.makeText(SimpleQuiz.this, "wrong", Toast.LENGTH_SHORT).show();
updateQuestion();
}
}
});
}
private void updateQuestion(){
DatabaseReference mQuestion1 = FirebaseDatabase.getInstance().getReference();
DatabaseReference gQuestion1=mQuestion1.child(mQuestionNumber+"/question");
gQuestion1.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String question1=String.valueOf(dataSnapshot.getValue(String.class).toString());
mQuestionView.setText(question1);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();
DatabaseReference gOption1=mDatabase.child(mQuestionNumber+"/choice1");
gOption1.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String Option1=String.valueOf(dataSnapshot.getValue(String.class).toString());
mButtonChoice1.setText(Option1);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
DatabaseReference gOption2=mDatabase.child(mQuestionNumber+"/choice2");
gOption2.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String Option2=String.valueOf(dataSnapshot.getValue(String.class).toString());
mButtonChoice2.setText(Option2);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
DatabaseReference gOption3=mDatabase.child(mQuestionNumber+"/choice3");
gOption3.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String Option3=String.valueOf(dataSnapshot.getValue(String.class).toString());
mButtonChoice3.setText(Option3);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
DatabaseReference gOption4=mDatabase.child(mQuestionNumber+"/choice4");
gOption4.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String Option4=String.valueOf(dataSnapshot.getValue(String.class).toString());
mButtonChoice4.setText(Option4);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
DatabaseReference mQAnswer=mDatabase.child(mQuestionNumber+"/answer");
mQAnswer.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
mAnswer=String.valueOf(dataSnapshot.getValue(String.class));
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
mQuestionNumber++;
}
private void updateScore(int point) {
mScoreView.setText("" + mScore);
}
}
数据库结构
{
"0" : {
"answer" : "President",
"choice1" : "PM",
"choice2" : "AM",
"choice3" : "President",
"choice4" : "Vice President",
"question" : "who is Ramnath kovind ?"
},
"1" : {
"answer" : "Red",
"choice1" : "Red",
"choice2" : "Green",
"choice3" : "Black",
"choice4" : "Yellow",
"question" : "Rose color is___?"
},
"2" : {
"answer" : 3,
"choice1" : 5,
"choice2" : 4,
"choice3" : 3,
"choice4" : 2,
"question" : "How many color in indian flage ?"
},
"UpdateVersion" : 1,
"apkUrls" : "https://www.ejobbox.com"
}
答案 0 :(得分:1)
要解决此问题,请更改以下代码行:
String Option1=String.valueOf(dataSnapshot.getValue(String.class).toString());
与
String Option1=String.valueOf(dataSnapshot.getValue(Long.class));
请记住,dataSnapshot.getValue(Long.class)
会返回long
,这是原始的,您无法在基元类型上调用toString()
。
因为choice1
拥有不同类型的对象,为了解决问题,OP决定将所有值都作为字符串。所以不要使用以下代码行:
ref.child("choice1").setValue(5);
我们应该使用:
use ref.child("choice1").setValue("5");
使用quotation marks
,该值将存储为String。最后,为了使其工作,只需删除测试数据并添加新数据。
答案 1 :(得分:0)
可能有三种方法:
1st:这是一个非常简单的方法,但我不喜欢它。
String option1 = dataSnapshot.getValue() + ""; //it it works
第二:您需要检查您正在访问的数据的每种数据类型,并相应地进行转换。
if ((dataSnapshot.getValue()) instanceof Long )
{
String Option1=String.valueOf(dataSnapshot.getValue(Long.class));
}
else if ((dataSnapshot.getValue()) instanceof Double )
{
String Option1=String.valueOf(dataSnapshot.getValue(Double.class));
}
else if (...)
{
//and so on
}
第3名:您可以将数据库中的每个数据都作为字符串。怎么做?只需在每个数据后添加任何字母。例如,
"choice1" : 5,
"choice2" : 4,
"choice3" : 3,
"choice4" : 2,
将成为
"choice1" : 5L,
"choice2" : 4L,
"choice3" : 3L,
"choice4" : 2L,
这样每个数据都是“String”类型。然后使用
String Option1 = String.valueOf(dataSnapshot.getValue(String.class));
int indexno = Option1.indexOf("L");
Option1 = Option1.substring(0,indexno); //this line will remove "L" from the string