RecyclerView上的System.NullReferenceException - Xamarin Android

时间:2017-06-08 08:26:52

标签: c# xamarin

我遇到了教程问题。我一直在使用硬编码数据(工作正常),现在我正在尝试使用firebase中的实时数据。

我的数据库中已有一些数据,我正在尝试执行获取请求(获取请求)来更新我的RecyclerView。目前,我的RecyclerView充满了硬编码数据。我想要实现的是在我的firebase中添加数据以更新我的RecyclerView。基本上

在手机上部署应用程序并尝试保存数据后,保存数据的功能正常。我能够在我的firebase中看到实时保存的数据。但是当它到达 GameActivity.cs 中的 updateGameDataList 函数时会抛出异常。

该行的例外情况如下:     recycle.SetAdapter(adpater); 并抛出一个System.NullReferenceException

我可能会错过什么或做错了什么?谢谢

Games.cs

public class Games
{
    private string GameName;
    private int GameImage;

    public string Name
    {
        get { return GameName; }
        set { GameName = value; }
    }

    public int Image
    {
        get { return GameImage; }
        set { GameImage = value; }
    }
}

AllGames

// this is where I hardcoded my data for practice.
public class AllGames 
{
    public static JavaList<Games> GetAllGames()
    {
        JavaList<Games> getGames = new JavaList<Games>();

        Games adventureGames = new Games();

        adventureGames.Name = "Heroes";
        adventureGames.Image = Resource.Drawable.game1;

        getGames.Add(adventureGames);
    }
}

GameFragment

public class GameFragment : SupportFragment
{    
    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        // GameDataList contains recycleView to display all games
        // inflated into List_AllGames

        RecyclerView recyclerView = inflater.Inflate(Resource.Layout.GameDataList, container, false) as RecyclerView;

        GameRecyclerView(recyclerView);

        return recyclerView;
    }

    private void GameRecyclerView(RecyclerView recyclerView)
    {
        var bindGames = AllGames.GetAllGames();

        recyclerView.SetLayoutManager(new LinearLayoutManager(recyclerView.Context));

        var Adapter = new GameAdapter(AllGames.GetAllGames());

        recyclerView.SetAdapter(Adapter);

        Adapter.NotifyDataSetChanged();

        recyclerView.SetItemClickListener((rv, position, view) =>
        {
            Context context = view.Context;
            Intent intent = new Intent(context, typeof(GamesAttributesActivity));
            context.StartActivity(intent);
        });
    }

// Game Adapter here - this is the same adapter I used
// to display the hard coded data 
public class GameAdapter : RecyclerView.Adapter
{
    private JavaList<Games> myGames;

    public EventAdapter(JavaList<Games> myGames)
    {
        this.myGames = myGames;
    }

    public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
    {            
        GameHolder adventure = holder as GameHolder;
        adventure.GameName.Text = myGames[position].Name;
        adventure.GameImage.SetImageResource(peaceEvents[position].Image);
    }

    public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
    {
        View games = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.List_AllGames, parent, false);
        GameHolder holder = new GameHolder(games);

        return holder;
    }

    public override int ItemCount
    {  
        get { return myGames.Size(); }
    }
}

//Same holder used to hold my hardcoded data    
public class GameHolder : RecyclerView.ViewHolder
{    
    public TextView GameName;
    public ImageView GameImage;

    public GameHolder(View itemView) : base(itemView)
    {        
        //binding to List_AllGames inflated in GameFragment
        GameImage = itemView.FindViewById<ImageView>(Resource.Id.displayGameImage);
        GameName = itemView.FindViewById<TextView>(Resource.Id.displayGameText);
    }
}

GameActivity

private RecyclerView recycle;
JavaList<Games> NewGames = new JavaList<Games>();

private async void CreateNewGames()
{
    //code to add games      
    var firebase = new FirebaseClient(FirebaseUrl);

    //add item
    var item = await firebase.Child("Games").PostAsync<PeaceEvents>(addGames);

    await UpdateGameDataList();
}

//where recycleView must update new games added to the list
private async Task UpdateGameDataList()
{
    var firebase = new FirebaseClient(FirebaseUrl);
    var items = await firebase.Child("Games").OnceAsync<PeaceEvents>();

    NewGames.Clear();

    GameAdapter adapter;

    foreach(var item in items)
    {
        Games fetchGames = new Games();
        fetchGames.Name = item.Object.Name;
        NewGames.Add(fetchGames);
    }

    //fetching the id of the GameDataList recycleView
    recycle = FindViewById<RecyclerView>(Resource.Id.recyclerview);

    adapter = new GameAdapter(NewGames);
    recycle.SetAdapter(adapter);
    recycle.SetLayoutManager(new LinearLayoutManager(this));
    adapter.NotifyDataSetChanged();
}

0 个答案:

没有答案