0

事情很简单,我希望我的应用程序有新闻提要,我试图通过 textswitcher 做到这一点,但它的应用程序崩溃了

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)
4

2 回答 2

2

每个视图都有一个现有的处理程序,您不需要在新线程上使用 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);

您应该取消任何待处理的 Runnables onPause()

tw.removeCallback(pageTurner);
于 2012-12-20T17:50:53.993 回答
1

setText将调用包装在 a 中runOnUiThread,但您需要获取活动:

Activity a = getActivity();
if (a != null) {
  a.runOnUiThread(new Runnable() { ... } );
}
于 2012-12-20T17:45:32.913 回答