问题
I have problem with my code. I'm learning how to create Simple Firebase CRUD application with Android . Here is my MainActivity code :
package com.ogigpermana.crudfirebaseapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;
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 com.ogigpermana.crudfirebaseapp.adapter.ArtistList;
import com.ogigpermana.crudfirebaseapp.model.Artist;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
EditText editTextName;
Button buttonAdd;
Spinner spinnerGenres;
DatabaseReference databaseArtists;
ListView listViewArtists;
List<Artist> artistList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
databaseArtists = FirebaseDatabase.getInstance().getReference("artists");
editTextName = (EditText) findViewById(R.id.editTextName);
buttonAdd = (Button) findViewById(R.id.buttonAddArtist);
spinnerGenres = (Spinner) findViewById(R.id.spinnerGenres);
listViewArtists = (ListView) findViewById(R.id.listViewArtists);
artistList = new ArrayList<>();
buttonAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
addArtist();
}
});
}
@Override
protected void onStart() {
super.onStart();
databaseArtists.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
artistList.clear();
for (DataSnapshot artistSnapshot: dataSnapshot.getChildren()){
Artist artist = artistSnapshot.getValue(Artist.class);
artistList.add(artist);
}
ArtistList adapter = new ArtistList(MainActivity.this, artistList);
listViewArtists.setAdapter(adapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
private void addArtist(){
String name = editTextName.getText().toString().trim();
String genre = spinnerGenres.getSelectedItem().toString();
if (!TextUtils.isEmpty(name)){
String id = databaseArtists.push().getKey();
Artist artist = new Artist(id, name, genre);
databaseArtists.child(id).setValue(artist);
Toast.makeText(this, "Artis berhasil ditambah", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(this, "Nama tidak boleh kosong", Toast.LENGTH_LONG).show();
}
}
}
And here is the errors code that displayed when i run :
07-03 00:30:45.727 30907-30907/com.ogigpermana.crudfirebaseapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.ogigpermana.crudfirebaseapp, PID: 30907
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference at com.ogigpermana.crudfirebaseapp.MainActivity$2.onDataChange(MainActivity.java:72) at com.google.android.gms.internal.zzbmz.zza(Unknown Source) at com.google.android.gms.internal.zzbnz.zzYj(Unknown Source) at com.google.android.gms.internal.zzboc$1.run(Unknown Source) 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:5441) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)
回答1:
Try this -
Cross check your ListView
Id in activity_main.xml
回答2:
First, I think you shouldn't create a new adapter in the callback. It's a bad design. You can notify your adapter when you change the data. I think the problem is that you may receive callback before you have the views, e. g. during configuration change (like device rotation).
You should be aware of the lifecycle. There are certain techniques which may make it easier like LiveData.
To sum up: don't create new adapter in the callback. Change the data and notify adapter using notifyDataSetChanged()
. Don't forget to unregister listeners during tear down of your activities/fragments and check that views exist.
来源:https://stackoverflow.com/questions/44873766/null-pointer-exception-attempt-to-invoke-virtual-method-void