我有一个Activity(调用Home)扩展了SherlockFragmentActivity,它有5个Tabs。 每个Tab都是SherlockFragment。
这是布局home.xml:
<RelativeLayout 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" >
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</android.support.v4.view.ViewPager>
这是Home for Home:
public class ViewPagerAdapter extends FragmentPagerAdapter {
// Declare the number of ViewPager pages
final int PAGE_COUNT = 5;
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int arg0) {
switch (arg0) {
// Open FragmentTab1.java
case 0:
FragmentTab1 fragmenttab1 = new FragmentTab1();
return fragmenttab1;
// Open FragmentTab2.java
case 1:
FragmentTab2 fragmenttab2 = new FragmentTab2();
return fragmenttab2;
// Open FragmentTab3.java
case 2:
FragmentTab3 fragmenttab3 = new FragmentTab3();
return fragmenttab3;
case 3:
FragmentTab4 fragmenttab4 = new FragmentTab4();
return fragmenttab4;
// Open FragmentTab3.java
case 4:
FragmentTab5 fragmenttab5 = new FragmentTab5();
return fragmenttab5;
}
return null;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return PAGE_COUNT;
}
}
扩展SherlockFragment的每个Tab都有一个ListView。
我想要的只是当点击ListView的每个项目时,一个新的片段被替换在相同的位置。
我尝试使用Adapter for ListView,但我的应用程序强制丢失。
public class ListAdapter extends BaseAdapter {
......................
private FragmentManager mFragmentManager;
public ListAdapter(FragmentManager fm) {
this.mFragmentManager = fm;
}
..............
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
........................
convertView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Create new fragment and transaction
FragmentDetail newFragment = new FragmentDetail();
FragmentTransaction ft = mFragmentManager.beginTransaction();
// Replace fragmenttab1.xml layout. It's id is R.id.container
ft.replace(R.id.container, newFragment);
ft.addToBackStack(null);
// Commit the transaction
ft.commit();
}
});
return convertView;
}
}
这是LogCat。 LogCat中的ListVideoAdapter是上面的ListAdapter。
我该怎么做?
答案 0 :(得分:0)
您没有初始化convertView,并且当setOnclickListener
时您有nullpointerexceptionpublic View getView(final int position, View convertView, ViewGroup parent)