我正在使用 firebase 数据库,想要在列表视图中显示数据。当我调用 firebase 数据库的 onDataChange() 时,它显示空指针异常。我在谷歌和许多其他网站上找到了最好的方法,但没有任何东西可以帮助我摆脱这个错误,所以请帮助我,我在过去三个小时内陷入了这个错误......
这是名为 Articles_from_firebase.java 的 Mainactiviy:
public class Articles_from_fireabse extends AppCompatActivity {
EditText editTextName;
Spinner spinnerGenre;
Button buttonAddArtist;
ListView listViewArtists;
DatabaseReference databaseArtists;
//a list to store all the artist from firebase database
List<Artist> artists;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_articles_from_fireabse);
databaseArtists = FirebaseDatabase.getInstance().getReference("artists");
editTextName = (EditText) findViewById(R.id.editTextName);
spinnerGenre = (Spinner) findViewById(R.id.spinnerGenres);
listViewArtists = (ListView) findViewById(R.id.listViewArtists);
buttonAddArtist = (Button) findViewById(R.id.buttonAddArtist);
buttonAddArtist.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//calling the method addArtist()
//the method is defined below
//this method is actually performing the write operation
addArtist();
}
});
}
private void addArtist() {
//getting the values to save
String name = editTextName.getText().toString().trim();
String genre = spinnerGenre.getSelectedItem().toString();
//checking if the value is provided
if (!TextUtils.isEmpty(name)) {
//getting a unique id using push().getKey() method
//it will create a unique id and we will use it as the Primary Key for our Artist
String id = databaseArtists.push().getKey();
//creating an Artist Object
Artist artist = new Artist(id, name, genre);
}
}
蝴蝶刀刀
慕斯709654
相关分类