我正在处理片段,我想检查我的用户是否已注册(在Firebase上)或他是否离线(类似于您可能知道的Spotify)
无论如何,我正在研究以下代码:
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;
}
您可能会猜到,这给了我一个错误: java.lang.IllegalStateException:指定的子代已经有一个父代。您必须先在孩子的父母上调用removeView()。
对removeView()的研究没有使我明白这一点,所以我想这是错误的方法,对吗?
按要求提供完整的崩溃日志:
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)
答案 0 :(得分:0)
在获取当前用户的详细信息时,您需要调用getCurrentUser()
方法,该方法在您的代码中丢失。请尝试下面的代码段:
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
// User is signed in
} else {
// No user is signed in
}
有关其他信息,如果您需要查看更多有关如何管理用户相关信息的选项,可以选中official documentation
如果您需要检查用户设备的当前网络状态,可以查看此thread
中的答案。我希望它会有所帮助:)