在尝试使用FirebaseRecyclerView并遇到导致populateViewHolder()无法调用的问题后,我尝试了一种旧方法,在该方法中检索Firebase数据库中RecyclerView的值。然而,我得到了这个错误:
java.lang.RuntimeException:无法在null上启动活动ComponentInfo{com.example.chris.story/com.example.cchris.story.StoryPage}:java.lang.NullPointerException:尝试调用虚拟方法"com.google.firebase.database.ChildEventListener com.google.ffirebase.database.DatabaseReference.addChildEventListener(com.google.fire base.datalog.ChildEvent Listener)"对象引用在android.app.ActivityThread.performLaunchActivity(ActivityThreads.java:2984)在android.app.ActivityThread.handleLaunchActivity(ActivityThreads.java:3045)在android.app.ActivityThread.-wrap14(ActivityThreads.java)在android.app.ActivityThread$H.handleMessage(ActivityThreads.java:1642)在android.os.Handler.dispatchMessage(Handler.java:102)在android.os.Looper.loop(Looper.java:154)在android.app.ActivityThread.main上(ActivityThreads.java:6776)位于java.lang.reflect.Method.ioke(本机方法)网址:com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)网址:com.android.internal.os.ZygoteInit.main(ZygoteNit.java:1410)由以下原因引起:java.lang.NullPointerException:试图在null对象引用上调用虚拟方法"com.google.firebase.database.ChildEventListener com.google.ffirebase.database.DatabaseReference.addChildEventListener(com.google.fFirebase.datalog.ChildEvent Listener)"网址:com.example.chrs.storyboard.FirebaseHelper.retrieve(FirebaseHelper.java:51)网址:com.example.chrs.story.StoryPage.onCreate(StoryPage.java:98)在android.app.Activity.performCreate(Activity.java:66955)在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)在android.app.ActivityThread.performLaunchActivity(ActivityThreads.java:2927)在android.app.ActivityThread.handleLaunchActivity(ActivityThreads.java:3045)在android.app.ActivityThread.-wrap14(ActivityThreads.java)在android.app.ActivityThread$H.handleMessage(ActivityThreads.java:1642)在android.os.Handler.dispatchMessage(Handler.java:102)在android.os.Looper.loop(Looper.java:154)在android.app.ActivityThread.main上(ActivityThreads.java:6776)位于java.lang.reflect.Method.ioke(本机方法)网址:com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)网址:com.android.internal.os.ZygoteInit.main(ZygoteNit.java:1410)
我很难弄清楚为什么它将对象引用视为"null",因为我在另一个工作正常的类中使用了类似的方法。
这是我的其余代码:
FirebaseHelper.class
public class FirebaseHelper{
DatabaseReference db = FirebaseDatabase.getInstance().getReference();
Boolean isSaved = null;
ArrayList stories = new ArrayList<>();
FirebaseAuth firebaseAuth;
public FirebaseHelper(DatabaseReference db) {
this.db = db;
}
public Boolean Saved(Stories stories) {
if (stories == null) {
isSaved = false;
} else {
try {
db.child("Stories").push().setValue(stories);
isSaved = true;
} catch (DatabaseException e) {
e.printStackTrace();
isSaved = false;
}
}
return isSaved;
}
public ArrayList<String> retrieve() {
db.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Log.d("Testing: ", "Child added!");
// fetchData(dataSnapshot);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
Log.d("Testing: ", "Child changed!");
// fetchData(dataSnapshot);
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
return stories;
}
private void fetchData(DataSnapshot datasnapshot) {
stories.clear();
String storyTitle = datasnapshot.child("storyTitle").getValue(String.class);
stories.add(storyTitle);
}
}
StoriesAdapter.class
public class StoriesAdapter extends RecyclerView.Adapter<StoriesHolder> {
Context c;
ArrayList<String> stories;
public StoriesAdapter(Context c, ArrayList<String> stories) {
this.c = c;
this.stories = stories;
}
@Override
public StoriesHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v= LayoutInflater.from(c).inflate(R.layout.customlistview,parent,false);
return new StoriesHolder(v);
}
@Override
public void onBindViewHolder(StoriesHolder holder, int position) {
holder.titletxtview.setText(stories.get(position));
holder.cattxtview.setText(stories.get(position));
holder.createdbytxtview.setText(stories.get(position));
}
@Override
public int getItemCount() {
return stories.size();
}
StoriesHolder.class
public class StoriesHolder extends RecyclerView.ViewHolder {
TextView titletxtview;
TextView cattxtview;
TextView createdbytxtview;
public StoriesHolder(View itemView) {
super(itemView);
titletxtview = (TextView) itemView.findViewById(R.id.titleTxtView);
cattxtview = (TextView) itemView.findViewById(R.id.cattxtview);
createdbytxtview = (TextView) itemView.findViewById(R.id.createdbytxtview);
}
StoryPage.class
public class StoryPage extends AppCompatActivity {
Button homePage;
Button CreateStory;
Button logOut;
RecyclerView rv;
DatabaseReference db;
FirebaseHelper helper;
StoriesAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.storypage);
this.setTitle("Browse stories");
homePage = (Button) findViewById(R.id.homeBtn);
CreateStory = (Button) findViewById(R.id.CreateStoryBtn);
logOut = (Button) findViewById(R.id.logOutbtn);
rv = (RecyclerView) findViewById(R.id.storyRecyclerView);
rv.setLayoutManager(new LinearLayoutManager(this));
helper = new FirebaseHelper(db);
db = FirebaseDatabase.getInstance().getReference().child("Stories");
homePage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(StoryPage.this, Home.class));
}
});
CreateStory.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(StoryPage.this, CreateStory.class));
}
});
homePage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
FirebaseAuth.getInstance().signOut();
startActivity(new Intent(StoryPage.this, MainActivity.class));
}
});
Query query = FirebaseDatabase.getInstance()
.getReference()
.child("Stories")
.limitToLast(10);
FirebaseRecyclerOptions<Stories> options =
new FirebaseRecyclerOptions.Builder<Stories>()
.setQuery(query, Stories.class)
.build();
adapter = new StoriesAdapter(this, helper.retrieve());
rv.setAdapter(adapter);
}
"未能调用null对象引用"错误显示以下行
adapter = new StoriesAdapter(this, helper.retrieve());
和
db.addChildEventListener(new ChildEventListener() {
我开始觉得要显示RecyclerView了!
非常感谢您的帮助!干杯
更改
helper = new FirebaseHelper(db);
db = FirebaseDatabase.getInstance().getReference().child("Stories");
至
db = FirebaseDatabase.getInstance().getReference().child("Stories");
helper = new FirebaseHelper(db);
当db对象为null时,您正在初始化firebaseheper。这将解决问题。