问题在于标签活动。当我运行这个应用程序时工作正常。但是当我滑动或点击标签时,它完全关闭。请任何人帮助我。
11-14 12:08:12.460 30777-30777/? I/art: Late-enabling -Xcheck:jni
11-14 12:08:12.535 30777-30777/? I/System.out: Sending WAIT chunk
11-14 12:08:12.729 30777-30783/com.example.mn.tabt I/art: Debugger is active
11-14 12:08:12.736 30777-30777/com.example.mn.tabt I/System.out: Debugger has connected
11-14 12:08:12.736 30777-30777/com.example.mn.tabt I/System.out: waiting for debugger to settle...
11-14 12:08:12.936 30777-30777/com.example.mn.tabt I/System.out: waiting for debugger to settle...
11-14 12:08:13.136 30777-30777/com.example.mn.tabt I/System.out: waiting for debugger to settle...
11-14 12:08:13.336 30777-30777/com.example.mn.tabt I/System.out: waiting for debugger to settle...
11-14 12:08:13.537 30777-30777/com.example.mn.tabt I/System.out: waiting for debugger to settle...
11-14 12:08:13.737 30777-30777/com.example.mn.tabt I/System.out: waiting for debugger to settle...
11-14 12:08:13.938 30777-30777/com.example.mn.tabt I/System.out: waiting for debugger to settle...
11-14 12:08:14.138 30777-30777/com.example.mn.tabt I/System.out: waiting for debugger to settle...
11-14 12:08:14.339 30777-30777/com.example.mn.tabt I/System.out: debugger has settled (1406)
11-14 12:08:14.356 30777-30777/com.example.mn.tabt W/System: ClassLoader referenced unknown path: /data/app/com.example.mn.tabt-1/lib/arm64
11-14 12:08:14.399 30777-30777/com.example.mn.tabt W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
11-14 12:08:14.544 30777-30777/com.example.mn.tabt I/HwSecImmHelper: mSecurityInputMethodService is null
11-14 12:08:14.557 30777-30777/com.example.mn.tabt E/HAL: load: id=gralloc != hmi->id=gralloc
11-14 12:08:14.649 30777-30849/com.example.mn.tabt E/HAL: load: id=gralloc != hmi->id=gralloc
11-14 12:08:14.649 30777-30849/com.example.mn.tabt I/OpenGLRenderer: Initialized EGL, version 1.4
11-14 12:08:14.655 30777-30849/com.example.mn.tabt W/OpenGLRenderer: load: so=/system/lib64/libhwuibp.so
dlopen failed: library "/system/lib64/libhwuibp.so" not found
11-14 12:08:14.655 30777-30849/com.example.mn.tabt W/OpenGLRenderer: Initialize Binary Program Cache: Load Failed
11-14 12:08:14.655 30777-30849/com.example.mn.tabt E/HAL: load: id=gralloc != hmi->id=gralloc
11-14 12:08:21.698 30777-30777/com.example.mn.tabt I/Process: Sending signal. PID: 30777 SIG: 9
这里是logcat文件。应用程序运行顺畅,但当我尝试查找选项卡时,它会很紧张。
public class tab1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tab1, container, false);
return rootView;
}
}
`
这里是tab1类。同样,我创建了两个class.for 3片段。 `
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/appbar_padding_top"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/AppTheme.PopupOverlay">
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
如果有人想查看主类的xml文件。这里是 。
`
block
`
答案 0 :(得分:0)
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
tab1 tab1 = new tab1();
return tab1;
case 1:
tab2 tab2 = new tab2();
return tab2;
case 3: // Should be 2
tab3 tab3 =new tab3();
return tab3;
default: return null ;
}
首先,请纠正 CASE
部分。
case 2:
tab3 tab3 =new tab3();
return tab3;
答案 1 :(得分:0)
此问题还有另一种解决方案
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
setupViewPager(mViewPager); //Replace your line to this
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
FloatingActionButton fab = (FloatingActionButton) 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();
}
});
}
//method for adding fragment to the adapter
private void setupViewPager(ViewPager viewPager) {
SectionsPagerAdapter adapter = new SectionsPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new TabOne(), "ONE");
adapter.addFragment(new TabSecond(), "TWO");
adapter.addFragment(new TabThird(), "THREE");
viewPager.setAdapter(adapter);
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
// add this method in your adapter
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}