这是我的Stock Fragment //
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 //
<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类//
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 //
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"
/>
我的日志cat错误是
java.lang.NullPointerException:尝试调用虚方法'void android.support.v4.view.ViewPager.setAdapter(android.support.v4.view.PagerAdapter)” 在null对象引用上 在com.example.admin.navigationandtabbed.StockFragment.onCreateView(StockFragment.java:79) 在android.support.v4.app.Fragment.performCreateView(Fragment.java:2080) 在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1108) 在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1290) 在android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801)
说明---
StockFragment片段中的此代码中存在运行时错误 空指针异常的例外。 当我从Activity中执行它时,同样的运行正在运行,但是当我在Fragment上执行它时。这是投掷错误。
请帮助 先谢谢你。
答案 0 :(得分:1)
viewPager与viewpager不同
在您的StockFragment.java
上viewPager = (ViewPager) view.findViewById(R.id.viewpager);
将其更改为
viewPager = (ViewPager) view.findViewById(R.id.viewPager);