现在问题如图下面的四个导航,每一个应该能对应加载一个Fragment,但是全部加载不出来
切换文字没有问题,也用debugger检查过了,可以确定bottombar的listener是正常的,代码可以执行关键代码activity_main.xml<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"> <LinearLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"> //<Textview/> 省略 <FrameLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:id="@+id/main_content">
</FrameLayout> </LinearLayout> <com.roughike.bottombar.BottomBar
android:id="@+id/bottomBar"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
app:bb_tabXmlResource="@xml/bottombar_tabs" />
</LinearLayout>
activity_main.java
loadFragmentHome() 也加载不出来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);
ft.setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(null);
ft.commitAllowingStateLoss();
break;
}
case R.id.tab_dashboard: {
mTextMessage.setText(R.string.title_dashboard);
mDashboard = new DashboardFragment();
ft.replace(R.id.main_content, mDashboard);
ft.setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(null);
ft.commitAllowingStateLoss();
break;
}
case R.id.tab_bbs: {
mTextMessage.setText(R.string.title_bbs);
mBBS = new BBSFragment();
ft.replace(R.id.main_content, mBBS);
ft.setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(null);
ft.commitAllowingStateLoss();
break;
}
case R.id.tab_profile: {
mTextMessage.setText(R.string.title_profile);
mProfile = new ProfileFragment();
ft.replace(R.id.main_content, mProfile);
ft.setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(null);
ft.commitAllowingStateLoss();
break;
}
default:{
Toast.makeText(getApplicationContext(),R.string.error_nulltab,Toast.LENGTH_SHORT).show();
}
}
}
}); 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(随便举个例子,四个都加载不出来)<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"> <TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/title_Home" /></FrameLayout>
HomeFragment.javaimport 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);
}
//其它省略
}
切换文字没有问题,也用debugger检查过了,可以确定bottombar的listener是正常的,代码可以执行关键代码activity_main.xml<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"> <LinearLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"> //<Textview/> 省略 <FrameLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:id="@+id/main_content">
</FrameLayout> </LinearLayout> <com.roughike.bottombar.BottomBar
android:id="@+id/bottomBar"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
app:bb_tabXmlResource="@xml/bottombar_tabs" />
</LinearLayout>
activity_main.java
loadFragmentHome() 也加载不出来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);
ft.setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(null);
ft.commitAllowingStateLoss();
break;
}
case R.id.tab_dashboard: {
mTextMessage.setText(R.string.title_dashboard);
mDashboard = new DashboardFragment();
ft.replace(R.id.main_content, mDashboard);
ft.setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(null);
ft.commitAllowingStateLoss();
break;
}
case R.id.tab_bbs: {
mTextMessage.setText(R.string.title_bbs);
mBBS = new BBSFragment();
ft.replace(R.id.main_content, mBBS);
ft.setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(null);
ft.commitAllowingStateLoss();
break;
}
case R.id.tab_profile: {
mTextMessage.setText(R.string.title_profile);
mProfile = new ProfileFragment();
ft.replace(R.id.main_content, mProfile);
ft.setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(null);
ft.commitAllowingStateLoss();
break;
}
default:{
Toast.makeText(getApplicationContext(),R.string.error_nulltab,Toast.LENGTH_SHORT).show();
}
}
}
}); 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(随便举个例子,四个都加载不出来)<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"> <TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/title_Home" /></FrameLayout>
HomeFragment.javaimport 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);
}
//其它省略
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货