简体   繁体   English

错误的第一个参数类型

[英]Wrong 1st argument type

I am relatively new to Java and Android development, so I am sorry for disturbing you. 我对Java和Android开发相对较新,所以我很抱歉打扰你。 I want to create fade-in animation on long click using AnimationUtils.LoadAnimation(), but I am facing error: 我想使用AnimationUtils.LoadAnimation()长时间点击创建淡入动画,但我遇到错误:

Wrong 1st argument type. 错误的第一个参数类型。 Found: 'android.view.View.OnLongClickListener', required: 'android.content.Context' 找到:'android.view.View.OnLongClickListener',必填:'android.content.Context'

This is my code: 这是我的代码:

  BasicsButton.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {

            Vibrator vib = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                vib.vibrate(VibrationEffect.createOneShot(500,VibrationEffect.DEFAULT_AMPLITUDE));
            }else{
                vib.vibrate(500);
            }
                Animation in = AnimationUtils.loadAnimation(this, R.anim.fadein);
                blurView.startAnimation(in);
                blurView.setVisibility(View.VISIBLE);
            return true;
        }

I am unaware of what is wrong, this example seems to work, but not for me. 我不知道什么是错的, 这个例子似乎有用,但不适合我。

Thank you in advance. 先感谢您。 :) :)

The problem is following line 问题是跟随线

Animation in = AnimationUtils.loadAnimation(this, R.anim.fadein);

Since the above method is call in anonymous class, this refers to OnLongClickListener rather than Activity. 由于上述方法是在匿名类中调用的, this指的是OnLongClickListener而不是Activity。

Change it to as follows: 将其更改为如下:

Animation in = AnimationUtils.loadAnimation(<ActivityName>.this, R.anim.fadein);

if you use this code inside a fragment, use getContext() instead of this. 如果在片段中使用此代码,请使用getContext()而不是此。

Have you tried the below code: 您是否尝试过以下代码:

  Vibrator vib = (Vibrator) getContext().getSystemService(Context.VIBRATOR_SERVICE);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
               vib.vibrate(VibrationEffect.createOneShot(500,VibrationEffect.DEFAULT_AMPLITUDE));
            }else{
                vib.vibrate(500);
            }
            Animation in = AnimationUtils.loadAnimation(getContext(), R.anim.slide_in_left);
            blurView.startAnimation(in);
            blurView.setVisibility(View.VISIBLE);

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

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