我创建了一个列表视图,它将显示我的数据库子项。如果我单击它,我将有一个警报对话框,询问我是否要删除图像。
删除图像后,我将尝试转到显示ListView的活动,该活动会崩溃并显示日志:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.user.alphaver, PID: 5739
java.lang.NullPointerException: uriString
at android.net.Uri$StringUri.<init>(Uri.java:496)
at android.net.Uri$StringUri.<init>(Uri.java:486)
at android.net.Uri.parse(Uri.java:458)
at com.example.alphaver.ImagesActivity$1.populateView(ImagesActivity.java:61)
at com.firebase.ui.database.FirebaseListAdapter.getView(FirebaseListAdapter.java:121)
at android.widget.AbsListView.obtainView(AbsListView.java:2405)
at android.widget.ListView.makeAndAddView(ListView.java:2071)
at android.widget.ListView.fillDown(ListView.java:797)
at android.widget.ListView.fillFromTop(ListView.java:859)
at android.widget.ListView.layoutChildren(ListView.java:1814)
at android.widget.AbsListView.onLayout(AbsListView.java:2202)
at android.view.View.layout(View.java:21912)
at android.view.ViewGroup.layout(ViewGroup.java:6260)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1829)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1582)
at android.view.View.layout(View.java:21912)
at android.view.ViewGroup.layout(ViewGroup.java:6260)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
at android.view.View.layout(View.java:21912)
at android.view.ViewGroup.layout(ViewGroup.java:6260)
at androidx.appcompat.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:446)
at android.view.View.layout(View.java:21912)
at android.view.ViewGroup.layout(ViewGroup.java:6260)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
at android.view.View.layout(View.java:21912)
at android.view.ViewGroup.layout(ViewGroup.java:6260)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1829)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1582)
at android.view.View.layout(View.java:21912)
at android.view.ViewGroup.layout(ViewGroup.java:6260)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:779)
at android.view.View.layout(View.java:21912)
at android.view.ViewGroup.layout(ViewGroup.java:6260)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:3080)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2590)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1721)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7598)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:966)
at android.view.Choreographer.doCallbacks(Choreographer.java:790)
at android.view.Choreographer.doFrame(Choreographer.java:725)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:951)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
这是我导致崩溃的ListView代码,但我不知道为什么:
lv = (ListView)findViewById(R.id.lv);
Query query = FirebaseDatabase.getInstance().getReference().child("Uploads");
FirebaseListOptions<Upload> options = new FirebaseListOptions.Builder<Upload>()
.setLayout(R.layout.information)
.setLifecycleOwner(ImagesActivity.this)
.setQuery(query, Upload.class)
.build();
adapter = new FirebaseListAdapter(options) {
@Override
protected void populateView(@NonNull View v, @NonNull Object model, int position) {
TextView name = v.findViewById(R.id.name_tv);
ImageView imageView = v.findViewById(R.id.imageViewTV);
Upload std = (Upload) model;
name.setText(std.getName());
if(Uri.parse(std.getmImageUrl()) != null){
Picasso.get().load(Uri.parse(std.getmImageUrl())).into(imageView);
}
}
};
我希望解决问题,并在没有要显示的 mImageUrl 值时使列表视图不显示任何内容。
看起来std.getmImageUrl()
在没有URL时返回null
,Uri.parse
无法处理。
简单的解决方案是为该null
添加额外的检查:
if(std.getmImageUrl() != null && Uri.parse(std.getmImageUrl()) != null){
Picasso.get().load(Uri.parse(std.getmImageUrl())).into(imageView);
}