简体   繁体   中英

Android: IntelliJ IDEA 12.1 Parameter view never used

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

在此处输入图像描述

 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?

For Kotlin, use this sentence to supress this warning :

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

}

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();
}

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

未使用的符号

压制

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'

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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