【问题标题】:Android animate text when calling setText()调用 setText() 时的 Android 动画文本
【发布时间】:2014-04-23 12:14:07
【问题描述】:

我想在更改时动画文本淡入和淡出我正在扩展 TextSwitcher 以便定义一次,因为我有很多 TextSwitcher 对象。

但是我不断得到一个空指针“java.lang.NullPointerException android.widget.TextSwitcher.setText(TextSwitcher.java:80) 这样做时:

homeTeamTextView.setText("Text");

我决定像这样扩展 TextSwitcher 类:

public class MyTextSwitcher extends TextSwitcher{

public MyTextSwitcher(Context context, AttributeSet attrs)
{
    super(context, attrs);
    // TODO Auto-generated constructor stub

    setInAnimation(context,R.anim.fade_in);
    setOutAnimation(context,R.anim.fade_out);
}}

活动

private MyTextSwitcher homeTeamTextView;

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

    homeTeamTextView = (MyTextSwitcher) findViewById(R.id.hometeam);
    awayScoreTextView = (TextView) findViewById(R.id.awayteamscore);
    homeScoreTextView = (TextView) findViewById(R.id.hometeamscore);
    gameStatusTextView = (TextView) findViewById(R.id.gamestatus);
    gameClockTextView = (TextView) findViewById(R.id.gameclock);
    gameStartTextView = (TextView) findViewById(R.id.gamestart);
    gameClockSectionTextView = (TextView) findViewById(R.id.gameclocksection);

    LoadDataAsyncTask loadDataTask = new LoadDataAsyncTask();
    loadDataTask.execute();
}

布局:

  <com.MyTextSwitcher
        android:id="@+id/hometeam"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />

【问题讨论】:

  • 您将动画文件淡入淡出保存在哪里??
  • 我认为问题出在 setInAnimation(context,R.anim.fade_in); setOutAnimation(context,R.anim.fade_out);而是写 android.R.anim.fade_in,android.R.fade_out 作为第二个参数

标签: android android-animation textview android-textattributes


【解决方案1】:

看起来TextSwitcher是普通的ViewSwitcher,只能包含TextViews。当您调用 setText() 时,它会尝试获取下一个视图。你可以在源代码中找到它:https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/widget/TextSwitcher.java,但它是空的。我认为您必须在 TextSwitcher 中添加 textView(甚至两个 textView 才能正常工作)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-10
    • 1970-01-01
    • 1970-01-01
    • 2016-01-14
    相关资源
    最近更新 更多