简体   繁体   English

Android Kotlin:在显示 Snackbar 之前检查视图是否不是 null

[英]Android Kotlin: check if view is not null before displaying Snackbar

I used the following code to display a snackbar when data has been loaded using retrofit .当使用retrofit加载数据时,我使用以下代码显示小吃栏。

Sometimes, I get a NullPointerException when user tries to navigate from this fragment to another one whereas the request to get data is not yet terminated:有时,当用户尝试从该片段导航到另一个片段而获取数据的请求尚未终止时,我会收到NullPointerException

kotlin.KotlinNullPointerException at com.test.data.ui.cars.SiteCarsFragment$getCars$1.onFailure(SiteCarsFragment.kt:279) at retrofit2.DefaultCallAdapterFactory$ExecutorCallbackCall$1$2.run(DefaultCallAdapterFactory.java:92) 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:7520) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.Z93F725A07423FE1C889F448B33D21F kotlin.KotlinNullPointerException at com.test.data.ui.cars.SiteCarsFragment$getCars$1.onFailure(SiteCarsFragment.kt:279) at retrofit2.DefaultCallAdapterFactory$ExecutorCallbackCall$1$2.run(DefaultCallAdapterFactory.java:92) 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:7520) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.Z93F725A07423FE1C889F448B33D21F 46Z:539) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950) 46Z:539) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.Z93F725A07423FE1C889F448B03D21F46Z9)5:

It's due to the fact that view is null in that case.这是因为在这种情况下view是 null 。

How can I modify the code to be sure that the view is defined before displaying the snackbar?如何修改代码以确保在显示快餐栏之前定义视图?

// Call getCars() API
val call = App.myService.getCars(site.id!!)
call.enqueue(object : Callback<CarsResponse> {

    override fun onResponse(call: Call<CarsResponse>, response: Response<CarsResponse>) {

        Log.i(tagLabel, "getCars() - onResponse() Result = ${response.body()}")

        if (response.code() == 200) {

            if (response.body()?.data?.size == 0){
                // do stuff
            } else {
                val sb = Snackbar.make(view!!, getString(R.string.mytitle), Snackbar.LENGTH_SHORT)
                sb.view.setBackgroundColor(ContextCompat.getColor(context!!, R.color.snackbarGreen))
                sb.show()
            }

        }
    }

    override fun onFailure(call: Call<CarsResponse>, t: Throwable) {
        // do stuff
    }

})

I would try this one.我会试试这个。 I just added a null check我刚刚添加了一个 null 检查

if(view != null){

  // Your code
  val sb = Snackbar.make(view!!, getString(R.string.mytitle), Snackbar.LENGTH_SHORT)
  sb.view.setBackgroundColor(ContextCompat.getColor(context!!, R.color.snackbarGreen))
  sb.show()
}

You should use a view referenced to your activity (as activity views will not be destroyed if one of your fragment is).您应该使用引用您的活动的视图(因为如果您的片段之一是活动视图,则不会被破坏)。 If you have any instance of activity's view, pass it there.如果您有任何活动视图的实例,请将其传递到那里。

Or you can try this:或者你可以试试这个:

val sb = Snackbar.make(activity!!.findViewById(android.R.id.content), getString(R.string.mytitle), Snackbar.LENGTH_SHORT)

here I have used !!这里我用过!! but activity will not be null unless its destroyed.但活动不会是 null 除非它被破坏。

Edit: Here if you use view of fragment with null check, your snackbar will not be shown if your fragment's view is destroyed by switching between fragments.编辑:在这里,如果您使用带有 null 检查的片段视图,如果您的片段视图因在片段之间切换而被破坏,您的小吃栏将不会显示。 So you should use a activity's view or the code above if you want it to be shown even after your fragment's view destroyed.因此,如果您希望在片段的视图被破坏后仍显示它,则应该使用活动的视图或上面的代码。

I would recommend you to get rid of the double-bang(!!) operator and use the null-safety(?) operator, in the place where you are using view!!我建议您在使用视图的地方摆脱双键(!!)运算符并使用空安全(?)运算符! and context.!.上下文。!

You should do something like this:你应该这样做:

if(view !=null){
//TODO Snakbar code here.
}

Hope this helps.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM