android fragment 不显示不出来,Fragment无法加载,显示空白

现在问题如图

bVWFPH?w=533&h=925

下面的四个导航,每一个应该能对应加载一个Fragment,但是全部加载不出来

切换文字没有问题,也用debugger检查过了,可以确定bottombar的listener是正常的,代码可以执行

关键代码

activity_main.xml

xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto"

android:id="@+id/container"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

android:id="@+id/content"

android:layout_width="match_parent"

android:layout_height="0dp"

android:layout_weight="1"

android:orientation="horizontal">

// 省略

android:layout_width="match_parent"

android:layout_height="fill_parent"

android:id="@+id/main_content">

android:id="@+id/bottomBar"

android:layout_width="match_parent"

android:layout_height="60dp"

android:layout_alignParentBottom="true"

app:bb_tabXmlResource="@xml/bottombar_tabs" />

activity_main.java

import android.app.FragmentManager;

import android.app.FragmentTransaction;

import android.app.Fragment;

public class MainActivity extends AppCompatActivity{

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

mTextMessage = (TextView) findViewById(R.id.message);

BottomBar bottomBar = (BottomBar) findViewById(R.id.bottomBar);

bottomBar.setOnTabSelectListener(new OnTabSelectListener() {

@Override

public void onTabSelected(@IdRes int tabId) {

FragmentManager fm=getFragmentManager();

FragmentTransaction ft=fm.beginTransaction();

switch(tabId) {//四个都无法加载

case R.id.tab_home: {

mTextMessage.setText(R.string.title_home);

mHome = new HomeFragment();

ft.replace(R.id.main_content,mHome);

break;

}

case R.id.tab_dashboard: {

mTextMessage.setText(R.string.title_dashboard);

mDashboard = new DashboardFragment();

ft.replace(R.id.main_content, mDashboard);

break;

}

case R.id.tab_bbs: {

mTextMessage.setText(R.string.title_bbs);

mBBS = new BBSFragment();

ft.replace(R.id.main_content, mBBS);

break;

}

case R.id.tab_profile: {

mTextMessage.setText(R.string.title_profile);

mProfile = new ProfileFragment();

ft.replace(R.id.main_content, mProfile);

break;

}

default:{

Toast.makeText(getApplicationContext(),R.string.error_nulltab,Toast.LENGTH_SHORT).show();

}

}

ft.setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_FADE);

ft.addToBackStack(null);

ft.commitAllowingStateLoss();

}

});

loadFragmentHome();

}

//这个也加载不出来

public void loadFragmentHome(){

FragmentManager fm=getFragmentManager();

FragmentTransaction ft=fm.beginTransaction();

mHome=new HomeFragment();

ft.replace(R.id.main_content,mHome);

ft.commitAllowingStateLoss();

}

//其它省略

}

fragment_home.xml(随便举个例子,其它结构类似)

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent">

android:layout_width="match_parent"

android:layout_height="match_parent"

android:text="@string/title_Home" />

HomeFragment.java

import android.app.Fragment;

public class HomeFragment extends Fragment{

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container,

Bundle savedInstanceState) {

// Inflate the layout for this fragment

return inflater.inflate(R.layout.fragment_home, container, false);

}

//其它省略

}

已经尝试过将app.Fragment替换成support.v4.app.Fragment,无效

尝试过将MainActivity改为继承Activity或者FragmentActivity,皆无效

新手,轻喷

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值