【问题标题】:View Pager without Fragment not working查看没有片段的寻呼机不起作用
【发布时间】: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


【解决方案1】:

您正在将您的活动布局设置为 activity_route_details.xml,其中不包含 viewpager 项目但 pager_route_details

在 RouteDetails.java 中更改这一行

setContentView(R.layout.activity_route_details);

setContentView(R.layout.route_details);

【讨论】:

  • 谢谢,所以我尝试了您的代码,现在我的 CustomPagerAdapter 第 36 行出现错误,它是 public boolean isViewFromObject(View view, Object object) { return view == ((LinearLayout) object );错误是 java.lang.ClassCastException: android.widget.ScrollView cannot be cast to android.widget.LinearLayout at com.example.zach.BirdsboroClimbing.CustomPagerAdapter.isViewFromObject(CustomPagerAdapter.java:36)
  • 尝试移除演员表。 == 不需要您强制转换对象或使用 equals 方法而不是 == 在任何情况下删除强制转换我认为没有必要
  • 我想通了!我确实更改为 view.equals(object);但后来我在第 41 行收到一个错误,说滚动视图只能有一个直接子级,所以我将按钮、文本视图和图像视图嵌套在另一个线性布局中。我还必须将我的相对布局更改为线性布局。最后,我必须将滚动视图从第一个线性布局外部移动到线性布局内部。但我会接受 uguboz 回复作为正确答案,因为这让我克服了最初的错误。谢谢大家!
【解决方案2】:

有两件事可能会导致您出现问题

A.你没有为你的RouteDetails Activity 使用正确的布局,你应该用 ViewPager 填充它

R.layout.pager_route_details

而不是

R.layout.activity_route_details

B.在您的适配器内部,您应该删除对LinearLayout 的强制转换== 运算符不需要您强制转换项目。我建议你使用

view.equals(object)

现在应该可以工作了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-19
    • 1970-01-01
    • 1970-01-01
    • 2016-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多