【问题标题】:ProgressBar giving me NullPointerException on AnimationListenerProgressBar 在 AnimationListener 上给我 NullPointerException
【发布时间】:2014-02-02 08:42:23
【问题描述】:

我正在做一个项目,我在 Intent 中添加了 ProgressBarProgressBar 位于图像之上。

每当我添加以下代码时

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


        pb = (ProgressBar)findViewById(R.id.progressBar1);


        pb.getAnimation().setAnimationListener(new AnimationListener(){

            @Override
            public void onAnimationEnd(Animation animation) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onAnimationRepeat(Animation animation) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onAnimationStart(Animation animation) {
                // TODO Auto-generated method stub

            }

        });

        Intent intent = new Intent(Greet.this, SearchField.class);
        startActivity(intent);

    }

我收到了nullPointerException

当我删除

pb.getAnimation().setAnimationListener(new AnimationListener(){

            @Override
            public void onAnimationEnd(Animation animation) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onAnimationRepeat(Animation animation) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onAnimationStart(Animation animation) {
                // TODO Auto-generated method stub

            }

        });

错误消失了。

我无法弄清楚为什么我会收到 NullPointerException,查看代码,一切似乎都应该是这样。

我觉得我要在这里学习一些新东西,因为我清理了项目,尝试了它,但无论如何都会出现错误......所以我不确定我错过了什么。

编辑:

XML 中的 ActivityGreet

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    tools:context=".Greet" >

    <ImageView
        android:id="@+id/fitnessPic"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:contentDescription="@string/description"
        android:src="@drawable/appleorange"/>

    <ProgressBar
        android:id="@+id/progressBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="64dp" />

</RelativeLayout>

在图像上放置 ProgressBar 是否存在已知问题?

【问题讨论】:

  • 底部的建议均无效。

标签: java android animation nullpointerexception progress-bar


【解决方案1】:
  1. 确保您的 activity_greet 布局中存在 ID 为 progressBar1ProgressBar
  2. 确保您正在导入项目的 R 类,而不是 android.R,因为 android.R 可能还包含 R.id.progressBar1

【讨论】:

    【解决方案2】:

    我认为您没有在 /res/anim 文件夹中定义动画。像这样更改您的代码:

    ProgressBar pb = (ProgressBar) findViewById(R.id.progressbar1);
    Animation progressAnimation= AnimationUtils.loadAnimation(this, R.anim.progressAnim);
    pb.startAnimation(progressAnimation);
    

    并单独实现您的动画侦听器,而不是使其匿名。

    更多详情请参阅Android Docs

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多