簡體   English   中英

第一次使用monoDevelop(C#)進行Android編程的應用程序無法理解錯誤

[英]first time programming app for android using monoDevelop (C#) cant understand bug

Error CS0508: 'my_android_project.ImageAdapter.GetItem(int)': return type must be 'Java.Lang.Object' to match overridden member 'Android.Widget.BaseAdapter.GetItem(int)

這是我的第一堂課:

using System;

using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;

namespace my_android_project
{
    [Activity (Label = "my_android_project", MainLauncher = true)]
    public class Activity1 : Activity
    {
        int count = 1;

        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);

            // Set our view from the "main" layout resource
            SetContentView (Resource.Layout.Main);
            var gridview = FindViewById<GridView> (Resource.Id.gridview); 
            gridview.ItemClick += delegate(object sender, ItemEventArgs args) { 
                Toast.MakeText (this, EventArgs.Postion.ToString () , ToastLength.Short).Show(); 
            }; 

            // Get our button from the layout resource,
            // and attach an event to it
            Button button = FindViewById<Button> (Resource.Id.myButton);

            button.Click += delegate {
                button.Text = string.Format ("{0} clicks!", count++); };
        }
    }
}

二等艙:

using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;

namespace my_android_project
{
    // base class is ImageAdapter , derived class is BaseAdapter 

    /// <summary>
    /// class start 
    /// </summary> 
    public class ImageAdapter : BaseAdapter
{
    private readonly Context context;

    public ImageAdapter(Context c)
    {
        context = c;
    }

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

    public override Object GetItem(int position)
    {
        return null;
    }

    public override long GetItemId(int position)
    {
        return 0;
    }

        public override View GetView(int position, View convertView, ViewGroup parent)
    {
        ImageView imageView;

        if (convertView == null)
        {
            // if it's not recycled, initialize some attributes
            imageView = new ImageView(context);
            imageView.LayoutParameters = new AbsListView.LayoutParams(85, 85);
            imageView.SetScaleType(ImageView.ScaleType.CenterCrop);
            imageView.SetPadding(8, 8, 8, 8);
        }
        else
        {
            imageView = (ImageView) convertView;
        }
        imageView.SetImageResource(thumbIds[position]);
        return imageView;
    } // end of GetView 

        private readonly int[] thumbIds = { 
                                            Resource.Drawable.sample_2, 
                                          Resource.Drawable.sample_3,
                                          Resource.Drawable.sample_4, 
                                          Resource.Drawable.sample_5,
                                          Resource.Drawable.sample_6, 
                                          Resource.Drawable.sample_7,
                                          Resource.Drawable.sample_0, 
                                          Resource.Drawable.sample_1,
                                          Resource.Drawable.sample_2, 
                                          Resource.Drawable.sample_3,
                                          Resource.Drawable.sample_4, 
                                          Resource.Drawable.sample_5,
                                          Resource.Drawable.sample_6, 
                                          Resource.Drawable.sample_7,
                                          Resource.Drawable.sample_0, 
                                          Resource.Drawable.sample_1,
                                          Resource.Drawable.sample_2, 
                                          Resource.Drawable.sample_3,
                                          Resource.Drawable.sample_4, 
                                          Resource.Drawable.sample_5,
                                          Resource.Drawable.sample_6, 
                                          Resource.Drawable.sample_7        


        }; // end of thumbIds array 


    } // end of class
} // end of namespace 

看起來像我的第二個類Mono Develop的第32行不喜歡那個'null'return ..

有任何想法嗎? 我正在學習C#的過程中,而Android應用程序開發人員對此感到更自在:)

問題實際上是您的第二個GetItem()方法。 基類定義了一個抽象object getItem(int position) (每個http://developer.android.com/reference/android/widget/BaseAdapter.html ),因此您的第二個方法不會覆蓋任何內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM