数据从数据库加载时延迟应用程序

时间:2016-05-16 10:27:01

标签: java android events fragment blocking

我有问题,我需要加载用户的数据并在屏幕上显示他的详细信息 即时通讯在活动中使用片段,当我想在片段中打印细节时,它会调用空异常

现在我认为导致问题的原因是 - 数据仍然加载,当片段想要打印数据时,变量仍然指向NULL。

所以我的问题,我该怎么办, 1.阻止应用程序直到数据加载? 2.举办活动?

最重要的是如何?

TY !!

编辑:这是加载数据+初始化显示片段的viewpager,它将curr用户1加载到DataHelper类的静态字段中,所有内容都在onCreate()方法中:

firebaseRef = new Firebase(ReferenceUrl.FIREBASE_CHAT_URL);
    mAuthStateListener=new Firebase.AuthStateListener() {
        @Override
        public void onAuthStateChanged(AuthData authData) {
            if(authData!=null)
            {
                firebaseRef.child(ReferenceUrl.CHILD_USERS).addChildEventListener(new ChildEventListener() {
                    @Override
                    public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                        if (dataSnapshot.getKey().equals(DataHelper.currUid)) {
                            DataHelper.currUser = dataSnapshot.getValue(User.class);

                        }
                    }

                    @Override
                    public void onChildChanged(DataSnapshot dataSnapshot, String s) {


                    }

                    @Override
                    public void onChildRemoved(DataSnapshot dataSnapshot) {

                    }

                    @Override
                    public void onChildMoved(DataSnapshot dataSnapshot, String s) {

                    }

                    @Override
                    public void onCancelled(FirebaseError firebaseError) {

                    }
                });
            }
            else
            {
                LoadLogInActivity();
            }
        }
    };
    firebaseRef.addAuthStateListener(mAuthStateListener);

    //init viewpager and tabs for layout
    viewPager = (ViewPager) findViewById(R.id.viewpager);
    tabLayout = (TabLayout) findViewById(R.id.tabs);
    setupViewPager(viewPager);
    tabLayout.setupWithViewPager(viewPager);

setupviewpager方法:

  private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
    adapter.addFragment(FeedFragment.newInstance());
    adapter.addFragment(ChatFragment.newInstance());
    adapter.addFragment(SettingsFragment.newInstance());
    viewPager.setAdapter(adapter);
}

这是片段中的onCreateView:

    @Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.feed_layout,container,false);

    TextView t = (TextView)view.findViewById(R.id.textView);
    t.setText(DataHelper.currUser.getLastName());

    return view;
}

和给出null的行:

t.setText(DataHelper.currUser.getLastName());

0 个答案:

没有答案