我发现了许多与我的问题相关的问题,但我无法解决我的问题,这就是我发布此问题的原因。我第一次使用firebase作为实时数据库。使用下面的代码,我试图通过单击添加按钮将值保存到firebase。但我的应用程序stops.Here是我的logcat
FATAL EXCEPTION: main
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
at com.example.xyz.finance.MainActivity.addArtist(MainActivity.java:88)
at com.example.xyz.finance.MainActivity.access$000(MainActivity.java:25)
at com.example.xyz.finance.MainActivity$1.onClick(MainActivity.java:75)
at android.view.View.performClick(View.java:5721)
at android.widget.TextView.performClick(TextView.java:10936)
at android.view.View$PerformClick.run(View.java:22620)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7406)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Artist
模态类
@IgnoreExtraProperties
public class Artist {
private String artistId;
private String artistName;
private String artistGenre;
public Artist(){
//this constructor is required
}
public Artist(String artistId, String artistName, String artistGenre) {
this.artistId = artistId;
this.artistName = artistName;
this.artistGenre = artistGenre;
}
public String getArtistId() {
return artistId;
}
public String getArtistName() {
return artistName;
}
public String getArtistGenre() {
return artistGenre;
}
}
Track
模态类
public class Track {
private String id;
private String trackName;
private int rating;
public Track() {
}
public Track(String id, String trackName, int rating) {
this.trackName = trackName;
this.rating = rating;
this.id = id;
}
public String getTrackName() {
return trackName;
}
public int getRating() {
return rating;
}
}
MainActivity
public class MainActivity extends AppCompatActivity {
//we will use these constants later to pass the artist name and id to another activity
public static final String ARTIST_NAME = "com.example.xyz.finance.artistname";
public static final String ARTIST_ID = "com.example.xyz.finance.artistid";
//view objects
EditText editTextName;
Spinner spinnerGenre;
Button buttonAddArtist;
ListView listViewArtists;
//a list to store all the artist from firebase database
List<Artist> artists;
//our database reference object
DatabaseReference databaseArtists;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//getting the reference of artists node
databaseArtists = FirebaseDatabase.getInstance().getReference("finance");
//getting views
editTextName = (EditText) findViewById(R.id.editTextName);
spinnerGenre = (Spinner) findViewById(R.id.spinnerGenres);
listViewArtists = (ListView) findViewById(R.id.listViewArtists);
buttonAddArtist = (Button) findViewById(R.id.buttonAddArtist);
//list to store artists
artists = new ArrayList<>();
//adding an onclicklistener to button
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();
}
});
}
/*
* This method is saving a new artist to the
* Firebase Realtime Database
* */
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);
//Saving the Artist
databaseArtists.child(id).setValue(artist);
//setting edittext to blank again
editTextName.setText("");
//displaying a success toast
Toast.makeText(this, "Artist added", Toast.LENGTH_LONG).show();
} else {
//if the value is not given displaying a toast
Toast.makeText(this, "Please enter a name", Toast.LENGTH_LONG).show();
}
}
}
答案 0 :(得分:0)
请尝试在“编辑开始”和“编辑结束”
之间编写代码
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
//Edit Started
Map<String, Object> params = new HashMap<>();
params.put("id", id);
params.put("name", name);
params.put("genre",genre);
//Saving the Artist
databaseArtists.child(id).updateChildren(params)
// Edit Ended
//setting edittext to blank again
editTextName.setText("");
//displaying a success toast
Toast.makeText(this, "Artist added", Toast.LENGTH_LONG).show();
} else {
//if the value is not given displaying a toast
Toast.makeText(this, "Please enter a name", Toast.LENGTH_LONG).show();
}
}
}