How to avoid complier warning Parameter 'x' is never used

孤者浪人 提交于 2019-12-25 01:00:20

问题


Suggest the best way to solve this type of compiler warning in Android Studio 3.3

Note: I found many solutions to avoid the warnings and uncheck the Studio inspection. But I am expecting something different.

Example: If warning Parameter 'view1' is never used is in the button onClick method.

Method

fun buttonClicked(view1: View) {
//   Call Intent to new Activity . 
// Parameter **view1** is not used  }

Call method from XML

 <Button
 android:id="@+id/button_id"
 android:layout_height="wrap_content"
 android:layout_width="wrap_content"
 android:onClick="@{viewModel::buttonClicked}"
 android:text="@string/example" />.

So we need (view1: View) parameter for the method for onClick. But it is not used. How can I solve this warning?. (Just one example).


回答1:


It took me a few days to figure out the exact way to manage this warning without using @SuppressWarnings("unused")

android:onClick="@{viewModel::buttonClicked}" is equal to android:onClick="@{(v) -> viewModel.buttonClicked(v)}, both were calling same method fun buttonClicked(view1: View) in Kotlin. (just try to mention difference between '::' and '.' usage)

So I fixed my warning my calling a method explicitly instead of using -'::'

fun buttonClicked() // Button onClick function

It will be called from XML - >android:onClick="@{(v) -> viewModel.buttonClicked()}". Thus "Parameter view1 is never used" will not occur any more.

Simple Fix !! Happy Coding




回答2:


I just make sure to use the view variable in a cheap way so the error goes away:

Log.d("IGNORE", "Logging view to curb warnings: $view1")

Not an elegant answer, but one Log isn't gonna slow down the app and it does the trick



来源:https://stackoverflow.com/questions/54770552/how-to-avoid-complier-warning-parameter-x-is-never-used

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!