FirebaseRecyclerViewを使用しようとして、populateViewHolder()が呼び出されなかった問題を解決した後、Firebaseデータベース内のRecyclerViewの値を取得する古い方法を試みました。childEventListerer on nullオブジェクト参照、Firebaseデータベース
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.chris.storyboard/com.example.chris.storyboard.StoryPage}: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.database.ChildEventListener com.google.firebase.database.DatabaseReference.addChildEventListener(com.google.firebase.database.ChildEventListener)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2984) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045) at android.app.ActivityThread.-wrap14(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6776) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.database.ChildEventListener com.google.firebase.database.DatabaseReference.addChildEventListener(com.google.firebase.database.ChildEventListener)' on a null object reference at com.example.chris.storyboard.FirebaseHelper.retrieve(FirebaseHelper.java:51) at com.example.chris.storyboard.StoryPage.onCreate(StoryPage.java:98) at android.app.Activity.performCreate(Activity.java:6955) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2927) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045) at android.app.ActivityThread.-wrap14(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6776) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520) at com.android.internal.os.ZygoteInit.main(ZygoteInit.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);
}
「ヌルオブジェクト参照上で起動に失敗した」というエラーがRecyclerViewを表示するように感じるように開始し、次の行
adapter = new StoriesAdapter(this, helper.retrieve());
と
db.addChildEventListener(new ChildEventListener() {
がちょうど一致しない表示されます私!
ご協力いただきありがとうございます。
db = FirebaseDatabase.getInstance().getReference().child("Stories");
helper = new FirebaseHelper(db);
に乾杯
乾杯! :D – JavaStruggleIsReal