事は簡単です、アプリにニュースフィードを持たせたいのですが、テキストスイッチャーを介してそれをやろうとしていますが、アプリがクラッシュします
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)