尝试从字段'android.view.View android.support.v7.widget.RecyclerView $ ViewHolder.itemView'中读取 [英] Attempt to read from field 'android.view.View android.support.v7.widget.RecyclerView$ViewHolder.itemView'

查看:341
本文介绍了尝试从字段'android.view.View android.support.v7.widget.RecyclerView $ ViewHolder.itemView'中读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图延迟随机选择recyclerview项.我需要在片段加载后启动随机选择方法,而无需任何用户交互,但是会出现以下错误.之后,将其放在ImageView上单击以进行检查,但再次出现相同的异常.这里有人会告诉我我在哪里犯错,或者还有什么可能是实现此目的的更好的方法.下面是我的代码

I am trying to select recyclerview item randomly with delay.I need to start random selection method after fragment load without any user interaction,But getting the following error. Afterward I put it on ImageView click to check but again I am getting same exception.Will anybody here tell me where I am making a mistake,or what else could be better way to achieve this.Below is my code

    package com.apponative.bjja.fragments;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.apponative.bjja.R;
import com.apponative.bjja.adapters.AttackGridAdapter;

import java.util.Random;

public class Fragment_AttackGrid extends Fragment {

    View v;
    RecyclerView grid_attack;
    AttackGridAdapter attackGridAdapter;
    Bundle b;
    int itemCount;
    Handler randomHandler;
    Runnable randomRunnable;
    Random rand;
    int randomNum = 0;
    long emptyTime;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        v = inflater.inflate(R.layout.fragment_attack_grid, container, false);

        setUpViews();

        return v;
    }

    void setUpViews() {

        b = getArguments();
        itemCount = b.getInt(getString(R.string.item_count));
        rand = new Random();
        randomHandler = new Handler();

        grid_attack = (RecyclerView) v.findViewById(R.id.attack_grid);
        RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getActivity(), 4);

        grid_attack.setLayoutManager(layoutManager);
        attackGridAdapter = new AttackGridAdapter(getActivity(), itemCount);
        grid_attack.setAdapter(attackGridAdapter);

        v.findViewById(R.id.belt_black).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                selectRandomly();
            }
        });
    }

    void selectRandomly() {

        for (int i = 0; i < itemCount; i++) {
            emptyTime = emptyTime+1000;
            randomRunnable = new Runnable() {
                @Override
                public void run() {
                    grid_attack.findViewHolderForAdapterPosition(randomNum).itemView.setSelected(false);
                    randomNum = rand.nextInt((itemCount - 0) + 1) + 0;
                    grid_attack.findViewHolderForAdapterPosition(randomNum).itemView.setSelected(true);
                }
            };
            randomHandler.postDelayed(randomRunnable, emptyTime);

        }
        emptyTime = emptyTime+2000;
        randomRunnable = new Runnable() {
            @Override
            public void run() {
                grid_attack.findViewHolderForAdapterPosition(randomNum).itemView.performClick();
            }
        };
        randomHandler.postDelayed(randomRunnable, emptyTime);
    }

    @Override
    public void onResume() {
        super.onResume();
        //   selectRandomly();
    }
}

一开始我放

selectRandomly()

selectRandomly()

onResume()

onResume()

方法,我得到一个例外:

method and I was getting an exception:

01-27 18:28:44.969 19041-19041/com.apponative.bjja E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.apponative.bjja, PID: 19041
java.lang.NullPointerException: Attempt to read from field 'android.view.View android.support.v7.widget.RecyclerView$ViewHolder.itemView' on a null object reference                                                                                      at com.apponative.bjja.fragments.Fragment_AttackGrid$2.run(Fragment_AttackGrid.java:77)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:155)
    at android.app.ActivityThread.main(ActivityThread.java:5696)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)

根据片段生命周期,网址为片段生命周期 onResume()和onStart()创建视图后将调用方法.我也尝试将方法放在那里,但没有成功.

According to fragment lifecycle at Fragment LifeCycle onResume() and onStart() methods are called after view has been created. I tried putting my method there too, But no success.

推荐答案

如果您的onCreateViewHolder方法返回null,则可能会发生这种情况

This may happen if your onCreateViewHolder method returns null

这篇关于尝试从字段'android.view.View android.support.v7.widget.RecyclerView $ ViewHolder.itemView'中读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆