【问题标题】:Animation Drawable - null object reference动画 Drawable - 空对象引用
【发布时间】:2020-03-21 03:57:09
【问题描述】:

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

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

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

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

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

【问题讨论】:

  • stackoverflow.com/a/5300955/2754562。可绘制电流 = background.getCurrent();
  • 谢谢!这很有帮助。但现在我遇到了这个错误:尝试在 com.musicapp.android.musicapp.PlayingScreen.onCreate 的空对象引用上调用虚拟方法 'android.graphics.drawable.Drawable android.graphics.drawable.StateListDrawable.getCurrent()' (PlayingScreen.java:46)
  • 编辑回滚以恢复问题的相关代码,因为问题文本不再与错误消息匹配

标签: java android android-studio


【解决方案1】:

试试下面的代码

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

【讨论】:

  • 谢谢!这很有帮助。但现在我遇到了这个错误:尝试在 com.musicapp.android.musicapp.PlayingScreen.onCreate 的空对象引用上调用虚拟方法 'android.graphics.drawable.Drawable android.graphics.drawable.StateListDrawable.getCurrent()' (PlayingScreen.java:46)
  • 你也可以添加xml
  • 它不让我
  • 我添加了少量的 XML。我正在尝试更改该 Constrail 布局的背景颜色(它的 id 是正在播放屏幕)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多