使用onCreateView的Android片段但无法添加Firebase

时间:2018-09-01 17:47:36

标签: java android firebase firebase-realtime-database

我已经在我的应用程序中创建了一个片段,我正在尝试将Firebase添加到该片段中。但是由于面对它正在使用onCreateView的原因,因此它对此调用了错误。

public class ProfileFragment extends Fragment {

private DatabaseReference mUserDatabase;
private FirebaseUser mCurrentUser;

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

    mCurrentUser = FirebaseAuth.getInstance().getCurrentUser();

}

在mCurrentUser = FirebaseAuth.getInstance()。getCurrentUser();我收到错误无法到达的声明,有什么办法可以让我仍然为该活动使用片段吗?

    @Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    final View view =  inflater.inflate(R.layout.fragment_profile, container, false);
    mCurrentUser = FirebaseAuth.getInstance().getCurrentUser();

    String current_uid = Objects.requireNonNull(mCurrentUser).getUid();

    mUserDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child(current_uid);

    ValueEventListener valueEventListener = mUserDatabase.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

            String name = dataSnapshot.child("name").getValue().toString();
            String image = dataSnapshot.child("image").getValue().toString();
            String status = dataSnapshot.child("status").getValue().toString();
            String thumb_image = dataSnapshot.child("thumb_image").getValue().toString();

        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }

    return view;
    });}}

返回视图错误;

4 个答案:

答案 0 :(得分:1)

更新此

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_profile, container, false);
    mCurrentUser = FirebaseAuth.getInstance().getCurrentUser();
}

收件人

    @Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view =  inflater.inflate(R.layout.fragment_profile, container, false);
    mCurrentUser = FirebaseAuth.getInstance().getCurrentUser();
    String current_uid = Objects.requireNonNull(mCurrentUser).getUid();

    mUserDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child(current_uid);

    ValueEventListener valueEventListener = mUserDatabase.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

        String name = dataSnapshot.child("name").getValue().toString();
        String image = dataSnapshot.child("image").getValue().toString();
        String status = dataSnapshot.child("status").getValue().toString();
        String thumb_image = dataSnapshot.child("thumb_image").getValue().toString();

    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {

    }

    // you are calling return view inside the Value Listner that why you are getting the error
    });
    return view;
}

答案 1 :(得分:0)

只需在 return 语句之前将 mCurrentUser = FirebaseAuth.getInstance()。getCurrentUser()行移动。

答案 2 :(得分:0)

onCreateView()方法中返回语句之前,移动以下行。

 mCurrentUser = FirebaseAuth.getInstance().getCurrentUser();

return语句之后的所有内容均无法访问,如错误语句所述。

答案 3 :(得分:0)

onActivityCreated()方法处理其他工作。

ValueEventListenerDatabaseReference的内部类,因此,您无需在实例化对象之前创建对象。因此,删除该行。

还要提醒您,return view;应该是您onCreateView()中的最后一个语句。确保在关闭它之前您有return语句。

现在执行此操作。在onCreateView()之后,创建另一个副本并覆盖下面的代码。

@Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);



   mUserDatabase.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

            String name = dataSnapshot.child("name").getValue().toString();
            String image = dataSnapshot.child("image").getValue().toString();
            String status = dataSnapshot.child("status").getValue().toString();
            String thumb_image = 
             dataSnapshot.child("thumb_image").getValue().toString();

        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        });
}