【问题标题】:Unable to initialize object of type Animation无法初始化动画类型的对象
【发布时间】:2015-10-02 06:31:30
【问题描述】:

我在初始化动画类型的对象时收到以下警告。

(警告被添加为 cmets)

Animation bottomUp = AnimationUtils.loadAnimation(
     android.content.Context, // warning: Expression expected 
     R.animator.bottom_up     // warning: Expected resource of type anim
);

这是一张照片

这里是代码摘要:

   public class MainActivity extends AppCompatActivity {

        // Class variables go here

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

                // Set onclick listener 

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

                    // Animation Code

                    Animation bottomUp = AnimationUtils.loadAnimation(
                            android.content.Context, // warning: Expression expected 
                            R.animator.bottom_up // warning: Expected resource of type 
                    );


                    ViewGroup hiddenPanel = (ViewGroup)findViewById(R.id.hidden_panel);
                    hiddenPanel.startAnimation(bottomUp);
                    hiddenPanel.setVisibility(View.VISIBLE);


                }
            });



        }

        // Other stuff
    }

这是我尝试编译后的日志猫

这是我使用错误代码的地方

  public class MainActivity extends AppCompatActivity {

listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {

 @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

            Animation bottomUp = AnimationUtils.loadAnimation(
                    android.content.Context,
                    R.animator.bottom_up
            );


            ViewGroup hiddenPanel = (ViewGroup)findViewById(R.id.hidden_panel);
            hiddenPanel.startAnimation(bottomUp);
            hiddenPanel.setVisibility(View.VISIBLE);

我找不到错误。

我创建了正确的文件夹和文件。他们来了。

Here 是我得到我正在使用的动画代码的地方。

bottom_up.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate 
        android:fromYDelta="75%p" 
        android:toYDelta="0%p"
        android:fillAfter="true"
        android:duration="500"/>
</set>

bottom_down.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate 
        android:fromYDelta="0%p" 
        android:toYDelta="100%p" 
        android:fillAfter="true"
        android:interpolator="@android:anim/linear_interpolator"
        android:duration="500" />
</set>

Java

Animation bottomUp = AnimationUtils.loadAnimation(getContext(),
            R.anim.bottom_up);
ViewGroup hiddenPanel = (ViewGroup)findViewById(R.id.hidden_panel);
hiddenPanel.startAnimation(bottomUp);
hiddenPanel.setVisibility(View.VISIBLE);

试图创建一个anim 文件夹。收到这条消息。

【问题讨论】:

  • 警告在哪里?而在代码的第一个摘录中,android.content.Context 是类,你应该提供一个实例
  • @IliiazAkhmedov 警告在对象初始化代码中作为 cmets 添加

标签: android


【解决方案1】:

第一个参数应该是 Context 对象。如果您在 Activity 中使用它,请尝试“this”。

第二个警告是因为您在 animator 文件夹中有您的动画(因此它被识别为 animator 资源),并且 AnimationUtils.loadAnimation 中的第二个参数带有 @AnimRes 注释,因此这只是警告/建议您的参数应该是资源类型的动画。这只是 lint 警告而不是编译器。

这个你可以编译:

Animation bottomUp = AnimationUtils.loadAnimation(
 this, // if You are in Activity
 R.animator.bottom_up 
);

要加载 Animator 试试:

Animator animator = AnimatorInflater.loadAnimator(this, R.animator.bottom_up );

但这不是你想要的。只需将文件移动到 anim 文件夹即可。

完整示例代码:

package com.example.user.exampleapp;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Animation bottomUp = AnimationUtils.loadAnimation(this,
            R.anim.bottom_up);
        TextView hiddenPanel = (TextView) findViewById(R.id.textView);
        hiddenPanel.startAnimation(bottomUp);
    }
}

【讨论】:

  • 我已经尝试过this。这只会导致required: 'android.content.Context'不确定我是否理解你的第二点,但下划线是红色的,所以这是一个错误,而不仅仅是“lint catch”。
  • @the_prole 相信我,这只是 lint ;) 你能显示更多代码吗?你在哪里使用你的动画负载?
  • 不,这是编译错误。我在顶部添加了图片,以及日志猫输出。此外,其余代码请参见底部的 Java。
  • 1.将文件从动画师移动到动画文件夹。 2.传递上下文,而不是一些奇怪的 android.content.Context 东西 ;)
  • 你确定吗? animator 文件夹是我在资源目录中创建动画文件时由 Android Studio 创建的。如何创建带有动画资源的anim 文件夹?
【解决方案2】:

首先,您是否在活动中使用它?

我只知道如何处理第一个警告,很抱歉,但我自己是个业余爱好者。 如果您在活动中,您可以使用以下方法获取上下文:

this

getApplicationContext()

如果您不在活动范围内,则必须将上下文传递给使用它的类:

public class MainActivity extends Activity(){

     ThisClassNeedsContext test;

     public MainActivity(Context context){
          // other code
          test = new ThisClassNeedsContext(context);
     }
}

public class ThisClassNeedsContext { 
     public ThisClassNeedsContext(Context context){
          // now you have acces to the context
     }
}

我希望这会有所帮助!祝你好运 编辑:解释中的错字

【讨论】:

  • 我在主函数的 onclick 监听器中使用它。请参阅帖子底部的代码摘要。
  • Err 好吧我很抱歉我在这里和那里犯了一些错误。我实际上误解了我自己的代码'-_- 正如塞巴斯蒂安的回答中提到的那样,这实际上应该可以解决问题。我将编辑我的答案。
  • 好的,所以我找到了其他可能有用的东西。请参阅我的代码以获取更新版本。
猜你喜欢
  • 1970-01-01
  • 2014-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多