android studio java 中布局管理器的空指针异常
null pointer Exeption for layout manager in android studio java
所以在管理应用程序中,我想添加从 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.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.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);
}
}
}
Gallery_list回收站视图
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();
}
});
}
}
xml 文件 gallery_list
<?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
<?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>
您在 Gallery_list activity 中缺少 setContentView(R.layout.gallery_list);。在 super.onCreate(savedInstanceState); 之后添加 activity.
所以在管理应用程序中,我想添加从 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.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.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);
}
}
}
Gallery_list回收站视图
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();
}
});
}
}
xml 文件 gallery_list
<?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
<?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>
您在 Gallery_list activity 中缺少 setContentView(R.layout.gallery_list);。在 super.onCreate(savedInstanceState); 之后添加 activity.