2

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).

Saikrishna Rajaraman
  • 3,205
  • 2
  • 16
  • 29
Joyal
  • 414
  • 7
  • 19
  • https://stackoverflow.com/questions/23382532/remove-method-is-never-used-warning-for-onclick-annotation-in-android-studio – Zoe Feb 19 '19 at 16:18
  • @Zoe what will be the command in mac machine. Also, I'm not using butterknife in my project – Joyal Feb 19 '19 at 16:37
  • Same idea, different keys. I have no clue what alt is on a mac. – Zoe Feb 19 '19 at 16:47

2 Answers2

2

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

Joyal
  • 414
  • 7
  • 19
0

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

Hannah S.
  • 338
  • 1
  • 4
  • 11