简体   繁体   English

Android动画无法点击

[英]Android animation not working on click

My animation I'snt working on click, here is my code: 我不在点击动画,这是我的代码:

Edit: can now click the button, but nothing happens on click 编辑:现在可以单击按钮,但是单击时什么也没有发生

animation: bottom_up.xml: 动画: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"/>

here is my java MusicPlayerActivity.java: 这是我的Java MusicPlayerActivity.java:

    btnPlayList.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Animation bottomUp = AnimationUtils.loadAnimation(MusicPlayerActivity.this, R.anim.bottom_up);
            ViewGroup hiddenPanel = (ViewGroup) v.findViewById(R.id.hidden_panel);
            hiddenPanel.startAnimation(bottomUp);
            hiddenPanel.setVisibility(View.VISIBLE);

        }
    });
} 

Thank you! 谢谢!

check if you initialed btnPlayList like this btnPlayList=(Button)findViewById(R.id.btnPlayList); 检查您是否像这样btnPlayList =(Button)findViewById(R.id.btnPlayList)初始化了btnPlayList;

or hiddenPanel not exist in this view that you click on; 或hiddenPanel在您单击的此视图中不存在;

The error you are getting is a very common one. 您遇到的错误是非常常见的错误。 It's called NullPointerException because you try to call a method of an object whose reference is null, so the system can't resolve the method. 之所以称为NullPointerException,是因为您尝试调用其引用为null的对象的方法,因此系统无法解析该方法。 You cand read more about this exception in this link . 您可以在此链接中阅读有关此异常的更多信息。

The line that is probably causing this exception is: 可能导致此异常的行是:

ViewGroup hiddenPanel = (ViewGroup) v.findViewById(R.id.hidden_panel);

In order to fix it, I would suggest two things: 为了解决它,我建议两件事:

  • Check that the id R.id.hidden_panel is correct. 检查ID R.id.hidden_panel是否正确。
  • If you are contemplating that R.id.hidden_panel might not be in your layout, wrap its usage with a null check: 如果您正在考虑R.id.hidden_panel可能不在您的布局中,请使用null检查将其用法包装起来:

     if (hiddenPanel != null) { hiddenPanel.startAnimation(bottomUp); hiddenPanel.setVisibility(View.VISIBLE); } 

EDIT: 编辑:

To fix the animation not showing, try calling startAnimation after you set the hiddenPanel to Visible, and not before. 若要修复动画不显示的情况,请在将hiddenPanel设置为Visible之后而不是在此之前尝试调用startAnimation

java.lang.NullPointerException -> some variables not initialized. java.lang.NullPointerException->一些未初始化的变量。 check all variables 检查所有变量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM