查看没有片段的寻呼机不工作

时间:2017-01-06 19:50:35

标签: android android-fragments android-viewpager

所以我试图使用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);
}

}

感谢您的帮助!

编辑:添加relvant 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主要活动,当你在列表视图中选择一个项目时,它会打开路径详细信息视图分页器活动的相应位置,从那里我希望能够滑动到上一个或下一个项目。

谢谢!

2 个答案:

答案 0 :(得分:2)

您将ur活动布局设置为activity_route_details.xml,其中不包含viewpager项但是pager_route_details

在RouteDetails.java中更改此行

setContentView(R.layout.activity_route_details);

setContentView(R.layout.route_details);

答案 1 :(得分:0)

有两件事可能会导致你的问题

一个。您没有使用log_device_placement=True活动的正确布局,您应该使用RouteDetails中的ViewPager充气

R.layout.pager_route_details

而不是

R.layout.activity_route_details

B中。在您的适配器内部,您应该将转换移至LinearLayout ==运算符并不要求您投射项目。我建议你使用

view.equals(object)

现在应该工作。