в моем приложении у меня есть адаптер с кнопкой. В onclicklistener этой кнопки я должен запустить поток, который изменяет пользовательский интерфейс, но я получаю сообщение об ошибке. Код:
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.detailnews_layout, parent,
false);
holder.commentButton = (ImageView) convertView
.findViewById(R.id.imageButtonComment);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
final View commentView=convertView;
holder.commentButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (menuPosition == false) {
holder.commentsLayout.animate().translationY(-950)
.setDuration(2000);
Runnable commentsRun = new CommentThread(position, commentView, holder);
new Thread(commentsRun).start();
menuPosition = true;
} else {
holder.commentsLayout.animate().translationY(-0)
.setDuration(500);
menuPosition = false;
}
}
});
public class CommentThread implements Runnable {
private int position;
private View convertView;
private ViewHolder holder;
public CommentThread(int position, View convertView, ViewHolder holder) {
this.position = position;
this.convertView = convertView;
this.holder = holder;
}
public void run() {
...
}
}
Но у меня есть эта ошибка:
07-17 14:04:27.975: E/AndroidRuntime(950): FATAL EXCEPTION: Thread-97
07-17 14:04:27.975: E/AndroidRuntime(950): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
07-17 14:04:27.975: E/AndroidRuntime(950): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4746)
07-17 14:04:27.975: E/AndroidRuntime(950): at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:823)
07-17 14:04:27.975: E/AndroidRuntime(950): at android.view.View.requestLayout(View.java:15473)
07-17 14:04:27.975: E/AndroidRuntime(950): at android.view.View.requestLayout(View.java:15473)
07-17 14:04:27.975: E/AndroidRuntime(950): at android.view.View.requestLayout(View.java:15473)
07-17 14:04:27.975: E/AndroidRuntime(950): at android.view.View.requestLayout(View.java:15473)
07-17 14:04:27.975: E/AndroidRuntime(950): at android.view.View.requestLayout(View.java:15473)
07-17 14:04:27.975: E/AndroidRuntime(950): at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:318)
07-17 14:04:27.975: E/AndroidRuntime(950): at android.view.View.requestLayout(View.java:15473)
07-17 14:04:27.975: E/AndroidRuntime(950): at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:318)
07-17 14:04:27.975: E/AndroidRuntime(950): at android.view.View.requestLayout(View.java:15473)
07-17 14:04:27.975: E/AndroidRuntime(950): at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:318)
07-17 14:04:27.975: E/AndroidRuntime(950): at android.view.View.requestLayout(View.java:15473)
07-17 14:04:27.975: E/AndroidRuntime(950): at android.widget.ScrollView.requestLayout(ScrollView.java:1440)
07-17 14:04:27.975: E/AndroidRuntime(950): at android.view.View.requestLayout(View.java:15473)
07-17 14:04:27.975: E/AndroidRuntime(950): at android.view.ViewGroup.addView(ViewGroup.java:3208)
07-17 14:04:27.975: E/AndroidRuntime(950): at android.view.ViewGroup.addView(ViewGroup.java:3155)
07-17 14:04:27.975: E/AndroidRuntime(950): at com.app.FlipAdapter.addItem(FlipAdapter.java:447)
07-17 14:04:27.975: E/AndroidRuntime(950): at com.app.FlipAdapter.access$2(FlipAdapter.java:422)
07-17 14:04:27.975: E/AndroidRuntime(950): at com.app.FlipAdapter$CommentThread.run(FlipAdapter.java:551)
07-17 14:04:27.975: E/AndroidRuntime(950): at java.lang.Thread.run(Thread.java:856)
Код в методе запуска потока в порядке и хорошо работает из метода onClick. Как я могу изменить свой код? Спасибо