简体   繁体   English

片段中用户的不同视图

[英]Different View in Fragment for User

I'm working with fragments and I want to check if my User is registred (on Firebase) or if he's offline (similar to Spotify as you may know) 我正在处理片段,我想检查我的用户是否已注册(在Firebase上)或他是否离线(类似于您可能知道的Spotify)

Anyway, I'm working on the following Code: 无论如何,我正在研究以下代码:

FirebaseAuth mAuth;
private DatabaseReference myRef;
private FirebaseAuth.AuthStateListener mAuthListener;
private FirebaseDatabase mFirebaseDatabase;
private String userID;

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

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
            // Initialize Authentication Object
            mAuth = FirebaseAuth.getInstance();

    if (mAuth.getCurrentUser() != null) {

        View view = inflater.inflate(R.layout.fragment_unregistred, container, true);
        return view;
    } else {
        View view = inflater.inflate(R.layout.fragment_stats, container, true);
        return view;
    }

As you may guess, this gives me an Error: java.lang.IllegalStateException: The specified child already has a parent. 您可能会猜到,这给了我一个错误:java.lang.IllegalStateException:指定的子代已经有一个父代。 You must call removeView() on the child's parent first. 您必须先在孩子的父母上调用removeView()。

Research on removeView() didn't get me to the point, so I guess this is the wrong way to do it, am I right? 对removeView()的研究并没有使我明白这一点,所以我想这是错误的方法,对吗?

Full Crash Log as requested: 根据要求提供完整的崩溃日志:

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
        at android.view.ViewGroup.addViewInner(ViewGroup.java:4937)
        at android.view.ViewGroup.addView(ViewGroup.java:4768)
        at android.view.ViewGroup.addView(ViewGroup.java:4708)
        at android.view.ViewGroup.addView(ViewGroup.java:4681)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1434)
        at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1759)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1827)
        at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:797)
        at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2596)
        at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2383)
        at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2338)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2245)
        at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:703)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

When fetching details of the current user you need to call the getCurrentUser() method, which is missing in your code. 在获取当前用户的详细信息时,您需要调用getCurrentUser()方法,该方法在您的代码中丢失。 Please try with the snippet down below: 请尝试下面的代码段:

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
    // User is signed in
} else {
    // No user is signed in
}

For additional information and in case you need to look at more options on how to manage user's related information you can check the official documentation 有关其他信息,并且如果您需要查看更多有关如何管理用户相关信息的选项,可以查看官方文档。

If you need to check the current Network state of the user's device you can have a look at the answers in this thread 如果您需要检查用户设备的当前网络状态,则可以查看此线程中的答案

I hope that it helps :) 我希望它会有所帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM