我在 android 中构建了一个底部导航选项卡栏,它有 5 个片段,但问题是在应用程序启动时它不会从 firestore 数据库加载 recyclerview 列表。这个片段就是Home Fragment。但如果我随机打开另一个选项卡,然后进入主页选项卡,主页选项卡列表会自行加载,但不会在应用程序启动时自动加载下面是代码。请协助public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener { private static final String TAG = "DocSnippets"; private FirebaseAuth mauth; private FirebaseFirestore db = FirebaseFirestore.getInstance(); private CollectionReference UsersRef = db.collection("Users"); private DocumentReference noteRef = db.document("Notebook/My First Note"); private MySharedPreferences sp; private DocumentReference currentUserRef; private Button notificationList; private String mUserId, tokenId, currentUserProfilePic, currentUserName, currentUserSpinnerC; String currentUserID; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); sp = MySharedPreferences.getInstance(this); setContentView(R.layout.activity_main); mauth = FirebaseAuth.getInstance();// currentUserID = mauth.getCurrentUser().getUid(); UsersRef =FirebaseFirestore.getInstance().collection("Users"); mUserId = mauth.getUid(); tokenId = FirebaseInstanceId.getInstance().getToken(); Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); FloatingActionButton fab = findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } }); }
3 回答
慕无忌1623718
TA贡献1744条经验 获得超4个赞
您没有在 onCreate() 方法中加载/调用任何片段。
试试这个方法
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/**
* Your code
*/
loadFragment(LOAD_YOUR_FRAGMENT);
}
MYYA
TA贡献1868条经验 获得超4个赞
您可以手动触发主片段选择。将其粘贴到 MainActivity 的 onCreate 函数的末尾。
View view = navigation.findViewById(R.id.navigation_home); view.performClick();
添加回答
举报
0/150
提交
取消