在Xamarin Android C中使用带有寻呼机适配器的View Pager的图像滑块#

时间:2018-05-30 08:48:44

标签: c# android xamarin android-viewpager android-pageradapter

我正在尝试使用带有pageradapter的viewpager创建imageslider。一切似乎都很好但是当我运行它时我得到错误并且它说“System.NullReferenceException:Object reference not not set to a object of object。”我不知道该怎么办。我尝试在我的适配器中添加覆盖对象,但仍然出错。

以下是我的代码:

SlidingImage_Adapter.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Util;
using Java.Lang;
using Android.Support.V4.View;
using Square.Picasso;

    namespace EFCAndroid
    {
        public class SlidingImage_Adapter : PagerAdapter
        {
            private int[] gridViewImage = { Resource.Drawable.cheese_1, Resource.Drawable.cheese_2, Resource.Drawable.cheese_3 };
            private LayoutInflater layoutInflater;
            private Context context;



            public SlidingImage_Adapter(Context context)
            {
                this.context = context;
                //this.gridViewImage = gridViewImage;
                //inflater = LayoutInflater.From(context);
            }


            public override int Count
            {
                get
                {
                    return gridViewImage.Length;
                }
            }

            public override bool IsViewFromObject(View view, Java.Lang.Object objects)
            {
                return view == objects;
            }

            public override Java.Lang.Object InstantiateItem(ViewGroup container, int position)
            {
                layoutInflater = (LayoutInflater)context.GetSystemService(Context.LayoutInflaterService);
                View view = layoutInflater.Inflate(Resource.Layout.slidingimages_layout, null);

                    ImageView imageview = view.FindViewById<ImageView>(Resource.Id.imageView);
                    Picasso.With(context)
                           .Load((gridViewImage[position]))
                           .Into(imageview);

                    ViewPager vp = (ViewPager)container;
                    vp.AddView(view, 0);


                return view;
            }

            public void destroyItem(ViewGroup container, int position, Java.Lang.Object objects)
            {
                ViewPager vp = (ViewPager)container;
                View view = (View)objects;
                vp.RemoveView(view);
            }


        }
    }

Activity_EFC_CHICKEN_TERIYAKI.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using SupportToolbar = Android.Support.V7.Widget.Toolbar;
using Android.Support.V7.App;
using Android.Support.Design.Widget;
using Android.Support.V4.Widget;
using Android.Support.V4.View;

namespace EFCAndroid
{
    [Activity(Label = "Activity_EFC_CHICKEN_TERIYAKI", Theme = "@style/Theme.DesignDemo")]
    public class Activity_EFC_CHICKEN_TERIYAKI : AppCompatActivity
    {
        private SupportToolbar mToolbar;
        ViewPager viewPager;
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.activity_efc_chicken_teriyaki);
            // Create your application here

            mToolbar = FindViewById<SupportToolbar>(Resource.Id.toolbar);

            SetSupportActionBar(mToolbar);
            SupportActionBar.Title = "EFC Chicken Teriyaki";
            SupportActionBar.SetDisplayHomeAsUpEnabled(true);
            SupportActionBar.SetDisplayShowHomeEnabled(true);

            viewPager = FindViewById<ViewPager>(Resource.Id.viewpager);
            SlidingImage_Adapter slideimageadapter = new SlidingImage_Adapter(this);
            viewPager.Adapter = slideimageadapter;

        }

        public override bool OnOptionsItemSelected(IMenuItem item)
        {
            switch(item.ItemId)
            {
                case Android.Resource.Id.Home:
                    Finish();
                    return true;
                default:
                    return base.OnOptionsItemSelected(item);
            }
        }
    }
}

以下是我的布局:

slidingimages_layout.axml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
  <ImageView
    android:id="@+id/imageView"
    android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:adjustViewBounds="true"
        android:layout_gravity="center"
        android:scaleType="centerCrop"/>

</LinearLayout>

activity_efc_chicken_terriyaki.axml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fitsSystemWindows="true">
  <android.support.v7.widget.Toolbar
      android:id="@+id/toolbar"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:minHeight="?android:attr/actionBarSize"
      android:background="?android:attr/colorPrimary"
      app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
      app:popupTheme="@style/ThemeOverlay.AppCompat.Dark"
      android:theme="@style/ThemeOverlay.AppCompat.Dark" />
  <android.support.v4.widget.DrawerLayout
      android:id="@+id/drawer_layout"
      android:layout_width="match_parent"
      android:layout_height="match_parent">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
      <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

      </android.support.v4.view.ViewPager>

    </RelativeLayout>
  </android.support.v4.widget.DrawerLayout>
</LinearLayout>

1 个答案:

答案 0 :(得分:0)

我自己解决了。错误未正确设置$ exec 5< file $ fun process 1 process 2 process 3 process 4 $ exec 5<&- # close the FD 底部的contentview