首页 > 解决方案 > 我在一个布局中创建了两个回收器视图,并且我有 nullPointerException

问题描述

当我尝试在 android studio 中实现两个回收器视图时。我得到了类似的错误NullPointerExceptionclass我为这两个视图创建了两个适配器和助手,并在 java 中调用了它们的函数。

import android.content.Intent;
import android.os.Bundle;
import android.view.WindowManager;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.college.HelperClasses.CollegeAdapter.CollegeAdapter;
import com.example.college.HelperClasses.CollegeAdapter.CollegeHelperClass;
import com.example.college.HelperClasses.HomeAdapter.DeptAdapter;
import com.example.college.HelperClasses.HomeAdapter.DeptHelperClass;
import com.example.college.R;

import java.util.ArrayList;

public class UserDashboard extends AppCompatActivity implements DeptAdapter.onDeptListener {

    RecyclerView deptRecycler,collegeRecycler;
    RecyclerView.Adapter adapter,adapter_clg;
    ArrayList<DeptHelperClass> deptLocations = new ArrayList<>();
    ArrayList<CollegeHelperClass> collegeLocations = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_user_dashboard);

        deptRecycler =(RecyclerView) findViewById(R.id.dept_recycler);
        collegeRecycler =(RecyclerView) findViewById(R.id.clg_recycler);
        deptRecycler();
        collegeRecycler();



    }

    private void collegeRecycler() {
        collegeRecycler.setHasFixedSize(true);
        collegeRecycler.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false));


        collegeLocations.add(new CollegeHelperClass(R.drawable.anti_ragging,"AntiRagging","Anti Ragging Committee helps to stop ragging activities in our College"));
        collegeLocations.add(new CollegeHelperClass(R.drawable.anti_ragging,"AntiRagging","Anti Ragging Committee helps to stop ragging activities in our College"));
        collegeLocations.add(new CollegeHelperClass(R.drawable.anti_ragging,"AntiRagging","Anti Ragging Committee helps to stop ragging activities in our College"));

        adapter_clg  = new CollegeAdapter(collegeLocations);
        collegeRecycler.setAdapter(adapter_clg);

    }

    private void deptRecycler() {
        deptRecycler.setHasFixedSize(true);
        deptRecycler.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false));

        deptLocations.add(new DeptHelperClass(R.drawable.cse,"Computer Science & Engineering","Dazzling CSE"));
        deptLocations.add(new DeptHelperClass(R.drawable.auto,"Automobile Engineering","Amazing Auto"));
        deptLocations.add(new DeptHelperClass(R.drawable.mechanic,"Mechanical Engineering","Royal Mech"));
        deptLocations.add(new DeptHelperClass(R.drawable.biotech,"Bio-Technology","Beautiful Bio"));
        deptLocations.add(new DeptHelperClass(R.drawable.civil,"Civil Engineering","Clever Civil"));
        deptLocations.add(new DeptHelperClass(R.drawable.electrical,"Electrical and Electronics Engineering","#Psych EEE"));
        deptLocations.add(new DeptHelperClass(R.drawable.chemistry,"Chemistry","Toxic chemistry"));
        deptLocations.add(new DeptHelperClass(R.drawable.computer_app,"Computer Applications","Playful Applications"));
        deptLocations.add(new DeptHelperClass(R.drawable.ece,"ECE","Dominating ECE"));
        deptLocations.add(new DeptHelperClass(R.drawable.english,"English","Educative English"));
        deptLocations.add(new DeptHelperClass(R.drawable.it_dept,"Information Technology","Informative IT"));
        deptLocations.add(new DeptHelperClass(R.drawable.management,"Management Studies","Magical Management"));
        deptLocations.add(new DeptHelperClass(R.drawable.maths,"Mathematics","Scary Mathematics"));
        deptLocations.add(new DeptHelperClass(R.drawable.petro,"Petrochemical Technology","Purposeful Petro"));
        deptLocations.add(new DeptHelperClass(R.drawable.pharma,"Pharma","Fundamental Pharma"));
        deptLocations.add(new DeptHelperClass(R.drawable.physics,"Physics","Mesmerising Physics"));
        deptLocations.add(new DeptHelperClass(R.drawable.physical_edu,"Physical Education","Sweaty PE"));

        adapter = new DeptAdapter(deptLocations, this);
        deptRecycler.setAdapter(adapter);
    }

    @Override
    public void onDeptClick(int position) {
                deptLocations.get(position);
                if(position==0){
                    Intent intent = new Intent(this,comp_science.class);
                    intent.putExtra("CSE","Called");
                    startActivity(intent);
                }
                else if(position==1){
                    Intent intent = new Intent(this,auto_engg.class);
                    intent.putExtra("Automobile","Called");
                    startActivity(intent);
                }
    }
}

我的Logcat

java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setHasFixedSize(boolean)' on a null object reference
        at com.example.college.User.UserDashboard.collegeRecycler(UserDashboard.java:42) 02-28 13:37:50.367 25630-25630/com.example.college E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.college, PID: 25630
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.college/com.example.college.User.UserDashboard}: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setHasFixedSize(boolean)' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2684)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2750)
    at android.app.ActivityThread.access$1100(ActivityThread.java:186)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1606)
    at android.os.Handler.dispatchMessage(Handler.java:111)
    at android.os.Looper.loop(Looper.java:238)
    at android.app.ActivityThread.main(ActivityThread.java:6006)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:937)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:798)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setHasFixedSize(boolean)' on a null object reference
    at com.example.college.User.UserDashboard.collegeRecycler(UserDashboard.java:42)
    at com.example.college.User.UserDashboard.onCreate(UserDashboard.java:35)
    at android.app.Activity.performCreate(Activity.java:6466)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1113)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2637)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2750) 
    at android.app.ActivityThread.access$1100(ActivityThread.java:186) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1606) 
    at android.os.Handler.dispatchMessage(Handler.java:111) 
    at android.os.Looper.loop(Looper.java:238) 
    at android.app.ActivityThread.main(ActivityThread.java:6006) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:937) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:798) ```

我的布局文件是

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="10dp"
    android:background="@color/white"
    android:orientation="vertical"
    tools:context=".User.UserDashboard">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">


        <ImageView
            android:id="@+id/textView"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_centerVertical="true"
            android:contentDescription="@string/menu_bar"
            android:src="@drawable/menu_icon_userdash_black" />

    </RelativeLayout>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="none"
        >

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


            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <TextView
                    android:id="@+id/userdash_title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:fontFamily="@font/josefin_regular"
                    android:text="@string/logo_name"
                    android:textSize="24sp"
                    android:textStyle="bold"

                    />

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="40dp"
                    android:layout_below="@id/userdash_title"
                    android:layout_marginLeft="40dp"
                    android:layout_marginTop="20dp"
                    android:layout_marginRight="40dp"
                    android:layout_marginBottom="20dp"
                    android:background="@color/white"
                    android:elevation="8dp">

                    <TextView
                        android:id="@+id/search_string"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerVertical="true"
                        android:layout_marginStart="15dp"
                        android:text="@string/search_string" />

                    <ImageView
                        android:id="@+id/search_bar"
                        android:layout_width="30dp"
                        android:layout_height="30dp"
                        android:layout_alignParentEnd="true"
                        android:layout_centerVertical="true"
                        android:layout_marginRight="15dp"
                        android:contentDescription="@string/search_bar_userdash"
                        android:src="@drawable/search_userdash_black" />


                </RelativeLayout>


            </RelativeLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:orientation="horizontal">

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="5dp"
                    android:orientation="vertical">

                    <RelativeLayout
                        android:layout_width="60dp"
                        android:layout_height="60dp"
                        android:background="@drawable/card_one_userdash"
                        android:elevation="8dp">

                        <ImageView
                            android:layout_width="30dp"
                            android:layout_height="30dp"
                            android:layout_centerInParent="true"
                            android:src="@drawable/cse" />
                    </RelativeLayout>

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:fontFamily="@font/josefin_regular"
                        android:gravity="center_horizontal"
                        android:text="@string/CSE"
                        android:textAlignment="center"
                        android:textSize="11sp"
                        android:textStyle="bold" />


                </LinearLayout>

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="5dp"
                    android:orientation="vertical">

                    <RelativeLayout
                        android:layout_width="60dp"
                        android:layout_height="60dp"
                        android:background="@drawable/card_two_userdash"
                        android:elevation="8dp">

                        <ImageView
                            android:layout_width="30dp"
                            android:layout_height="30dp"
                            android:layout_centerInParent="true"
                            android:src="@drawable/auto" />
                    </RelativeLayout>

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:fontFamily="@font/josefin_regular"
                        android:gravity="center_horizontal"
                        android:text="@string/automob"
                        android:textAlignment="center"
                        android:textSize="11sp"
                        android:textStyle="bold" />


                </LinearLayout>

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="5dp"
                    android:orientation="vertical">

                    <RelativeLayout
                        android:layout_width="60dp"
                        android:layout_height="60dp"
                        android:background="@drawable/card_three_userdash"
                        android:elevation="8dp">

                        <ImageView
                            android:layout_width="30dp"
                            android:layout_height="30dp"
                            android:layout_centerInParent="true"
                            android:src="@drawable/mechanic" />
                    </RelativeLayout>

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:fontFamily="@font/josefin_regular"
                        android:gravity="center_horizontal"
                        android:text="@string/mechanical"
                        android:textAlignment="center"
                        android:textSize="11sp"
                        android:textStyle="bold" />


                </LinearLayout>

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="5dp"
                    android:orientation="vertical">

                    <RelativeLayout
                        android:layout_width="60dp"
                        android:layout_height="60dp"
                        android:background="@drawable/card_four_userdash"
                        android:elevation="8dp">

                        <ImageView
                            android:layout_width="30dp"
                            android:layout_height="30dp"
                            android:layout_centerInParent="true"
                            android:src="@drawable/electrical" />
                    </RelativeLayout>

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:fontFamily="@font/josefin_regular"
                        android:gravity="center_horizontal"
                        android:text="@string/EEE"
                        android:textAlignment="center"
                        android:textSize="11sp"
                        android:textStyle="bold" />


                </LinearLayout>


            </LinearLayout>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="20dp"
                android:background="@color/white">

                <LinearLayout
                    android:id="@+id/featured_background"
                    android:layout_width="120dp"
                    android:layout_height="280dp"
                    android:layout_margin="10dp"
                    android:background="@color/ColorPrimaryDark"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="10dp"
                        android:layout_marginTop="50dp"
                        android:fontFamily="@font/josefin_regular"
                        android:text="@string/featured_locations"
                        android:textAllCaps="true"
                        android:textColor="@color/white"
                        android:textSize="14sp"
                        android:textStyle="bold" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_margin="10dp"
                        android:fontFamily="@font/josefin_regular"
                        android:text="Featured List of All Departments"
                        android:textColor="@color/colorPrimary"
                        android:textSize="12sp" />


                </LinearLayout>

                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/dept_recycler"
                    android:layout_width="match_parent"
                    android:layout_height="280dp"
                    android:layout_marginTop="10dp"
                    android:layout_toEndOf="@id/featured_background"
                    android:background="@color/card_two_userdash" />


            </RelativeLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="30dp"
                android:orientation="vertical"
                android:padding="20dp">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:fontFamily="@font/josefin_regular"
                        android:text="@string/most_viewed_locations"
                        android:textAllCaps="true"
                        android:textColor="@color/black"
                        android:textSize="20sp" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentRight="true"
                        android:layout_centerVertical="true"
                        android:text="@string/view_all" />


                </RelativeLayout>

                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/clg_recycler"
                    android:layout_width="match_parent"
                    android:layout_height="100dp" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="30dp"
                android:orientation="vertical"
                android:padding="20dp">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:fontFamily="@font/josefin_regular"
                        android:text="@string/categories"
                        android:textAllCaps="true"
                        android:textColor="@color/black"
                        android:textSize="20sp" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentRight="true"
                        android:layout_centerVertical="true"
                        android:text="@string/view_all" />


                </RelativeLayout>

                <androidx.recyclerview.widget.RecyclerView
                    android:layout_width="match_parent"
                    android:layout_height="100dp" />
            </LinearLayout>

        </LinearLayout>


    </ScrollView>


</LinearLayout>

标签: javaandroidandroid-recyclerview

解决方案


        private void collegeRecycler() {
       if(collegeRecycler!=null)//add this line 
    {
    collegeRecycler.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false));
    collegeRecycler.setHasFixedSize(true);

    collegeLocations.add(new CollegeHelperClass(R.drawable.anti_ragging,"AntiRagging","Anti Ragging Committee helps to stop ragging activities in our College"));
    collegeLocations.add(new CollegeHelperClass(R.drawable.anti_ragging,"AntiRagging","Anti Ragging Committee helps to stop ragging activities in our College"));
    collegeLocations.add(new CollegeHelperClass(R.drawable.anti_ragging,"AntiRagging","Anti Ragging Committee helps to stop ragging activities in our College"));

    adapter_clg  = new CollegeAdapter(collegeLocations);
    collegeRecycler.setAdapter(adapter_clg);
 }
}

private void deptRecycler() {
     if(deptRecycler!=null)//add this line
     {
    deptRecycler.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false));
         deptRecycler.setHasFixedSize(true);
    deptLocations.add(new DeptHelperClass(R.drawable.cse,"Computer Science & Engineering","Dazzling CSE"));
    deptLocations.add(new DeptHelperClass(R.drawable.auto,"Automobile Engineering","Amazing Auto"));
    deptLocations.add(new DeptHelperClass(R.drawable.mechanic,"Mechanical Engineering","Royal Mech"));
    deptLocations.add(new DeptHelperClass(R.drawable.biotech,"Bio-Technology","Beautiful Bio"));
    deptLocations.add(new DeptHelperClass(R.drawable.civil,"Civil Engineering","Clever Civil"));
    deptLocations.add(new DeptHelperClass(R.drawable.electrical,"Electrical and Electronics Engineering","#Psych EEE"));
    deptLocations.add(new DeptHelperClass(R.drawable.chemistry,"Chemistry","Toxic chemistry"));
    deptLocations.add(new DeptHelperClass(R.drawable.computer_app,"Computer Applications","Playful Applications"));
    deptLocations.add(new DeptHelperClass(R.drawable.ece,"ECE","Dominating ECE"));
    deptLocations.add(new DeptHelperClass(R.drawable.english,"English","Educative English"));
    deptLocations.add(new DeptHelperClass(R.drawable.it_dept,"Information Technology","Informative IT"));
    deptLocations.add(new DeptHelperClass(R.drawable.management,"Management Studies","Magical Management"));
    deptLocations.add(new DeptHelperClass(R.drawable.maths,"Mathematics","Scary Mathematics"));
    deptLocations.add(new DeptHelperClass(R.drawable.petro,"Petrochemical Technology","Purposeful Petro"));
    deptLocations.add(new DeptHelperClass(R.drawable.pharma,"Pharma","Fundamental Pharma"));
    deptLocations.add(new DeptHelperClass(R.drawable.physics,"Physics","Mesmerising Physics"));
    deptLocations.add(new DeptHelperClass(R.drawable.physical_edu,"Physical Education","Sweaty PE"));

    adapter = new DeptAdapter(deptLocations, this);
    deptRecycler.setAdapter(adapter);
}

推荐阅读