问题:更改Firebase数据库中的数据或在我将“疾病:”食物中毒“添加到每个节点时获取实例时,它会停止工作。我的代码已正确同步以访问数据库,最后一个密钥为“疾病”。因此,当将“疾病”作为叶节点添加到每个其他节点并再次运行代码时,它会显示“应用程序已停止”。
SymptomActivity.java
package com.example.nishantsikri.microdoctor;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
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 com.google.firebase.database.ValueEventListener;
import java.security.SecurityPermission;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class SymptomActivity extends AppCompatActivity {
private DatabaseReference mFirebaseDatabase, spinnerDatabase;
private FirebaseDatabase mFirebaseInstance;
Button button;
TextView textView,symptom1,symptom2,symptom3;
Spinner spinner,spinner2,spinner3;
String textSymptom1,textSymptom2,textSymptom3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_symptom);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
button = (Button) findViewById(R.id.button8);
textView = (TextView) findViewById(R.id.textView2);
symptom1 = (TextView) findViewById(R.id.symptom1);
symptom2 = (TextView) findViewById(R.id.symptom2);
symptom3 = (TextView) findViewById(R.id.symptom3);
spinner = (Spinner) findViewById(R.id.spinner1);
spinner2 = (Spinner) findViewById(R.id.spinner2);
spinner3 = (Spinner) findViewById(R.id.spinner3);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
// Write a message to the database
mFirebaseInstance = FirebaseDatabase.getInstance();
// store app title to 'app_title' node
mFirebaseInstance.getReference("app_title").setValue("microDoctor");
mFirebaseDatabase = mFirebaseInstance.getReference("symptomList");
spinnerDatabase = mFirebaseInstance.getReference("symptomView");
//mFirebaseDatabase.child("Headache").child("Some Value").setValue("Head");
//mFirebaseDatabase.setValue("Headache");
spinnerData();
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
textSymptom1 = spinner.getItemAtPosition(i).toString();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
textSymptom2 = spinner2.getItemAtPosition(i).toString();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
spinner3.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
textSymptom3 = spinner3.getItemAtPosition(i).toString();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
readDataListener();
}
});
}
private void readDataListener() {
mFirebaseDatabase.child(textSymptom1).child(textSymptom2).child(textSymptom3).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Symptom symptom = dataSnapshot.getValue(Symptom.class);
// Check for null
if (symptom == null) {
Log.e("Data", "Symptom data is null!");
return;
}
Log.e("Symptom Data", "User data is changed!"+symptom.getDisease());
// Display newly updated name and email
textView.setText(symptom.disease+"\n"+dataSnapshot.getKey());
}
@Override
public void onCancelled(DatabaseError error) {
// Failed to read value
Log.e("Error", "Failed to read user", error.toException());
}
});
}
private void spinnerData() {
spinnerDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.e("Spinner Data", "Spinner data is changed!");
//DataSnapshot data = dataSnapshot;
Iterable<DataSnapshot> temp = dataSnapshot.getChildren();
ArrayList<String> list = new ArrayList<>();
// list.add(dataSnapshot.getValue().toString());
// showDataInSpinner(list);
for (DataSnapshot lists : temp){
Log.d("ddd","Array List: "+lists.getValue().toString());
list.add(lists.getValue().toString());
textView.setText(lists.getValue().toString());
}
showDataInSpinner(list);
}
@Override
public void onCancelled(DatabaseError error) {
// Failed to read value
Log.e("Error", "Failed to read user", error.toException());
}
});
}
public void showDataInSpinner(ArrayList<String> data) {
ArrayAdapter<String> adapter = new ArrayAdapter<>(
this, android.R.layout.simple_spinner_item, data
); //Create the Adapter to set the data
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); //Set the layout resource to create the drop down views.
spinner.setAdapter(adapter); //Set the data to your spinner
spinner2.setAdapter(adapter);
spinner3.setAdapter(adapter);
}
}
logcat的
11-10 23:28:57.985 27635-27640/com.example.nishantsikri.microdoctor I/art: Do partial code cache collection, code=51KB, data=61KB
11-10 23:28:57.985 27635-27640/com.example.nishantsikri.microdoctor I/art: After code cache collection, code=51KB, data=61KB
11-10 23:28:57.985 27635-27640/com.example.nishantsikri.microdoctor I/art: Increasing code cache capacity to 256KB
11-10 23:29:06.787 27635-27635/com.example.nishantsikri.microdoctor D/AndroidRuntime: Shutting down VM
11-10 23:29:06.788 27635-27635/com.example.nishantsikri.microdoctor E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.nishantsikri.microdoctor, PID: 27635
com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.example.nishantsikri.microdoctor.Symptom
at com.google.android.gms.internal.zzekp.zzb(Unknown Source)
at com.google.android.gms.internal.zzekp.zza(Unknown Source)
at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
at com.example.nishantsikri.microdoctor.SymptomActivity$6.onDataChange(SymptomActivity.java:127)
at com.google.android.gms.internal.zzeex.zza(Unknown Source)
at com.google.android.gms.internal.zzegs.zzbwg(Unknown Source)
at com.google.android.gms.internal.zzegy.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6236)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:891)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:781)
答案 0 :(得分:1)
您正在尝试获取disease
的{{1}}值,以便获取它,使用
String
或
String symptom = dataSnapshot.child("disease").getValue(String.class);