当前位置: 首页 > 知识库问答 >
问题:

无法初始化Animation类型的对象

裴英才
2023-03-14

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

(警告添加为注释)

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 {

列表视图1。setOnItemClickListener(新的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);

我找不到错误。

我创建了正确的文件夹和文件。在这里。

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

自下而上。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文件夹。收到这条信息。

共有2个答案

赫连泰宁
2023-03-14

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

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

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
     }
}

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

孔华池
2023-03-14

第一个参数应该是上下文对象。如果你在活动中使用它,请尝试“this”。

第二个警告是因为您的动画位于animator文件夹中(因此它被识别为animator资源),第二个参数位于AnimationUtils中。loadAnimation使用@AnimRes annotation进行注释,因此这只是警告/建议您的参数应为anim类型的资源。这只是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 );

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

完整示例代码:

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);
    }
}
 类似资料:
  • 大家好,我是Kotlin语言的新手,在运行Hello World代码的过程中遇到了一些错误。 无法为初始化脚本'C:\User\HP\AppData\Local\Temp\wrapper_init.gradle'打开初始化泛型类缓存(C:\User\HP. gradle\cache\6.8\script\5mjee5vr2mabvexqryui51pg)。 缺陷源单元“BuildScript”中的

  • 当我在iReport 5.1.0中预览JasperReports的报告时,它执行得很好。它包含一个饼图,当我需要从jsp文件运行它时,问题就来了。 烧烤-1.5-beta1.jar commons-beanutils-1.8.2.jar Commons-Collections-3.2.1.jar commons-digester-2.1.jar commons-javaflow-20060411.

  • 我正在使用tomee服务器运行我的javaEE应用程序。我写了一个过滤器,它注入一个对象。但是,该对象似乎没有被实例化: 以下是我的代码: Filter.java Faculty.java 我得到了一个NPE。以下是堆栈跟踪: 下面是正在使用的工件的版本: 我已经尝试在META-INF和WEB-INF中包含beans.xml,但我仍然看到NPE.Can有人让我知道我做错了什么?

  • 问题内容: 是我自己的一类。该类与主类位于同一JAR文件中。因此,这不应该是因为classpath中缺少任何JAR。 当我通过查阅JAR文件时,可以看到其中列出的内容。 顺便说一句:代码在我的本地计算机上运行良好。但是当我将其与某些脚本一起部署到Linux服务器上时无法工作。所以我认为这不是代码的问题。但是出于某种原因。部署过程很难跟踪。 可能是什么问题呢? 问题答案: 我最好的选择是这里有一个问

  • 问题内容: 我是EJB的新手。我的经理告诉我在测试服务器中部署ejb应用程序进行一些修改。该应用程序已经在我们的生产服务器中运行。但是,当我部署应用程序时,我遇到了异常。 我们正在使用Glassfish应用服务器 堆栈异常跟踪: 当我重新启动服务器时,我得到了这些: 在这里您也可以看到相同的问题 任何人都可以 请 帮我,为什么这个错误即将到来。 这是非常需要的。 谢谢 问题答案: 问题解决了。 实

  • 问题内容: 我在此添加了web.xml的源代码 Hibernate.cfg.xml。我的Web服务项目与Jersey + Hibernate一起使用。 这是HibernateUtil类 这是我添加了Hibernate最新的jar文件的错误。[ hibernate-core-4.3.7.Final.jar ] 请帮我解决这个问题谢谢 问题答案: 初始化失败时可能会发生这种错误。您已经提到您正在使用。