我一直收到一个空对象错误,我的应用程序崩溃,但我不明白为什么会出现错误。我看过其他的例子,但是这些都是一样的,对我的问题没有帮助。我的问题虽然从我看到它不应该发生。代码是:
package com.example.matthew.myapplication;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
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.util.ArrayList;
import java.util.List;
import static android.content.ContentValues.TAG;
public class makecharact extends Activity {
Button retur, mak, cont;
FirebaseUser user;
DataSnapshot datasss;
Spinner spinner2;
List<String> list = new ArrayList<String>();
String id;
DatabaseReference myRef;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.charlist);
Intent task = getIntent();
retur = (Button) findViewById(R.id.returnToLast);
mak = (Button) findViewById(R.id.make);
cont = (Button) findViewById(R.id.Conti);
addListenerOnButton();
ValueEventListener postListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
datasss = dataSnapshot;
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
};
myRef = FirebaseDatabase.getInstance().getReference();
user = FirebaseAuth.getInstance().getCurrentUser();
Log.d("Test Point 1", "Got to line 61 at the least");
id = user.getUid();
int check = 0;
boolean done = false;
Log.d("User ID", id);
while(done == false) {
String sCheck = Integer.toString(check);
if (datasss.hasChild(id) == true){
if(datasss.child(id).hasChild("Character List") == true){
if (datasss.child(id).child("Character List").hasChild(sCheck) == true) {
list.add(datasss.child(id).child("Character List").child(sCheck).getValue().toString());
check = check + 1;
}else{
done = false;
}
}else{
list.add("No characters");
done = true;
}
}
}
ArrayAdapter<String> dataset = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, list);
dataset.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(dataset);
Log.d("Test Point 2", "Got to line 77 at the least");
}
public void addListenerOnButton() {
cont.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
String stats;
stats = spinner2.getSelectedItem().toString();
myRef.child(id).child("Current Character").setValue(stats);
Intent TaskIntent = new Intent(getApplicationContext(), MainScreen.class);
startActivity(TaskIntent);
finish();
}
});
mak.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Log.d(TAG, "Moving");
Intent TaskIntent = new Intent(getApplicationContext(), spinner.class);
startActivity(TaskIntent);
finish();
}
});
retur.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "Moving");
finish();
}
});
}
}
我的logcat显示:
05-31 21:05:39.802 14318-14318/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.matthew.myapplication, PID: 14318
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.matthew.myapplication/com.example.matthew.myapplication.makecharact}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.google.firebase.database.DataSnapshot.hasChild(java.lang.String)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3253)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)
at android.app.ActivityThread.access$1100(ActivityThread.java:223)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7223)
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)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.google.firebase.database.DataSnapshot.hasChild(java.lang.String)' on a null object reference
at com.example.matthew.myapplication.makecharact.addListenerOnButton(makecharact.java:81)
at com.example.matthew.myapplication.makecharact.onCreate(makecharact.java:45)
at android.app.Activity.performCreate(Activity.java:6877)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3206)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)
at android.app.ActivityThread.access$1100(ActivityThread.java:223)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7223)
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)
虽然我仍然不知道它为什么不起作用,但我已经尝试了很多。任何帮助都表示赞赏,即使它已经在另一篇文章中得到了解决(我无法通过链接查找该帖子。谢谢
答案 0 :(得分:0)
您可以获得这样的firebase数据。
DatabaseReference databaseTracks;
List<Track> tracks;
databaseTracks = FirebaseDatabase.getInstance().getReference("tracks").child(intent.getStringExtra(MainActivity.ARTIST_ID));
@Override
protected void onStart() {
super.onStart();
databaseTracks.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
tracks.clear();
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
Track track = postSnapshot.getValue(Track.class);
tracks.add(track);
}
TrackList trackListAdapter = new TrackList(ArtistActivity.this, tracks);
listViewTracks.setAdapter(trackListAdapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
答案 1 :(得分:0)
是的,它将返回Null指针异常因为您的变量dataass
未初始化。
并且仅在ValueEventListener()
为触发器onDataChange()
时初始化它因此您需要执行onDataChange()
ValueEventListener postListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
datasss = dataSnapshot;myRef = FirebaseDatabase.getInstance().getReference();
user = FirebaseAuth.getInstance().getCurrentUser();
Log.d("Test Point 1", "Got to line 61 at the least");
id = user.getUid();
int check = 0;
boolean done = false;
Log.d("User ID", id);
while(done == false) {
String sCheck = Integer.toString(check);
if (datasss.hasChild(id) == true){
if(datasss.child(id).hasChild("Character List") == true){
if (datasss.child(id).child("Character List").hasChild(sCheck) == true) {
list.add(datasss.child(id).child("Character List").child(sCheck).getValue().toString());
check = check + 1;
}else{
done = false;
}
}else{
list.add("No characters");
done = true;
}
}
}
ArrayAdapter<String> dataset = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, list);
dataset.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(dataset);
Log.d("Test Point 2", "Got to line 77 at the least");
}