23

Explain me please, why IntelliJ IDEA shows me Parameter never used?

enter image description here

 public void onClick(View view) { // Parameter 'view' never used
    Toast.makeText(this, "Hello", Toast.LENGTH_LONG).show();
}

<ToggleButton
    ...
    android:onClick="onClick"/>

But I know, if I delete this never used parameter (view), Android will throw me Runtime Exception.

Why IntelliJ IDEA v12.1 shows such warnings but Eclipse doesn't show. How to configure IntelliJ IDEA to hide such warnings for all Android projects?

CoolMind
  • 26,736
  • 15
  • 188
  • 224
Tom Yang
  • 255
  • 1
  • 4
  • 8

6 Answers6

49

For Kotlin, use this sentence to supress this warning :

fun onClick(@Suppress("UNUSED_PARAMETER")view: View) {

}
Kevin ABRIOUX
  • 16,507
  • 12
  • 93
  • 99
  • 1
    Is suppression really the answer? What if you replace the value with "_" as suggested by the lint? – portfoliobuilder Aug 20 '19 at 17:54
  • 5
    Currently, as of Android Studio 3.5, using the discard ("_") causes a compilation error: "_, __, ___ are reserved names in kotlin" (formatting is broken) – Yennefer Sep 30 '19 at 12:13
  • For future reference, I feel like the answer given by @mgueydan is an all around better way to handle this, rather than suppress it, turn into into a simple test to improve maintainability – Blü Nov 25 '21 at 14:01
12

I don't think that changing the warning setting would be the best solution.

Instead you can try this code:

public void onClick(@SuppressWarnings("UnusedParameters")View ignored) { // Parameter 'ignored' never used
    Toast.makeText(this, "Hello", Toast.LENGTH_LONG).show();
}
Allan Spreys
  • 5,287
  • 5
  • 39
  • 44
6

Because you never use parameter view in method onClick. But that default signature for this method.

for example, if you will use a parameter in a method, IntelliJ IDEA doesn't shows "Parameter never used"

public void onClick(View view) { // Parameter 'view' used
if(view.getId() == R.id.myId)  //example start
{
    Toast.makeText(this, "CorrectId", Toast.LENGTH_LONG).show();
}                             //example finish
Toast.makeText(this, "Hello", Toast.LENGTH_LONG).show();
}

Update:

For example you have 3 buttons in main layout:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/my_switcher"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

<Button
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/btn1"
        android:onClick="onClick"
        />
<Button
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/btn2"
        android:onClick="onClick"
        />
<Button
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/btn3"
        android:onClick="onClick"
        />

</LinearLayout>

I your activity metod onClick handle events from three buttons. And we need to recognize that the button has been pressed.

 public void onClick(View view) { // Parameter 'view' used
    switch (view.getId())
    {
        case (R.id.btn1):
        {
            Toast.makeText(this, "Hello Button_1 pressed", Toast.LENGTH_LONG).show();
            break;
        }
        case (R.id.btn2):
        {
            Toast.makeText(this, "Hello Button_2 pressed", Toast.LENGTH_LONG).show();
            break;
        }
        case (R.id.btn3):
        {
            Toast.makeText(this, "Hello Button_2 pressed", Toast.LENGTH_LONG).show();
            break;
        }
    }

This is one example of how you can use the 'view'

jimpanzer
  • 3,470
  • 4
  • 44
  • 84
  • I know but eclipse for example don't warn about it. Is possible to configure IntelliJ IDEA to hide such warnings for all android projects? – Tom Yang Apr 10 '13 at 13:20
  • Yes, check [this answer](http://stackoverflow.com/questions/7111027/intellij-disable-code-analysis-light-bulb) – jimpanzer Apr 10 '13 at 13:23
  • This is just a warning that you have not forgotten anything. This should be respected. IDE respects you! – jimpanzer Apr 10 '13 at 13:26
  • I don't want to use view parameter I only want to show a Toast to the user why put if else statements it's silly. – Tom Yang Apr 10 '13 at 13:29
  • I wrote this code, to show that if you use in the method 'view', then ide does not swear. Just use your code - everything is OK with him. But if a few more controls will Handle Charging the same method, then you have to compare his id, to understand what action to take. – jimpanzer Apr 10 '13 at 13:34
  • @TomYang I updated my answer. Hope you understand, why 'view' couldn't be removed. – jimpanzer Apr 10 '13 at 13:48
  • Thanks. But please understand me too, I know how to handle buttons clicks in Android and Java etc. – Tom Yang Apr 10 '13 at 14:15
  • Why can't the Code Analysis system see a handler that enforces a specific signature links to the method, hence the programmer can't remove it even if it's unused? – Phlip Jun 04 '20 at 23:55
6

You see this warning because the Unused symbol inspection is enabled by default and is configured to show any unused parameters as well. While it's not an issue in this specific case, it may help you to trace bugs in other cases (like when you are using a local variable with similar name because of the typo instead of the actual method parameter).

This inspection provides a lot of options and you can tune it for your needs, for example by disabling the Check Parameters option. Or you can define annotations like @Unused so that IDE will ignore any parameters with these annotations, or you can just suppress this inspection from the Alt+Enter Menu, right arrow sub menu (suppress for class/method/statement, edit inspection settings or disable it completely).

unused symbol

suppress

CrazyCoder
  • 389,263
  • 172
  • 990
  • 904
4

Everything said above is great and true. Here is another way to see it :

At first I didn't see the need to use this parameter. It is due to the fact that, in the application I am working on, there only is a single View (probably Button), that call this OnClick callback.

That said, there is no need to check the parameter. There is only one button, it must be it.

So I became puzzled, angry or sad that:

  • the API is requiring the parameter,
  • the IDE is complaining about the parameter not being used.

...

But we could see it another way :

There is no need to check the parameter. There is only one button, it must be it

There is nothing in my project that ensure the previous statement. It may be true today and it may become untrue later in the project. It also may be untrue by mistake or accident. A simple bug.

And what does a developer do when he wants an application to be easy to maintain ? He checks or ensure facts he wants to be true.

So the way I chose to make this warning disappear is :

fun start_sync(button : View){
    assert(button.id == R.id.my_only_button)
    //...
}

Finally, both IDE and API were right, the mistake was to assume I didn't need this parameter.

mgueydan
  • 356
  • 1
  • 13
0

The warning is obvious that the method parameter is not used anywhere in the method.

But looking only at the method can be too narrow. If your method handles an event such as onClick view: View parameter is mandated by the listener signature. If you removed the parameter you get an Exception such as the following..

 Could not find method buttonClicked(View) in a parent or ancestor Context for android:onClick

Ideally, Android Studio (IntelliJ) should be able to identify this case and should not show the warning but sadly this is not the case.

So we have two options

  1. Suppress this warning (Android Studio tool shows the options)

    @Suppress("UNUSED_PARAMETER")//Kotlin attribute

  2. Use this parameter in something not related to logic such as Log statement.

Sandeep Dixit
  • 799
  • 7
  • 12