我正在开发一款Android应用,它有4个标签,包含4个片段。我在Android设备上运行了该应用。但我遇到的问题是,当我点击前两个标签时,它工作正常,但是当我打开第3和第4个标签时,APP立即崩溃!
最初,我认为第3和第4个碎片上有一个错误,但我后来发现这不是第3或第4个碎片错误。我用第3和第4个片段替换了第1个和第2个片段,第1个和第2个片段用第3个和第4个片段替换。同样的事情仍然发生了!
这是我的main_activity.java。
public class Main_activity extends AppCompatActivity {
@Bind(R.id.main_activity)
View mView;
private int[] tabIcons = {
R.drawable.friend_chat,
R.drawable.call,
R.drawable.tab_contact,
R.drawable.profile
};
private TabLayout tabLayout;
private FragmentManager mFragmentManager;
private FragmentTransaction mFragmentTransaction;
private NonSwipeableViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mFragmentManager = getSupportFragmentManager();
mFragmentTransaction = mFragmentManager.beginTransaction();
mFragmentTransaction.replace(R.id.viewpager, new ConversationsFragment()).commit();
viewPager = (NonSwipeableViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
setupTabIcons();
}
private void setupTabIcons() {
tabLayout.getTabAt(0).setIcon(tabIcons[0]);
tabLayout.getTabAt(1).setIcon(tabIcons[1]);
tabLayout.getTabAt(2).setIcon(tabIcons[2]);
tabLayout.getTabAt(3).setIcon(tabIcons[3]);
int tabIconColor = ContextCompat.getColor(Main_Intergister.this, R.color.holo_red_dark);
tabLayout.getTabAt(0).getIcon().setColorFilter(tabIconColor, PorterDuff.Mode.SRC_IN);
tabLayout.getTabAt(1).getIcon().setColorFilter(Color.parseColor("#a0a3a7"), PorterDuff.Mode.SRC_IN);
tabLayout.getTabAt(2).getIcon().setColorFilter(Color.parseColor("#a0a3a7"), PorterDuff.Mode.SRC_IN);
tabLayout.getTabAt(3).getIcon().setColorFilter(Color.parseColor("#a0a3a7"), PorterDuff.Mode.SRC_IN);
ColorStateList colors;
if (Build.VERSION.SDK_INT >= 23) {
colors = getResources().getColorStateList(R.color.ash, getTheme());
}
else {
colors = getResources().getColorStateList(R.color.ash);
}
for (int i = 0; i < tabLayout.getTabCount(); i++) {
TabLayout.Tab tab = tabLayout.getTabAt(i);
assert tab != null;
Drawable icon = tab.getIcon();
if (icon != null) {
icon = DrawableCompat.wrap(icon);
DrawableCompat.setTintList(icon, colors);
}
}
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
tab.getIcon().setColorFilter(tabIconColor, PorterDuff.Mode.SRC_IN);
switch (tab.getPosition()) {
case 0:
viewPager.setCurrentItem(0);
break;
case 1:
viewPager.setCurrentItem(1);
break;
case 2:
viewPager.setCurrentItem(2);
case 3:
viewPager.setCurrentItem(3);
break;
default:
break;
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
tab.getIcon().setColorFilter(Color.parseColor("#a0a3a7"), PorterDuff.Mode.SRC_IN);
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
private void setupViewPager(NonSwipeableViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new ConversationsFragment(), "Chats");
adapter.addFragment(new ContactsFragment(), "Calls");
adapter.addFragment(new CallsFragment(), "Contacts");
adapter.addFragment(new Profile_fragment(), "Profile");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return null;
}
}
}
这是我的main_activity.xml。
<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/main_activity"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:fitsSystemWindows="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay"
android:id="@+id/appbar">
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardElevation="4dp"
card_view:cardCornerRadius="0dp">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:id="@+id/toolbar"
android:layout_height="?attr/actionBarSize"
android:background="@color/white"
app:layout_scrollFlags="scroll|enterAlways"
app:elevation="6dp"
app:popupTheme="@style/AppTheme.PopupOverlay">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/toolbar_app_name"
android:text="@string/app_name"
android:textColor="@color/holo_red_dark"
android:textSize="30sp"
android:textStyle="bold"/>
</android.support.v7.widget.Toolbar>
</android.support.v7.widget.CardView>
</android.support.design.widget.AppBarLayout>
<com.solid.helpers.NonSwipeableViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardElevation="4dp"
card_view:cardCornerRadius="0dp">
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabIndicatorColor="@color/white"
app:tabIndicatorHeight="8dp"
app:elevation="6dp"
app:tabGravity="fill"
android:background="@color/white"/>
</android.support.v7.widget.CardView>
</LinearLayout>
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
答案 0 :(得分:2)
由于您有4个标签,请尝试在main_activity onCreate事件下添加以下代码,看看它是否可以解决您的问题。
viewpager.setOffscreenPageLimit(4);