private int current_posisition_page ;
@Override
protected v
oid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
.....
viewpager.setAdapter(new PagePagerAdapter(getSupportFragmentManager()));
viewpager.setCurrentItem(999);
viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
// This method will be invoked when a new page becomes selected.
@Override
public void onPageSelected(int position) {
current_posisition_page = position;
}
// This method will be invoked when the current page is scrolled
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
.......
}
private class PagePagerAdapter extends FragmentPagerAdapter {
public PagePagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int pos) {
return PageFragment.newInstance(pos);
}
@Override
public int getCount() {
return 1000;
}
}
}
这是我的片段
public class PageFragment extends Fragment {
public static PageFragment newInstance(int page_index) {
PageFragment fragment = new PageFragment();
Bundle args = new Bundle();
args.putString(Variabel.page_index, String.valueOf(page_index));
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
page_index = getArguments().getString(Variabel.page_index);
}
activity=(MainActivity)getActivity();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.item_page, container, false);
ButterKnife.bind(this, v);
return v;
}
public void setSelectedAyat(String aya_id, String sura_name) {
surat.setText("Surat " + sura_name);
ayat.setText("Ayat " + aya_id);
}
}
现在我从父活动调用方法当前选定的片段viewpager:
PageFragment page = (PageFragment) adapter.getItem(current_posisition_page);
page.setSelectedAyat(aya_id, sura_name);
但我收到错误:
java.lang.ClassCastException:java.lang.Integer无法强制转换为 com.ad.alquran.kemendag.fragment.PageFragment
排成一行:PageFragment page = (PageFragment) adapter.getItem(current_posisition_page);
那么如何解决呢?感谢
答案 0 :(得分:0)
这一行可能导致问题:
viewpager.setAdapter(new PagePagerAdapter(getSupportFragmentManager()));
因为adapter
对象用于调用getItem
方法但在ViewPager
setAdapter
方法中传递PagePagerAdapter
类的不同对象:
adapter=new PagePagerAdapter(getSupportFragmentManager());
viewpager.setAdapter(adapter);
并确保在为ViewPager设置适配器后调用getItem
方法。
修改强>
java.lang.NullPointerException:尝试调用虚方法'void android.widget.TextView.setText(java.lang.CharSequence)'为null 对象参考
因为没有初始化surat
和ayat
。这样做:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.item_page, container, false);
//...
surat=(TextView)v. findViewById(R.id.<surat_id_in_xml>);
ayat=(TextView)v. findViewById(R.id.<ayat_id_in_xml>);
return v;
}