public class HomeFragment extends SherlockFragment implements ViewFactory{
List<String> newsList = new ArrayList<String>();
TextSwitcher tw;
int position = 0;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.home_fragment, null, false);
//TextView news = (TextView)v.findViewById(R.id.app_bottom_news);
//news.setSelected(true);
newsList.add("String 1 String 1 String 1 String 1 String 1");
newsList.add("String 2 String 2 String 2 String 2 String 2");
newsList.add("String 3 String 3 String 3 String 3 String 3");
newsList.add("String 4 String 4 String 4 String 4 String 4");
newsList.add("String 5 String 5 String 5 String 5 String 5");
newsList.add("String 6 String 6 String 6 String 6 String 6");
tw = (TextSwitcher)v.findViewById(R.id.app_bottom_news);
Animation in = AnimationUtils.makeInAnimation(getActivity(), true);
Animation out = AnimationUtils.makeOutAnimation(getActivity(), true);
tw.setFactory(this);
tw.setInAnimation(in);
tw.setOutAnimation(out);
tw.setText(String.valueOf(newsList.get(position)));
TimerTask tm = new TimerTask(){
@Override
public void run() {
// TODO Auto-generated method stub
if(position < newsList.size()){
position++;
tw.setText(String.valueOf(newsList.get(position)));
}else{
position = 0;
}
}
};
Timer t = new Timer();
t.schedule(tm, 2000);
}
@Override
public View makeView() {
// TODO Auto-generated method stub
TextView txtView = new TextView(getActivity());
txtView.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
txtView.setTextSize(20);
return txtView;
}
}
并且Log Cat输出为
12-20 16:44:16.882: W/dalvikvm(1838): threadid=11: thread exiting with uncaught exception (group=0x40a13300)
12-20 16:44:16.937: E/AndroidRuntime(1838): FATAL EXCEPTION: Timer-1
12-20 16:44:16.937: E/AndroidRuntime(1838): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
12-20 16:44:16.937: E/AndroidRuntime(1838): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4609)
12-20 16:44:16.937: E/AndroidRuntime(1838): at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:835)
12-20 16:44:16.937: E/AndroidRuntime(1838): at android.view.View.requestLayout(View.java:15129)
12-20 16:44:16.937: E/AndroidRuntime(1838): at android.view.View.requestLayout(View.java:15129)
12-20 16:44:16.937: E/AndroidRuntime(1838): at android.view.View.requestLayout(View.java:15129)
12-20 16:44:16.937: E/AndroidRuntime(1838): at android.view.View.requestLayout(View.java:15129)
12-20 16:44:16.937: E/AndroidRuntime(1838): at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:292)
12-20 16:44:16.937: E/AndroidRuntime(1838): at android.view.View.requestLayout(View.java:15129)
12-20 16:44:16.937: E/AndroidRuntime(1838): at android.view.View.requestLayout(View.java:15129)
12-20 16:44:16.937: E/AndroidRuntime(1838): at android.view.View.requestLayout(View.java:15129)
12-20 16:44:16.937: E/AndroidRuntime(1838): at android.view.View.requestLayout(View.java:15129)
12-20 16:44:16.937: E/AndroidRuntime(1838): at android.view.View.requestLayout(View.java:15129)
12-20 16:44:16.937: E/AndroidRuntime(1838): at android.view.View.requestLayout(View.java:15129)
12-20 16:44:16.937: E/AndroidRuntime(1838): at android.view.View.requestLayout(View.java:15129)
12-20 16:44:16.937: E/AndroidRuntime(1838): at android.widget.TextView.checkForRelayout(TextView.java:6309)
12-20 16:44:16.937: E/AndroidRuntime(1838): at android.widget.TextView.setText(TextView.java:3547)
12-20 16:44:16.937: E/AndroidRuntime(1838): at android.widget.TextView.setText(TextView.java:3405)
12-20 16:44:16.937: E/AndroidRuntime(1838): at android.widget.TextView.setText(TextView.java:3380)
12-20 16:44:16.937: E/AndroidRuntime(1838): at android.widget.TextSwitcher.setText(TextSwitcher.java:80)
12-20 16:44:16.937: E/AndroidRuntime(1838): at net.evolution.betting.application.HomeFragment$1.run(HomeFragment.java:59)
12-20 16:44:16.937: E/AndroidRuntime(1838): at java.util.Timer$TimerImpl.run(Timer.java:284)
答案 0 :(得分:2)
每个View都有一个现有的Handler,你不需要在新的Thread上使用TimerTask。试试这个:
pageTurner = new Runnable() {
@Override
public void run() {
if(position < newsList.size()){
position++;
tw.setText(String.valueOf(newsList.get(position)));
}else{
position = 0;
}
// This line will run the timer again if you want
//tw.postDelayed(this, 2000);
}
};
tw.postDelayed(pageTurner, 2000);
您应该使用以下内容取消onPause()
中的所有待处理的Runnable:
tw.removeCallback(pageTurner);
答案 1 :(得分:1)
将setText
来电换入runOnUiThread
,您需要参加活动:
Activity a = getActivity();
if (a != null) {
a.runOnUiThread(new Runnable() { ... } );
}