我的应用程序出错了。 我想把viewPager放在片段中。 但是在viewPager上执行setCurrentItem时会出现一些问题。
public void clickBottomToolbar(View view) {
viewPager viewPager=new viewPager();
int position=0;
switch (view.getId()) {
case R.id.map:
position=0;
break;
case R.id.customerList:
position=1;
break;
case R.id.captureNameCard:
position=2;
break;
case R.id.captureDocument:
position=3;
break;
case R.id.documentList:
position=4;
break;
}
viewPager.setPosition(position);
FragmentTransaction fragmentTransaction=fm.beginTransaction();
fragmentTransaction.replace(R.id.fragment,viewPager);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
这是错误消息
public class viewPager extends Fragment {
ViewPager viewPager;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.viewpager,container,false);
viewPager=(ViewPager)view.findViewById(R.id.viewPager);
viewPager.setAdapter(new viewPagerAdapter(view.getContext()));
return view;
}
public void setPosition(int position){
viewPager.setCurrentItem(position);
return;
}
}
Upper是main_activitiy.java中的代码,Lower是viewPager.java
<RelativeLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
</android.support.v4.view.ViewPager>
</RelativeLayout>
这是viewpager.xml
{{1}}