【发布时间】:2017-01-06 19:50:35
【问题描述】:
所以我正在尝试使用 viewpager 在屏幕之间滑动。我试图在不使用片段的情况下做到这一点,但我无法让它工作。我在第 59 行遇到错误,即 mViewPager.setAdapter(mCustomPagerAdapter);当我尝试设置我的适配器时出现空指针异常。我要离开我在这里找到的一个例子。我想我对此感到头疼,这是我第一次尝试使用视图寻呼机。
这是我尝试设置适配器的路线详细信息活动
public class RouteDetails extends AppCompatActivity {
CustomPagerAdapter mCustomPagerAdapter;
ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_route_details);
dbHandler = new MyDBHandler(this, null, null, 1);
//route detail xml array info
final CharSequence[] routeDetail= getResources().getTextArray(R.array.routeDetail);
///array of route images
final TypedArray image = getResources().obtainTypedArray(R.array.routeImages);
///////
mCustomPagerAdapter = new CustomPagerAdapter(this, routeDetail, image);
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mCustomPagerAdapter);
///////
}
这是我的 CustomPagerAdapter
class CustomPagerAdapter extends PagerAdapter {
CharSequence[] routeDetail;
TypedArray routeImageId;
Context mContext;
LayoutInflater mLayoutInflater;
public CustomPagerAdapter(Context context, CharSequence[] routeDetail, TypedArray routeImageId) {
mContext = context;
mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.routeDetail = routeDetail;
this.routeImageId = routeImageId;
}
@Override
public int getCount() {
return routeDetail.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((LinearLayout) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
View itemView = mLayoutInflater.inflate(R.layout.activity_route_details, container, false);
TextView routeText = (TextView) itemView.findViewById(R.id.routeDetailsView);
routeText.setText(routeDetail[position]);
final int imageId = routeImageId.getResourceId(position, 0);
ImageView imageView = (ImageView) itemView.findViewById(R.id.routeImage);
imageView.setImageResource(imageId);
container.addView(itemView);
return itemView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout) object);
}
}
感谢您的帮助!
编辑:添加相关的 xml 文件
activity_route_details.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_route_details"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.zach.BirdsboroClimbing.RouteDetails">
<CheckBox
android:text="Route Climbed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/routeCheckBox"
android:gravity="center" />
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/routeDetailsView"
android:textSize="18sp"
android:textAlignment="center"
android:textColor="@android:color/black"
android:layout_below="@id/routeCheckBox"/>
<ImageView
android:layout_below="@id/routeDetailsView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/routeImage"
android:scaleType="fitCenter"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:adjustViewBounds="true" />
</RelativeLayout>
</ScrollView>
pager_route_details.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
和 routeListViewItems.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="routeDetail">
<item><b>Shark Bait - 5.9</b>\n\n \u25BA 2 draws + 2 for anchor.\n</item>
<item><b>Rain Check - 5.8</b>\n\n \u25BA 3 draws + 2 for anchor.\n</item>
<item><b>Rain Check Direct - 5.8</b>\n\n \u25BA 4 draws + 2 for anchor.\n</item>
<item><b>Arocknophobia - 5.7</b>\n\n \u25BA 3 draws + 2 for anchor.\n</item>
<item><b>Balls Deep - 5.9+</b>\n\n \u25BA 3 draws + 2 for anchor.\n</item>
<item><b>Jingle Bells - 5.9</b>\n\n \u25BA 4 draws + 2 for anchor.\n</item>
<item><b>Itching to Climb - 5.8</b>\n\n \u25BA 5 draws + 2 for anchor.\n</item>
</string-array>
<integer-array name="routeImages">
<item>@drawable/sharkbait</item>
<item>@drawable/raincheck</item>
<item>@drawable/raincheckdirect</item>
<item>@drawable/arocknophobia</item>
<item>@drawable/ballsdeep</item>
<item>@drawable/jinglebells</item>
<item>@drawable/itchingtoclimb</item>
</integer-array>
</resources>
我希望它的工作方式是,我有一个 listview 主要活动,当您在列表视图中选择一个项目时,它会打开路线详细信息视图寻呼机活动的相应位置,从那里我希望能够滑动到上一个或下一个项目。
谢谢!
【问题讨论】:
-
您能否提供更多信息,例如日志。我们甚至不知道第 59 行是什么。
-
Sry 59 行是 mViewPager.setAdapter(mCustomPagerAdapter);当我到达我的电脑时,我会发布完整的日志
-
我尝试自己实现您的代码,它看起来不错,并且对我有用,您能否发布您的相关 XML 文件,以便我可以看到问题所在。
-
嘿,感谢您检查我仍然无法让它工作。我在原始帖子中添加了您要求的所有相关文件。谢谢
-
输入错误的地方
标签: android android-fragments android-viewpager