有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java我在一个布局中创建了两个回收器视图,我有nullPointerException

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

import 安卓.content.Intent;
import 安卓.os.Bundle;
import 安卓.view.WindowManager;

import 安卓x.appcompat.app.AppCompatActivity;
import 安卓x.recyclerview.widget.LinearLayoutManager;
import 安卓x.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 安卓x.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 安卓x.recyclerview.widget.RecyclerView.setHasFixedSize(boolean)' on a null object reference
    at 安卓.app.ActivityThread.performLaunchActivity(ActivityThread.java:2684)
    at 安卓.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2750)
    at 安卓.app.ActivityThread.access$1100(ActivityThread.java:186)
    at 安卓.app.ActivityThread$H.handleMessage(ActivityThread.java:1606)
    at 安卓.os.Handler.dispatchMessage(Handler.java:111)
    at 安卓.os.Looper.loop(Looper.java:238)
    at 安卓.app.ActivityThread.main(ActivityThread.java:6006)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:937)
    at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:798)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void 安卓x.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 安卓.app.Activity.performCreate(Activity.java:6466)
    at 安卓.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1113)
    at 安卓.app.ActivityThread.performLaunchActivity(ActivityThread.java:2637)
    at 安卓.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2750) 
    at 安卓.app.ActivityThread.access$1100(ActivityThread.java:186) 
    at 安卓.app.ActivityThread$H.handleMessage(ActivityThread.java:1606) 
    at 安卓.os.Handler.dispatchMessage(Handler.java:111) 
    at 安卓.os.Looper.loop(Looper.java:238) 
    at 安卓.app.ActivityThread.main(ActivityThread.java:6006) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:937) 
    at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:798) ```

我的布局文件是

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

    <RelativeLayout
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content">


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

    </RelativeLayout>

    <ScrollView
        安卓:layout_width="match_parent"
        安卓:layout_height="match_parent"
        安卓:scrollbars="none"
        >

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


            <RelativeLayout
                安卓:layout_width="match_parent"
                安卓:layout_height="wrap_content">

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

                    />

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

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

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


                </RelativeLayout>


            </RelativeLayout>

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

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

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

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

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


                </LinearLayout>

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

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

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

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


                </LinearLayout>

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

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

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

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


                </LinearLayout>

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

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

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

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


                </LinearLayout>


            </LinearLayout>

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

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

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

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


                </LinearLayout>

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


            </RelativeLayout>

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

                <RelativeLayout
                    安卓:layout_width="match_parent"
                    安卓:layout_height="wrap_content">

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

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


                </RelativeLayout>

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

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

                <RelativeLayout
                    安卓:layout_width="match_parent"
                    安卓:layout_height="wrap_content">

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

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


                </RelativeLayout>

                <安卓x.recyclerview.widget.RecyclerView
                    安卓:layout_width="match_parent"
                    安卓:layout_height="100dp" />
            </LinearLayout>

        </LinearLayout>


    </ScrollView>


</LinearLayout>

共 (1) 个答案

  1. # 1 楼答案

            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);
    }