我通过我的应用程序成功将数据保存到firebase,但是当应用程序以空白开始并立即崩溃whiteout任何日志错误时,我面临检索firebase数据库数据的问题。 我搜索很多,但没有用,我也公开更改了firebase规则,但这里没有用我的代码请帮助我 这是MainActivity
dependencies {
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:27.0.2'
compile 'com.android.support:design:27.0.2'
compile 'com.android.support:cardview-v7:27.0.2'
compile 'com.android.support:recyclerview-v7:27.0.2'
compile 'com.android.support:support-v4:27.0.1-alpha1'
compile 'com.google.firebase:firebase-database:11.0.4'
compile 'com.google.firebase:firebase-auth:11.0.4'
compile 'com.facebook.android:facebook-share:4.30.0'
compile 'com.facebook.android:facebook-login:4.30.0'
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.firebaseui:firebase-ui-database:0.4.0'
testCompile 'junit:junit:4.12'
}
package com.jogiprasadpakki.firebaseapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.firebase.client.Firebase;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
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.jogiprasadpakki.firebaseapp.classes.Users;
public class Mainactivity extends AppCompatActivity {
private RecyclerView recyclerView;
private FirebaseDatabase firebase;
private DatabaseReference dataReference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firebase = FirebaseDatabase.getInstance();
dataReference = firebase.getReference().child("Users");
recyclerView = (RecyclerView)findViewById(R.id.story_adapter);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
}
@Override
protected void onStart() {
super.onStart();
FirebaseRecyclerAdapter<Users,BlogViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Users, BlogViewHolder>(
Userstories.class,
R.layout.single_post_row,
BlogViewHolder.class,
dataReference
) {
@Override
protected void populateViewHolder(BlogViewHolder viewHolder, Users model, int position) {
viewHolder.setNameText(model.getName());
viewHolder.setProfession(model.getprofession());
}
};
recyclerView.setAdapter(firebaseRecyclerAdapter);
}
public static class BlogViewHolder extends RecyclerView.ViewHolder {
View view;
public BlogViewHolder(View itemView) {
super(itemView);
view = itemView;
}
public void setNameText(String title){
TextView Title = (TextView)view.findViewById(R.id.txt_title);
Title.setText(title);
}
public void setProfession(String Story){
TextView story = (TextView)view.findViewById(R.id.txt_story);
story.setText(Story);
}
}
}
logcat的
02-02 10:01:15.297 10570-10570/com.jogiprasadpakki.firebaseapp I/FA: App measurement is starting up, version: 11020
02-02 10:01:15.297 10570-10570/com.jogiprasadpakki.firebaseapp I/FA: To enable debug logging run: adb shell setprop log.tag.FA VERBOSE
02-02 10:01:15.357 10570-10570/com.jogiprasadpakki.firebaseapp V/FA: Collection enabled
02-02 10:01:15.357 10570-10570/com.jogiprasadpakki.firebaseapp V/FA: App package, google app id: com.jogiprasadpakki.firebaseapp, 1:462629035094:android:dfe6370866bf9591
02-02 10:01:15.357 10570-10570/com.jogiprasadpakki.firebaseapp I/FA: To enable faster debug mode event logging run:
adb shell setprop debug.firebase.analytics.app com.jogiprasadpakki.firebaseapp
02-02 10:01:15.357 10570-10570/com.jogiprasadpakki.firebaseapp D/FA: Debug-level message logging enabled
02-02 10:01:15.377 10570-10570/com.jogiprasadpakki.firebaseapp V/FA: Registered activity lifecycle callback
02-02 10:01:15.397 10570-10589/com.jogiprasadpakki.firebaseapp V/FA: Using measurement service
02-02 10:01:15.397 10570-10589/com.jogiprasadpakki.firebaseapp V/FA: Connecting to remote service
02-02 10:01:15.397 10570-10589/com.jogiprasadpakki.firebaseapp V/FA: Using measurement service
02-02 10:01:15.397 10570-10589/com.jogiprasadpakki.firebaseapp V/FA: Connection attempt already in progress
02-02 10:01:15.477 10570-10570/com.jogiprasadpakki.firebaseapp V/FA: onActivityCreated
02-02 10:01:15.738 10570-10589/com.jogiprasadpakki.firebaseapp V/FA: Using measurement service
02-02 10:01:15.738 10570-10589/com.jogiprasadpakki.firebaseapp V/FA: Connection attempt already in progress
02-02 10:01:15.738 10570-10589/com.jogiprasadpakki.firebaseapp V/FA: Activity resumed, time: 10022170
02-02 10:01:15.738 10570-10589/com.jogiprasadpakki.firebaseapp I/FA: Tag Manager is not found and thus will not be used
02-02 10:01:15.748 10570-10589/com.jogiprasadpakki.firebaseapp D/FA: Logging event (FE): screen_view(_vs), Bundle[{firebase_event_origin(_o)=auto, firebase_screen_class(_sc)=SignUpActivity, firebase_screen_id(_si)=-1409030738035503793}]
02-02 10:01:15.818 10570-10589/com.jogiprasadpakki.firebaseapp V/FA: Using measurement service
02-02 10:01:15.818 10570-10589/com.jogiprasadpakki.firebaseapp V/FA: Connection attempt already in progress
02-02 10:01:15.878 10570-10589/com.jogiprasadpakki.firebaseapp D/FA: Connected to remote service
02-02 10:01:15.878 10570-10589/com.jogiprasadpakki.firebaseapp V/FA: Processing queued up service tasks: 4
02-02 10:01:15.998 32122-10608/? V/FA-SVC: Logging event: origin=auto,name=screen_view(_vs),params=Bundle[{firebase_event_origin(_o)=auto, firebase_screen_class(_sc)=SignUpActivity, firebase_screen_id(_si)=-1409030738035503793}]
02-02 10:01:16.008 32122-10608/? V/FA-SVC: Saving event, name, data size: screen_view(_vs), 60
02-02 10:01:16.008 32122-10608/? V/FA-SVC: Event recorded: Event{appId='com.jogiprasadpakki.firebaseapp', name='screen_view(_vs)', params=Bundle[{firebase_event_origin(_o)=auto, firebase_screen_class(_sc)=SignUpActivity, firebase_screen_id(_si)=-1409030738035503793}]}
02-02 10:01:16.018 32122-10608/? V/FA-SVC: Upload scheduled in approximately ms: 1942055
02-02 10:01:16.028 32122-10608/? V/FA-SVC: Scheduling upload with GcmTaskService
02-02 10:01:16.028 32122-10608/? V/FA-SVC: Scheduling task with Gcm. Time1942055
02-02 10:01:16.038 32122-10608/? V/FA-SVC: Background event processing time, ms: 47
答案 0 :(得分:0)
您正在onCreate方法中设置datareference的值,该方法将在onStart方法之后调用。在onStart方法中,传递的数据引用将为null。
因此,在onStart本身初始化datareference的值。
答案 1 :(得分:0)
避免在onStart()方法中填充RecyclerView。最好调用自定义函数并在那里实现代码。对于你,我建议使用这样的代码:
public void populateFeed(){
FirebaseRecyclerAdapter<Users,BlogViewHolder> firebaseRecyclerAdapter = new
FirebaseRecyclerAdapter<Users, BlogViewHolder>(
Userstories.class,
R.layout.single_post_row,
BlogViewHolder.class,
dataReference
) {
@Override
protected void populateViewHolder(BlogViewHolder viewHolder, Users model, int position) {
viewHolder.setNameText(model.getName());
viewHolder.setProfession(model.getprofession());
}
};
recyclerView.setAdapter(firebaseRecyclerAdapter);
}
在onCreate()结束时别忘了调用populateFeed()函数,
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firebase = FirebaseDatabase.getInstance();
dataReference = firebase.getReference().child("Users");
recyclerView = (RecyclerView)findViewById(R.id.story_adapter);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
populateFeed();
}