我正在使用我们位于导航抽屉中的一个片段上的GridView为我们的android应用创建图库。我为图像视图创建了一个自定义适配器,该适配器使用Picasso在图像视图中显示图像。我制作了一种名为DohvatiSlike()的方法,该方法从firebase下载图像并将其保存在File变量中。当我构建应用程序并运行它时,每当我从导航抽屉中打开图库时,应用程序都会崩溃。如果我在onCreateView中排除设置GalleryGridAdapter的行,则不会崩溃,但显然不起作用。谁能帮我发现问题?
这是GalleryFragment代码:
public class GalleryFragment extends Fragment implements NavigationItem {
private DatabaseReference mDatabaseRef;
private List<File> slike;
private int position;
GridView galleryGridView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.gallery_fragment, container, false);
super.onCreateView(inflater, container, savedInstanceState);
galleryGridView = (GridView)v.findViewById(R.id.gallery_gridview);
DohvatiSlike();
galleryGridView.setAdapter(new GalleryGridAdapter(getActivity(), slike));
return v;
}
public void DohvatiSlike(){
mDatabaseRef=FirebaseDatabase.getInstance().getReference("images");
mDatabaseRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot postSnapshot: dataSnapshot.getChildren()){
File slika = postSnapshot.getValue(File.class);
slike.add(slika);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText((AppCompatActivity)getActivity(), databaseError.getMessage(), Toast.LENGTH_SHORT).show();
}
});}
@Override
public void onAttach(Activity activity){
super.onAttach(activity);
}
这是适配器代码:
public class GalleryGridAdapter extends BaseAdapter {
private Context mContext;
public List<File> slike;
public GalleryGridAdapter(Context context, List<File> slikice){
slike = slikice;
mContext = context;
}
@Override
public int getCount() {
return slike.size();
}
@Override
public Object getItem(int position) {
return slike.indexOf(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(500, 500));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
} else{
imageView = (ImageView) convertView;
}
Picasso.with(mContext).load(slike.indexOf(position)).into(imageView);
return imageView;
}
}
编辑*
这是崩溃日志
Process: hr.foi.air1817.botanico, PID: 26065
java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
at hr.foi.air1817.botanico.adapters.GalleryGridAdapter.getCount(GalleryGridAdapter.java:26)
at android.widget.GridView.setAdapter(GridView.java:211)
at hr.foi.air1817.botanico.fragments.GalleryFragment$1.onDataChange(GalleryFragment.java:62)
at com.google.firebase.database.obfuscated.zzap.zza(com.google.firebase:firebase-database@@16.0.3:75)
at com.google.firebase.database.obfuscated.zzca.zza(com.google.firebase:firebase-database@@16.0.3:63)
at com.google.firebase.database.obfuscated.zzcd$1.run(com.google.firebase:firebase-database@@16.0.3:55)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6863)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
我已经连续两天在这里工作了,只是无法正常工作,建议或想法得到赞赏
答案 0 :(得分:2)
这是异步操作的一个常见问题,当您执行此操作时,您的代码将始终崩溃,因为在您从Firebase到达适配器数据之前,您正在设置适配器数据
DohvatiSlike();
galleryGridView.setAdapter(new GalleryGridAdapter(getActivity(), slike));
方法DohvatiSlike();
仍在请求数据,可能无法在您拥有的Array
处添加元素。
相反,将适配器的设置移至DohvatiSlike();
方法内
public void DohvatiSlike(){
slike = new List<>();
mDatabaseRef = FirebaseDatabase.getInstance().getReference("images");
mDatabaseRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot postSnapshot: dataSnapshot.getChildren()){
File slika = postSnapshot.getValue(File.class);
slike.add(slika);
}
galleryGridView.setAdapter(new GalleryGridAdapter(getActivity(), slike));
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText((AppCompatActivity)getActivity(), databaseError.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
执行DohvatiSlike()
时,您将需要等待,具体取决于您的Internet连接以获取数据,在获取数据之后,您将填充阵列,在这种情况下,您正在请求数据并立即使用可能尚未获取的数据,只需在解析所有数据并将其添加到onDataChange
内之后,再设置适配器即可。
此外,在显示视图时,请在onViewCreated()
内执行此操作,因为onCreateView()
仅用于夸大视图元素