public class MainActivity extends AppCompatActivity {
FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance ();
DatabaseReference databaseReference = firebaseDatabase.getReference ();
ListView listView;
List<String> ListString = new ArrayList<String> ();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main22);
ListString.add ("Hello");
databaseReference.addValueEventListener (new ValueEventListener () {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
DataSnapshot snapshot = dataSnapshot.child ("User");
for(DataSnapshot s: snapshot.getChildren ())
Toast.makeText (MainActivity.this, s.getKey ().toString (), Toast.LENGTH_SHORT).show ();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
listView = (ListView) findViewById (R.id.listview);
ListAdapter listAdapter = new ListAdapter (MainActivity.this, ListString);
listView.setAdapter (listAdapter);
DatabaseReference s = databaseReference.child ("User");
DatabaseReference p = s.child ("Name");
p.setValue (ListString.get (1));
} }
正如我所说的,每当我调用此 Activity 时,它就会使应用程序崩溃,并且logcat是
最佳答案
这行导致错误
p.setValue (ListString.get (1));
您在ListString中只有一个值,并且访问第二个值,将其更改为以下值
p.setValue (ListString.get (0));