我想在更改时动画文本淡入和淡出我正在扩展TextSwitcher
以便定义一次,因为我有很多TextSwitcher
对象。
但是,在执行此操作时,我不断获得 Null 指针“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"
/>