java - 从 Firebase 数据库检索数据时出错
问题描述
我的应用程序总是在启动时崩溃。我的代码基于从 firebase 实时数据库中检索数据并将值放入文本视图中。然后单击按钮,它将比较用户输入的答案和真实答案并检查它们是否匹配。也许我的代码被 android studio 放置的决赛破坏了,因为如果我不放置决赛将是错误的。也许错误在于从 firebase 检索数据的部分,因为这是我第一次在 android studio 中这样做。
MainActivity.java:
public class MainActivity extends AppCompatActivity {
TextView questionText;
TextInputEditText answerText;
TextView hintText;
Button button;
FirebaseDatabase database;
DatabaseReference myRef;
String question;
String hint;
String answer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().hide();
questionText = findViewById(R.id.question);
hintText = findViewById(R.id.hint);
answerText = findViewById(R.id.input_answer);
button = findViewById(R.id.button);
final setClass setClass = new setClass();
final int cnt = 1;
database = FirebaseDatabase.getInstance();
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// This method is called once with the initial value and again
// whenever data at this location is updated.
for (DataSnapshot dataSnapshot0 : dataSnapshot.getChildren()) {
dataSnapshot0.getValue(com.example.quizremastered.setClass.class);
setClass.SetQuestion(dataSnapshot0.child(String.valueOf(cnt)).getValue(com.example.quizremastered.setClass.class).GetQuestion());
setClass.SetHint(dataSnapshot0.child(String.valueOf(cnt)).getValue(com.example.quizremastered.setClass.class).GetHint());
setClass.SetAnswer(dataSnapshot0.child(String.valueOf(cnt)).getValue(com.example.quizremastered.setClass.class).GetAnswer());
}
}
@Override
public void onCancelled(DatabaseError error) {
}
});
questionText.setText(setClass.GetQuestion());
hintText.setText(setClass.GetHint());
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String answerInput = answerText.getEditableText().toString().trim();
if (answerInput.isEmpty()) {
answerText.setError("Field can't be empty");
} else {
answerText.setError(null);
if (answerInput == setClass.GetAnswer()) {
button.setBackgroundColor(GREEN);
}
//else
{
button.setBackgroundColor(GREEN);
}
}
}
});
设置类.java:
package com.example.quizremastered;
public class setClass {
private String question;
private String hint;
private String answer;
public setClass(){
}
public String GetQuestion() {
return question;
}
public String GetHint() {
return hint;
}
public String GetAnswer() {
return answer;
}
public void SetQuestion(String question) {
this.question = question;
}
public void SetHint(String hint) {
this.hint = hint;
}
public void SetAnswer(String answer) {
this.answer = answer;
}
}
活动主.xml:
<include
android:id="@+id/toolbar"
layout="@layout/toolbar" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="60dp">
<TextView
android:id="@+id/question"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Question:"
android:layout_marginTop="100sp"
android:textSize="30sp"
/>
<TextView
android:id="@+id/hint"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10sp"
android:text="Hint:"
android:layout_below="@id/question"
android:textSize="15sp"
/>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/input_answer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/hint"
app:counterEnabled="true"
app:counterMaxLength="20"
app:errorEnabled="true">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Answer"
android:layout_marginTop="20sp"
android:inputType="text"
android:maxLength="20"
android:textSize="20sp" />
</com.google.android.material.textfield.TextInputLayout>
<Button
android:id="@+id/button"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="Answer"
android:layout_below="@id/input_answer"
/>
解决方案
推荐阅读
- google-apps-script - 在应用脚本中使用函数 unzipGmail1(){ 时如何选择要使用的文件
- dolphindb - 更新表失败,交易值更新为列名
- c - 访问程序参数时出现分段错误(核心转储)
- kotlin - 即使没有绑定设备,获得“由于 pin 或密码不正确而无法与 [device_name] 配对”警报的可能性有多大?
- python - 在 python 中使用网络抓取获取详细信息时出错
- c++ - OpenCV C++ RTSP Stream VideoCapture 错误 - “操作正在进行中”
- android - [Android 的 ONNX 运行时]。我该怎么做才能解决这个问题?AssignNodesToEpsFromHashesImpl 未能找到内核定义哈希
- python - 如何在python中重写这个嵌套的for循环?
- javascript - 我想将我的 dexie 值更改为空字符串
- php - Magento 1.9 如何解决缓存存储刷新后的错误?