0

在我的应用程序中,我有一个带按钮的适配器。在这个按钮的 onclicklistener 中,我必须启动一个修改 UI 的线程,但我收到一个错误。代码是:

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)

线程的 run 方法中的代码没问题,并且在 onClick 方法中运行良好。如何更改我的代码?谢谢

4

2 回答 2

0

您无法从 UI 线程更新 UI,这就是您看到错误消息的原因

android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

如果您需要在更新 UI 之前执行长时间运行的进程或其他操作,您需要使用 aHandler或使用方法回调主线程runOnUiThread

于 2013-07-17T14:13:41.687 回答
0

你可以使用 runOnUiThread

runOnUiThread(new Runnable() {
            public void run() {
                //some jobe
            }
        });

如果您想在完成一些复杂的工作后更新 UI,请使用 AsyncTask 并更新 UI onPostExecute

于 2013-07-17T14:22:34.143 回答