簡體   English   中英

android.support.v7.widget.RecyclerView null嗎? 使用LinearLayoutManager

[英]android.support.v7.widget.RecyclerView null ? with LinearLayoutManager

我試圖獲取一個視圖回收對象來添加,但是我有一些不明白的問題。

搜索了互聯網,大多數人說這是由於未設置“ LinearLayoutManager”

我不知道這個錯誤:

錯誤:

07-23 06:35:00.321  24842-24842/xxx.xxx.xxx.xxxE/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:1694)

代碼片段2擴展了片段{}

   @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    init(inflater.inflate(R.layout.fragment_fragment2, container, false));
    return inflater.inflate(R.layout.fragment_fragment2, container, false);
}

public void init(View v){
        ArrayList<Pusheen> pusheens = new ArrayList<>();

        Pusheen pusheen = new Pusheen();
        pusheen.setId(1);
        pusheen.setName("Pusheen");
        pusheen.setPasTime("Blogger");
        pusheens.add(pusheen);

        Pusheen pusheen2 = new Pusheen();
        pusheen2.setId(2);
        pusheen2.setName("Pusheen");
        pusheen2.setPasTime("Sculpor");
        pusheens.add(pusheen2);

        Pusheen pusheen3 = new Pusheen();
        pusheen3.setId(3);
        pusheen3.setName("Stormy");
        pusheen3.setPasTime("Mage");
        pusheens.add(pusheen3);

        Pusheen pusheen4 = new Pusheen();
        pusheen4.setId(4);
        pusheen4.setName("Pusheen");
        pusheen4.setPasTime("Tribute");
        pusheens.add(pusheen4);

        Pusheen pusheen5 = new Pusheen();
        pusheen5.setId(5);
        pusheen5.setName("Stormy");
        pusheen5.setPasTime("adventurer");
        pusheens.add(pusheen5);
        //Fin de nuestro dataset

        //Configuración del recyclerView, con nuestro dataSet y nuestro Adapter

        //Aqui buscamos el Widget recyclerView
        RecyclerView recyclerView = (RecyclerView) v.findViewById(R.id.my_recycler_view);

        //A pesar del tamaño de la pantalla, asegurar que los elementos se vean siempre igual.
        recyclerView.setHasFixedSize(true);

        //Configuramos la animación por defecto
        recyclerView.setItemAnimator(new DefaultItemAnimator());

        //Ahora le pasamos nuestro dataset y la forma en que se vera cada fila.
        recyclerView.setAdapter(new PusheenAdapter(pusheens, R.layout.row_notifications));

        //Importante, ahora podemos indicar si mostrar los elementos como fila o como grilla, en este
        //caso setLayoutManager() esta  configurado como grilla de una columna, es por es eso que se ve
        //como fila, recordar que el mismo resultado se obtiene con
        // recyclerView.setLayoutManager(new LinearLayoutManager(this));
        final LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        layoutManager.scrollToPosition(0);
        recyclerView.setLayoutManager(layoutManager);
    }

調用recyclerView.setLayoutManager(layoutManager); 在setAdapter之前

暫無
暫無

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

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