java - 动画可绘制 - 空对象引用

标签 java android android-studio

我的动画绘制遇到问题。它告诉我它指向一个空对象引用,但我很确定它不是。我该如何解决这个问题?

此时任何帮助都会非常有帮助!谢谢!

public class PlayingScreen extends AppCompatActivity implements View.OnClickListener {

int notLike_Liked = 1;
int Pause_Play = 1;
SeekBar seekBar;
ImageView back,pause,next,notLikes;
TextView text_fixed,text_plus;
ConstraintLayout playing_screen;
AnimationDrawable animationDrawable;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_playing_screen);

    playing_screen = findViewById(R.id.playing_screen);
    notLikes = findViewById(R.id.notLikes);
    text_fixed = findViewById(R.id.text_fixed);
    text_plus = findViewById(R.id.text_plus);
    seekBar = findViewById(R.id.seekBar);
    back = findViewById(R.id.back);
    pause = findViewById(R.id.pause);
    next = findViewById(R.id.next);

    //setting of click listeners ofr buttons
    notLikes.setOnClickListener(this);
    seekBar.setOnClickListener(this);
    back.setOnClickListener(this);
    pause.setOnClickListener(this);
    next.setOnClickListener(this);

    StateListDrawable background = (StateListDrawable) playing_screen.getBackground();
    Drawable current = background.getCurrent();
    if(current instanceof AnimationDrawable){
        animationDrawable =(AnimationDrawable) current;
        animationDrawable.setEnterFadeDuration(5000);
        animationDrawable.setExitFadeDuration(5000);
        animationDrawable.start();
    }

}

@Override
protected void onResume() {
    super.onResume();
    if(animationDrawable != null && animationDrawable.isRunning()){
        animationDrawable.start();

    }
}

记录猫:

Attempt to invoke virtual method 'android.graphics.drawable.Drawable android.graphics.drawable.StateListDrawable.getCurrent()' on a null object reference at com.musicapp.android.musicapp.PlayingScreen.onCreate(PlayingScreen.java:46) –

XML;

<androidx.constraintlayout.widget.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".PlayingScreen"
android:paddingBottom="25dp"
android:id="@+id/playing_screen">

我的动画绘制遇到问题。它告诉我它指向一个空对象引用,但我很确定它不是。我该如何解决这个问题?

此时任何帮助都会非常有帮助!谢谢!

最佳答案

尝试下面的代码

 Drawable current=playing_screen.getBackground();
 if (current instanceof AnimationDrawable) {
  animationDrawable=current;
  animationDrawable.setEnterFadeDuration(4000);
  animationDrawable.setExitFadeDuration(4000);
  animationDrawable.start();
  }
}

关于java - 动画可绘制 - 空对象引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60779755/

相关文章:

java - 如何使用 Java7 try-with-resources 特性将资源作为参数传递

java - 带有 Flex 的 IntelliJ Idea Java 项目,如何嵌入生成的 SWF

java - 如何在Android中显示区域字符

java - 非空对象上的空指针取消引用异常

android - 在 Android Studio 中显示文本有什么问题?

java - 我无法向 android studio 中的 color.xml 文件添加颜色

java - 通过 Java 建立 MySQL 表时出错

android - 如何在应用程序内单击按钮打开网页?

android - Flutter简单后台定时器示例

android - 签名 APK 向导到 keystore 的相对路径