简体   繁体   English

Android:从未使用过 IntelliJ IDEA 12.1 参数视图

[英]Android: IntelliJ IDEA 12.1 Parameter view never used

Explain me please, why IntelliJ IDEA shows me Parameter never used?请解释一下,为什么 IntelliJ IDEA 显示参数从未使用过?

在此处输入图像描述

 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.但我知道,如果我删除这个从未使用过的参数(视图),Android 会抛出运行时异常。

Why IntelliJ IDEA v12.1 shows such warnings but Eclipse doesn't show.为什么 IntelliJ IDEA v12.1 显示此类警告但 Eclipse 不显示。 How to configure IntelliJ IDEA to hide such warnings for all Android projects?如何配置 IntelliJ IDEA 以隐藏所有 Android 项目的此类警告?

For Kotlin, use this sentence to supress this warning :对于 Kotlin,使用这句话来抑制这个警告:

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).或者,您可以定义@Unused之类的注释,以便 IDE 忽略带有这些注释的任何参数,或者您可以从Alt + Enter菜单、右箭头子菜单(抑制类/方法/语句、编辑检查设置或完全禁用它)。

未使用的符号

压制

Because you never use parameter view in method onClick .因为您从不在方法onClick中使用参数view 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"例如,如果您将在方法中使用参数,IntelliJ IDEA 不会显示“从未使用过的参数”

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:例如,您在主布局中有 3 个按钮:

<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.我您的活动方法是 onClick 处理来自三个按钮的事件。 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.这是因为在我正在处理的应用程序中,只有一个视图(可能是按钮)调用此 OnClick 回调。

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, API 需要参数,
  • the IDE is complaining about the parameter not being used. IDE 抱怨未使用该参数。

... ...

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.最后,IDE和API都是对的,错误是假设我不需要这个参数。

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.如果您的方法处理诸如 onClick 视图之类的事件: View 参数由侦听器签名强制执行。 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.理想情况下,Android Studio (IntelliJ) 应该能够识别这种情况并且不应该显示警告,但遗憾的是事实并非如此。

So we have two options所以我们有两个选择

  1. Suppress this warning (Android Studio tool shows the options)禁止显示此警告(Android Studio 工具显示选项)

    @Suppress("UNUSED_PARAMETER")//Kotlin attribute @Suppress("UNUSED_PARAMETER")//Kotlin 属性

  2. Use this parameter in something not related to logic such as Log statement.在 Log 语句等与逻辑无关的地方使用该参数。

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

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