android studio java中布局管理器的空指针验证

android studio java中布局管理器的空指针验证,java,android,android-studio,adapter,Java,Android,Android Studio,Adapter,因此,在管理应用程序中,我想添加从firebase数据库和存储中删除图像(图库)的功能 所以我为它做了一个布局并设置了功能,但是对于布局管理器,我得到了运行时错误 这是日志 java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setLayoutManager(androidx.recyclerview.widget.Rec

因此,在管理应用程序中,我想添加从firebase数据库和存储中删除图像(图库)的功能 所以我为它做了一个布局并设置了功能,但是对于布局管理器,我得到了运行时错误

这是日志

 java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setLayoutManager(androidx.recyclerview.widget.RecyclerView$LayoutManager)' on a null object reference
     at com.jkstudio.collageadmin.gallery.Gallery_list$3.onDataChange(Gallery_list.java:129)
     at com.google.firebase.database.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:75)
     at com.google.firebase.database.core.view.DataEvent.fire(DataEvent.java:63)
     at com.google.firebase.database.core.view.EventRaiser$1.run(EventRaiser.java:55)
     at android.os.Handler.handleCallback(Handler.java:883)
     at android.os.Handler.dispatchMessage(Handler.java:100)
     at android.os.Looper.loop(Looper.java:224)
     at android.app.ActivityThread.main(ActivityThread.java:7590)
     at java.lang.reflect.Method.invoke(Native Method)
     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
多媒体资料适配器 包com.jkstudio.collageadmin.gallery

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.jkstudio.collageadmin.FullImageView;
import com.jkstudio.collageadmin.R;


import java.util.List;


public class GalleryAdapter extends RecyclerView.Adapter<GalleryAdapter.galleryViewAdapter>
{


    private Context context;
    private List<GalleryData> list;
    private DatabaseReference Drefrance,DRef;
    private StorageReference Srafrance,SRef;

    public GalleryAdapter(Context context, List<GalleryData> list)
    {

        this.context = context;
        this.list = list;
    }

    @Override
    public galleryViewAdapter onCreateViewHolder(@NonNull  ViewGroup parent, int viewType)
    {
        View view = LayoutInflater.from(context).inflate(R.layout.gallrey_imageitem_layout,parent,false);
        return new galleryViewAdapter(view);
    }

    @Override
    public void onBindViewHolder(@NonNull  galleryViewAdapter holder, int position)
    {

        try {
            Glide.with(context).load(list.get(position)).into(holder.gallery_imageView);
            holder.gallery_imageView.setOnClickListener(v ->
            {
                Intent intent = new Intent(context, FullImageView.class);
                intent.putExtra("imageurl_pass",list.get(position).getDurl());
                context.startActivity(intent);
            });


        } catch (Exception e) {
            e.printStackTrace();

        }
        holder.deletebtn.setOnClickListener(v ->
        {
            AlertDialog.Builder dialog = new AlertDialog.Builder(context);
            dialog.setMessage("delete this Image ?");
            dialog.setCancelable(true);
            dialog.setPositiveButton("confirm", (dialog1, which) ->
            {
                Srafrance = FirebaseStorage.getInstance().getReference().child("Gallery");
                SRef=Srafrance.child(list.get(position).getSfilename());
                SRef.delete().addOnSuccessListener(unused ->
                {
                    Toast.makeText(context, "image delete-cloud", Toast.LENGTH_SHORT).show();
                }).addOnFailureListener(e ->
                {
                    Toast.makeText(context, "image delete-cloud failed", Toast.LENGTH_SHORT).show();

                })  ;



                Drefrance = FirebaseDatabase.getInstance().getReference().child("Gallery");
                DRef =  Drefrance.child(list.get(position).getCategory());
               DRef.removeValue()
                        .addOnCompleteListener(task ->
                        {
                            Toast.makeText(context, "Deleted", Toast.LENGTH_SHORT).show();
                        }).addOnFailureListener(e ->
                {
                    Toast.makeText(context, "Seometing went wrong -Srefrance-DN", Toast.LENGTH_SHORT).show();
                });
                try {
                    notifyItemRemoved(position);
                } catch (Exception e) {
                    e.printStackTrace();
                }



            });

            dialog.setNegativeButton("Cancle", (Idialog, which) ->
            {
                Idialog.cancel();

            });

            AlertDialog Adialog = null;
            try {
                Adialog = dialog.create();
            } catch (Exception e) {
                e.printStackTrace();
            }
            if(dialog != null)
            {
                dialog.show();
            }
        });

    }

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

    public class galleryViewAdapter extends RecyclerView.ViewHolder
    {
        ImageView gallery_imageView;
        Button deletebtn;
        public galleryViewAdapter(@NonNull  View itemView)
        {
            super(itemView);
            gallery_imageView = itemView.findViewById(R.id.gallery_imageView);
            deletebtn = itemView.findViewById(R.id.gallery_delete_btn);

        }
    }

}
导入android.app.AlertDialog;
导入android.content.Context;
导入android.content.Intent;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.Button;
导入android.widget.ImageView;
导入android.widget.Toast;
导入androidx.annotation.NonNull;
导入androidx.recyclerview.widget.recyclerview;
导入com.bumptech.glide.glide;
导入com.google.firebase.database.DatabaseReference;
导入com.google.firebase.database.FirebaseDatabase;
导入com.google.firebase.storage.firebase存储;
导入com.google.firebase.storage.StorageReference;
导入com.jkstudio.collageadmin.FullImageView;
导入com.jkstudio.collageadmin.R;
导入java.util.List;
公共类GalleryAdapter扩展了RecyclerView.Adapter
{
私人语境;
私人名单;
私人数据库参考Drefrance,DRef;
私人存储参考Srafrance,SRef;
公共GalleryAdapter(上下文、列表)
{
this.context=上下文;
this.list=列表;
}
@凌驾
public galleryViewAdapter onCreateViewHolder(@NonNull ViewGroup父级,int-viewType)
{
视图=布局更平坦。从(上下文)。充气(R.layout.gallrey_imageitem_布局,父项,false);
返回新的galleryViewAdapter(视图);
}
@凌驾
公共无效onBindViewHolder(@NonNull galleryViewAdapter holder,int位置)
{
试一试{
使用(上下文)滑动。加载(list.get(position))。到(holder.gallery\u imageView);
holder.gallery_imageView.setOnClickListener(v->
{
意向意向=新意向(上下文,FullImageView.class);
intent.putExtra(“imageurl_pass”,list.get(position.getDurl());
背景。开始触觉(意图);
});
}捕获(例外e){
e、 printStackTrace();
}
holder.deletebtn.setOnClickListener(v->
{
AlertDialog.Builder dialog=新建AlertDialog.Builder(上下文);
setMessage(“删除此图像?”);
对话框。可设置可取消(true);
setPositiveButton(“确认”,对话框1,其中)->
{
Srafrance=FirebaseStorage.getInstance().getReference().child(“库”);
SRef=Srafrance.child(list.get(position.getSfilename());
SRef.delete().addOnSuccessListener(未使用->
{
Toast.makeText(上下文,“图像删除云”,Toast.LENGTH_SHORT).show();
}).addOnFailureListener(e->
{
Toast.makeText(上下文,“图像删除云失败”,Toast.LENGTH_SHORT.show();
})  ;
Drefrance=FirebaseDatabase.getInstance().getReference().child(“Gallery”);
DRef=Drefrance.child(list.get(position.getCategory());
DRef.removeValue()
.addOnCompleteListener(任务->
{
Toast.makeText(上下文“已删除”,Toast.LENGTH_SHORT).show();
}).addOnFailureListener(e->
{
Toast.makeText(上下文,“Seometing出错-Srefrance DN”,Toast.LENGTH_SHORT.show();
});
试一试{
已移除(位置)的项目;
}捕获(例外e){
e、 printStackTrace();
}
});
setNegativeButton(“Cancle”,(Idialog,which)->
{
Idialog.cancel();
});
AlertDialog=null;
试一试{
adilog=dialog.create();
}捕获(例外e){
e、 printStackTrace();
}
如果(对话框!=null)
{
dialog.show();
}
});
}
@凌驾
public int getItemCount(){
返回list.size();
}
公共类galleryViewAdapter扩展了RecyclerView.ViewHolder
{
ImageView图库\u ImageView;
按钮删除BTN;
公共galleryViewAdapter(@NonNull View itemView)
{
超级(项目视图);
gallery\u imageView=itemView.findViewById(R.id.gallery\u imageView);
deletebtn=itemView.findviewbyd(R.id.gallery\u delete\u btn);
}
}
}
“回收者”视图所在的库列表

package com.jkstudio.collageadmin.gallery;

import android.os.Bundle;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.jkstudio.collageadmin.R;

import java.util.ArrayList;

public class Gallery_list extends AppCompatActivity {
    RecyclerView gallery_recyclerview1;
    RecyclerView gallery_recyclerview2;
    RecyclerView gallery_recyclerview3;
    RecyclerView gallery_recyclerview4;
    RecyclerView gallery_recyclerview5;

    GalleryAdapter adapter;
    private DatabaseReference Drefrance, DbRef;
    ArrayList<GalleryData> list;


    @Override
    protected void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Drefrance = FirebaseDatabase.getInstance().getReference().child("Gallery");



        gallery_recyclerview1 = findViewById(R.id.gallery_recyclerview1);
        gallery_recyclerview2 = findViewById(R.id.gallery_recyclerview2);
        gallery_recyclerview3 = findViewById(R.id.gallery_recyclerview3);
        gallery_recyclerview4 = findViewById(R.id.gallery_recyclerview4);
        gallery_recyclerview5 = findViewById(R.id.gallery_recyclerview5);


        getCategory1();
        getCategory2();
        getCategory3();
        getCategory4();
        getCategory5();



    }


    private void getCategory1()
    {
        Drefrance.child("placement").addValueEventListener(new ValueEventListener()
        {
            @Override
            public void onDataChange(@NonNull  DataSnapshot snapshot)
            {


                for (DataSnapshot Dsnapshot : snapshot.getChildren())
                {
                    GalleryData data = Dsnapshot.getValue(GalleryData.class);
                    list.add(data);
                }
                adapter = new GalleryAdapter(Gallery_list.this,list);
                gallery_recyclerview1.setLayoutManager(new GridLayoutManager(Gallery_list.this,3));
                gallery_recyclerview1.setAdapter(adapter);
            }

            @Override
            public void onCancelled(@NonNull  DatabaseError error)
            {  Toast.makeText(Gallery_list.this,error.getMessage(), Toast.LENGTH_SHORT).show();

            }
        });

    }

    private void getCategory2()
    {
        Drefrance.child("events").addValueEventListener(new ValueEventListener()
        {
            @Override
            public void onDataChange(@NonNull  DataSnapshot snapshot)
            {


                for (DataSnapshot Dsnapshot : snapshot.getChildren())
                {
                    GalleryData data = Dsnapshot.getValue(GalleryData.class);
                    list.add(data);
                }
                adapter = new GalleryAdapter(Gallery_list.this,list);
                gallery_recyclerview2.setLayoutManager(new GridLayoutManager(Gallery_list.this,3));
                gallery_recyclerview2.setAdapter(adapter);
            }

            @Override
            public void onCancelled(@NonNull  DatabaseError error)
            {  Toast.makeText(Gallery_list.this,error.getMessage(), Toast.LENGTH_SHORT).show();

            }
        });

    }

    private void getCategory3()
    {
        Drefrance.child("festivals").addValueEventListener(new ValueEventListener()
        {
            @Override
            public void onDataChange(@NonNull  DataSnapshot snapshot)
            {


                for (DataSnapshot Dsnapshot : snapshot.getChildren())
                {
                    GalleryData data = Dsnapshot.getValue(GalleryData.class);
                    list.add(data);
                }
                adapter = new GalleryAdapter(Gallery_list.this,list);
                gallery_recyclerview3.setLayoutManager(new GridLayoutManager(Gallery_list.this,3));
                gallery_recyclerview3.setAdapter(adapter);
            }



            @Override
            public void onCancelled(@NonNull  DatabaseError error)
            {  Toast.makeText(Gallery_list.this,error.getMessage(), Toast.LENGTH_SHORT).show();

            }
        });

    }

    private void getCategory4()
    {
        Drefrance.child("achivements").addValueEventListener(new ValueEventListener()
        {
            @Override
            public void onDataChange(@NonNull  DataSnapshot snapshot)
            {

                for (DataSnapshot Dsnapshot : snapshot.getChildren())
                {
                    GalleryData data = Dsnapshot.getValue(GalleryData.class);
                    list.add(data);
                }
                adapter = new GalleryAdapter(Gallery_list.this,list);
                gallery_recyclerview4.setLayoutManager(new GridLayoutManager(Gallery_list.this,3));
                gallery_recyclerview4.setAdapter(adapter);
            }

            @Override
            public void onCancelled(@NonNull  DatabaseError error)
            {  Toast.makeText(Gallery_list.this,error.getMessage(), Toast.LENGTH_SHORT).show();

            }
        });
    }

    private void getCategory5()
    {
        Drefrance.child("sports").addValueEventListener(new ValueEventListener()
        {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot)
            {


                for (DataSnapshot Dsnapshot : snapshot.getChildren())
                {
                    GalleryData data = Dsnapshot.getValue(GalleryData.class);
                    list.add(data);
                }
                adapter = new GalleryAdapter(Gallery_list.this,list);
                gallery_recyclerview5.setLayoutManager(new GridLayoutManager(Gallery_list.this,3));
                gallery_recyclerview5.setAdapter(adapter);
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error)
            {  Toast.makeText(Gallery_list.this,error.getMessage(), Toast.LENGTH_SHORT).show();

            }
        });
    }








}
package com.jkstudio.collageadmin.gallery;
导入android.os.Bundle;
导入android.widget.Toast;
导入androidx.annotation.NonNull;
导入androidx.annotation.Nullable;
导入androidx.appcompat.app.appcompat活动;
导入androidx.recyclerview.widget.GridLayoutManager;
导入androidx.recyclerview.widget.recyclerview;
导入com.google.firebase.database.DataSnapshot;
导入com.google.firebase.database.DatabaseError;
导入com.google.firebase.database.DatabaseReference;
导入com.google.firebase.database.FirebaseDatabase;
导入com.google.firebase.database.ValueEventListener;
导入com.jkstudio.collageadmin.R;
导入java.util.ArrayList;
公共类库列表扩展了AppCompative活动{
RecyclerViewGallery_RecyclerViewW1;
RecyclerView画廊_RecyclerView 2;
RecyclerViewGallery_recyclerview3;
RecyclerView画廊_RecyclerView 4;
RecyclerView画廊_RecyclerView 5;
厨房适配器;
私人数据库参考Drefrance,DbRef;
数组列表;
@凌驾
创建时受保护的void(@Nullable@org.jetbrains.annotations.Nullable Bundle savedInstanceState){
苏
 <?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:animationCache="true"
    tools:context=".gallery.Gallery_list">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="16dp"

        >

        <com.google.android.material.card.MaterialCardView
            android:layout_width="match_parent"
            android:layout_height="300dp"

            android:layout_marginVertical="8dp"

            >


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


                <TextView
                    android:id="@+id/gallery_category1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:layout_marginTop="16dp"
                    android:fontFamily="@font/overpassbold"
                    android:text="placements"
                    android:textColor="#3f3b3b"
                    android:textSize="22dp"

                    />

                <View
                    android:layout_width="match_parent"
                    android:layout_height="2dp"
                    android:layout_marginVertical="4dp"
                    android:background="#828080" />

                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/gallery_recyclerview1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />


            </LinearLayout>


        </com.google.android.material.card.MaterialCardView>


        <com.google.android.material.card.MaterialCardView
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:layout_marginVertical="8dp"

            >


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


                <TextView
                    android:id="@+id/gallery_category2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:layout_marginTop="16dp"
                    android:fontFamily="@font/overpassbold"
                    android:text="events"
                    android:textColor="#3f3b3b"
                    android:textSize="22dp"

                    />

                <View
                    android:layout_width="match_parent"
                    android:layout_height="2dp"
                    android:layout_marginVertical="4dp"
                    android:background="#828080" />

                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/gallery_recyclerview2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />


            </LinearLayout>


        </com.google.android.material.card.MaterialCardView>

        <com.google.android.material.card.MaterialCardView
            android:layout_width="match_parent"
            android:layout_height="300dp"

            android:layout_marginVertical="8dp"

            >


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


                <TextView
                    android:id="@+id/gallery_category3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:layout_marginTop="16dp"
                    android:fontFamily="@font/overpassbold"
                    android:text="festivals"
                    android:textColor="#3f3b3b"
                    android:textSize="22dp"

                    />

                <View
                    android:layout_width="match_parent"
                    android:layout_height="2dp"
                    android:layout_marginVertical="4dp"
                    android:background="#828080" />

                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/gallery_recyclerview3"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />


            </LinearLayout>


        </com.google.android.material.card.MaterialCardView>

        <com.google.android.material.card.MaterialCardView
            android:layout_width="match_parent"
            android:layout_height="300dp"

            android:layout_marginVertical="8dp"

            >


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


                <TextView
                    android:id="@+id/gallery_category4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:layout_marginTop="16dp"
                    android:fontFamily="@font/overpassbold"
                    android:text="Achivements"
                    android:textColor="#3f3b3b"
                    android:textSize="22dp"

                    />

                <View
                    android:layout_width="match_parent"
                    android:layout_height="2dp"
                    android:layout_marginVertical="4dp"
                    android:background="#828080" />

                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/gallery_recyclerview4"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />


            </LinearLayout>


        </com.google.android.material.card.MaterialCardView>

        <com.google.android.material.card.MaterialCardView
            android:layout_width="match_parent"
            android:layout_height="300dp"

            android:layout_marginVertical="8dp"

            >


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


                <TextView
                    android:id="@+id/gallery_category5"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:layout_marginTop="16dp"
                    android:fontFamily="@font/overpassbold"
                    android:text="Sports"
                    android:textColor="#3f3b3b"
                    android:textSize="22dp"

                    />

                <View
                    android:layout_width="match_parent"
                    android:layout_height="2dp"
                    android:layout_marginVertical="4dp"
                    android:background="#828080" />

                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/gallery_recyclerview5"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />


            </LinearLayout>


        </com.google.android.material.card.MaterialCardView>


    </LinearLayout>


</androidx.core.widget.NestedScrollView>
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/gallery_image_item_card"

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

    <ImageView
        android:id="@+id/gallery_imageView"
        android:layout_width="wrap_content"
        android:layout_height="90dp"
        android:layout_gravity="center"
        android:scaleType="fitXY"


        app:srcCompat="@drawable/noimg" />
    <com.google.android.material.button.MaterialButton
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/gallery_delete_btn"
        android:text="delete"
        android:textSize="12dp"
         </LinearLayout>
  </com.google.android.material.card.MaterialCardView>