简体   繁体   English

适用于recyclerview的适配器中的空指针异常

[英]Null Pointer Exception in Adapter for recyclerview

I've been trying to implement a recyclerview for my activity but I keep getting NullPointerException. 我一直在尝试为我的活动实现recyclerview,但我一直在获取NullPointerException。 This is my first time making a recyclerview so if I'm doing anything wrong just tell me. 这是我第一次进行回收查看,所以如果我做错了就告诉我。 I'll show you all my code then tell where where the error is at 我将向您展示我的所有代码然后告诉错误在哪里

my main xml layout (called activity_main.xml) 我的主要xml布局(称为activity_main.xml)

<android.support.v4.widget.DrawerLayout
                xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:card_view="http://schemas.android.com/apk/res-auto"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/drawer"
                android:fitsSystemWindows="true">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <include
            android:id="@+id/tool_bar"
            layout="@layout/tool_bar">
        </include>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/main_recycler_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingRight="8dp"
            android:paddingLeft="8dp"
            android:paddingTop="8dp"/>

    </LinearLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/drawer_header"
        app:menu="@menu/drawer"/>

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

My row xml for the recyclerview (called main_recycler_view_row.xml) 我对recyclelerview的行xml(名为main_recycler_view_row.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <android.support.v7.widget.CardView
        android:id="@+id/main_card"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:id="@+id/period_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="24sp"
                android:textColor="@android:color/black"
                android:paddingTop="24dp"
                android:paddingLeft="16dp"
                android:paddingBottom="4dp"/>

            <TextView
                android:id="@+id/period_subtitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="14sp"
                android:paddingLeft="16dp"
                android:paddingBottom="16dp"/>

        </LinearLayout>

    </android.support.v7.widget.CardView>

</LinearLayout>

My main activity 我的主要活动

package maxbleggi.afstudentplanner;

import android.content.Intent;
import android.content.res.Configuration;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.parse.GetCallback;
import com.parse.Parse;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseUser;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity
{
    // "MACROS" for user's class periods
    private final int PERIOD1 = 0;
    private final int PERIOD2 = 1;
    private final int PERIOD3 = 2;
    private final int PERIOD4 = 3;
    private final int PERIOD5 = 4;
    private final int PERIOD6 = 5;
    private final int PERIOD7 = 6;

    // toolbar
    private Toolbar toolbar;

    // nav drawer
    private DrawerLayout navDrawer;

    // user's data
    private String[] userClasses = new String[10];

    // list to fill Classroom class with
    private List<ClassroomClass> classrooms;

    /*
    * on creation of activity
     */
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // initialise and set toolbar as actionbar
        toolbar = (Toolbar) findViewById(R.id.tool_bar);
        setSupportActionBar(toolbar);

        // initialize drawer layout
        navDrawer = (DrawerLayout) findViewById(R.id.drawer);
        NavigationView navView = (NavigationView) findViewById(R.id.navigation_view);

        // initialize recycler view elements
        RecyclerView mainRecyclerView = (RecyclerView)findViewById(R.id.main_recycler_view);

        // initialize layout manager for recycler view
        RecyclerView.LayoutManager mainLayoutManager = new LinearLayoutManager(this);

        // adapter for recycler view
        RecyclerView.Adapter mainAdapter= new MainRecyclerAdapter(classrooms);;

        // initialize nav bars
        initNavBars();

        // initialize nav drawer
        initNavDrawer();

        // initialize data for all classes
        initClassData();

        // add layout manager to recycler view
        mainRecyclerView.setLayoutManager(mainLayoutManager);

        // add adapter to recycler view
        mainRecyclerView.setAdapter(mainAdapter);

    }

    /*
    * initializes the class data for each class
    */
    public void initClassData()
    {
        // check if a user is not cached
        ParseUser currentUser = ParseUser.getCurrentUser();
        if (currentUser == null)
        {
            // prompt user to Register screen
            Intent intent = new Intent(MainActivity.this, RegisterActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
            startActivity(intent);
        }

        // query database for user's classes
        final ParseQuery<ParseObject> query = ParseQuery.getQuery("StudentClasses");
        query.whereEqualTo("student_id", ParseUser.getCurrentUser());

        query.getFirstInBackground(new GetCallback<ParseObject>()
        {
            @Override
            public void done(ParseObject parseObject, ParseException e)
            {
                if (parseObject == null)
                {
                    // retrieved the object
                    userClasses[PERIOD1]= parseObject.getString("first_period");
                    userClasses[PERIOD2]= parseObject.getString("second_period");
                    userClasses[PERIOD3]= parseObject.getString("third_period");
                    userClasses[PERIOD4]= parseObject.getString("fourth_period");
                    userClasses[PERIOD5]= parseObject.getString("fifth_period");
                    userClasses[PERIOD6]= parseObject.getString("sixth_period");
                    userClasses[PERIOD7]= parseObject.getString("seventh_period");
                }
                else
                {
                    // failed lookup. Do something here
                }
            }
        });

        // fill all fields for class periods
        classrooms = new ArrayList<>();

        // titles of classes to add to list
        String[] classTitles = {"First Period", "Second Period", "Third Period", "Fourth Period",
                "Fifth Period", "Sixth Period", "Seventh Period"};

        // iterate over every period to add it to the list
        for (int i = 0; i <= PERIOD7; i++)
        {
            // if period registered was "Nothing" don't add it to list
            //if (!userClasses[i].equals("Nothing"))
            //{
                classrooms.add(new ClassroomClass(userClasses[i], classTitles[i], i + 1));
            //}
        }
    }

    public void initNavDrawer()
    {
        navDrawer = (DrawerLayout) findViewById(R.id.drawer);

        final NavigationView navView = (NavigationView) findViewById(R.id.navigation_view);

        navView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener()
        {
            @Override
            public boolean onNavigationItemSelected(MenuItem menuItem)
            {
                menuItem.setChecked(true);
                navDrawer.closeDrawers();
                return true;
            }
        });
    }

    private void initNavBars()
    {
       if (toolbar != null)
       {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            toolbar.setNavigationIcon(R.drawable.ic_menu_white);
            toolbar.setNavigationOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    navDrawer.openDrawer(GravityCompat.START);
                }
            });
       }
    }
}

my class for my data fields 我的数据字段类

package maxbleggi.afstudentplanner;

/*
 * Define the characteristics that all classroom cards have in common
*/
public class ClassroomClass
{

    String className;
    String classPeriod;
    int classNumber;

    public ClassroomClass(String className, String classPeriod, int classNumber)
    {
        this.className = className;
        this.classPeriod = classPeriod;
        this.classNumber = classNumber;
    }

    public String getClassName()
    {
        return className;
    }

    public String getClassPeriod()
    {
        return classPeriod;
    }

    public int getClassNumber()
    {
        return classNumber;
    }
}

Finally my adapter 最后我的适配器

package maxbleggi.afstudentplanner;

import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

public class MainRecyclerAdapter extends RecyclerView.Adapter<MainRecyclerAdapter.ViewHolder>
{
    private List<ClassroomClass> classrooms;

    public static class ViewHolder extends RecyclerView.ViewHolder
    {
        // hold data items
        public View view;

        ViewHolder(View itemView)
        {
            super(itemView);
            view = itemView;
        }
    }

    public MainRecyclerAdapter(List<ClassroomClass> classrooms)
    {
        this.classrooms = classrooms;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i)
    {
        // create a new view
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.main_recycler_view_row, viewGroup, false);

        // set the view's parameters
        ViewHolder pvh = new ViewHolder(v);
        return pvh;
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int position)
    {
        // get elements from view
        TextView title = (TextView) viewHolder.view.findViewById(R.id.period_title);
        TextView subtitle = (TextView) viewHolder.view.findViewById(R.id.period_subtitle);

        // retrieve data from data field and put in view
        title.setText(classrooms.get(position).getClassName());
        subtitle.setText(classrooms.get(position).classPeriod);

    }

    @Override
    public int getItemCount()
    {
        return classrooms.size();
    }


    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView)
    {
        super.onAttachedToRecyclerView(recyclerView);
    }
}

I get a java.lang.NullPointerException in my MainRecyclerAdapter at the get item count here 我在MainRecyclerAdapter中获取了一个java.lang.NullPointerException,其中get项为count

    @Override
    public int getItemCount()
    {
        return classrooms.size();
    }

Any help or comments is appreciated thanks and sorry for all the code. 任何帮助或评论表示感谢,感谢所有代码。 I just wanted to make sure I was thorough. 我只是想确保我彻底。

You have to init classrooms data before making constructor, so just invoke 你必须在构造函数之前初始化教室数据,所以只需调用

initClassData();

before adapter constructor. 适配器构造函数之前。

Btw you are supposed to do this code in viewholder constructor 顺便说一句,你应该在viewholder构造函数中执行此代码

// get elements from view
        TextView title = (TextView) viewHolder.view.findViewById(R.id.period_title);
        TextView subtitle = (TextView) viewHolder.view.findViewById(R.id.period_subtitle);

and later use it like 后来就像使用它一样

holder.title.setText holder.title.setText

holder.subtitle.setText holder.subtitle.setText

You call 你打电话

initClassData();

after the call of the constructor: 在构造函数调用之后:

RecyclerView.Adapter mainAdapter= new MainRecyclerAdapter(classrooms);;

this is the reason of the NullPoimter. 这就是Nul​​lPoimter的原因。 the object classrooms is still empty so when you invoke the size() on it you get nullPoint. 对象教室仍然是空的,所以当你调用它上面的size()时你得到nullPoint。

Just replace the order of those to lines. 只需将那些顺序替换为行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM