关于 java:Run time error while inflating SliderImages using Viewpager

Run time error while inflating SliderImages using Viewpager

这是我的股票片段 //

1
2
3
4
5
6
7
8
9
10
11
12
13
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_stock, container, false);
    slideDotspanel = (LinearLayout) view.findViewById(R.id.SliderDots);

    gridview = (GridView) view.findViewById(R.id.custom_grid);
    gridview.setAdapter(new CustomAdapter(this, osNameList, osImages));

    viewPager = (ViewPager) view.findViewById(R.id.viewpager);
    adapter = new ViewPagerAdapter(this.getActivity());
    viewPager.setAdapter(adapter);
    return view;

这是我的 stockfragment.xml //

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<android.support.v4.view.ViewPager
    android:layout_width="wrap_content"
    android:id="@+id/viewPager"
    android:layout_height="200dp"
    android:layout_marginBottom="8dp" />
<LinearLayout
    android:id="@+id/SliderDots"
    android:layout_below="@+id/viewPager"
    android:orientation="horizontal"
    android:gravity="center_horizontal|center_vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

</LinearLayout>

这是我的 ViewPagerAdapter 类 //

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
   class ViewPagerAdapter extends PagerAdapter {

     private int[] image_resource = {R.drawable.arrow, R.drawable.bin, R.drawable.gallery, R.drawable.got};
    private Context ctx;
    private LayoutInflater layoutInflater;

    public ViewPagerAdapter(Context ctx) {
        this.ctx = ctx;
    }

    @Override
    public int getCount() {
        return image_resource.length;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return (view == (ImageView)object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        layoutInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View item_view = layoutInflater.inflate(R.layout.custom_layout, container, false);
        ImageView imageview = (ImageView) item_view.findViewById(R.id.imageView);
                    imageview.setImageResource(image_resource[position]);

        container.addView(item_view);
        //((ViewPager) container).addView(item_view, 0);
        return item_view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((LinearLayout)object);
    }

这是 ViewPagerAdapter.xml //

1
2
3
4
5
6
7
8
9
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.AppCompatImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@mipmap/ic_launcher"
    android:id="@+id/imageView"
    />

我的日志猫错误是

java.lang.NullPointerException: Attempt to invoke virtual method 'void
android.support.v4.view.ViewPager.setAdapter(android.support.v4.view.PagerAdapter)'
on a null object reference
at com.example.admin.navigationandtabbed.StockFragment.onCreateView(StockFragment.java:79)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2080)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1108)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1290)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801)

解释---

在抛出的 StockFragment 片段中此代码存在运行时错误
空指针异常的异常。
当我从 Activity 执行此操作时,相同的部分正在运行,但是当我在 Fragment 执行此操作时。它正在抛出错误。

请帮助
提前谢谢你。


viewPager 不同于 viewpager

在你的 StockFragment.java

1
viewPager = (ViewPager) view.findViewById(R.id.viewpager);

改成

1
viewPager = (ViewPager) view.findViewById(R.id.viewpager);