android.widget.TextView.setText中的System.err(TextView.java:2676)

时间:2014-07-17 03:40:03

标签: android android-edittext stack

我在logcat中的System.err得到android.widget.TextView.setText(TextView.java:2676)。这是我的代码:

package com.example.client;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.mylibrary.client.*;

public class MainActivity extends Activity{

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    final EditText jmlsyn = (EditText) findViewById(R.id.jmlsyn);
    final EditText jmlack = (EditText) findViewById(R.id.jmlack);
    Button btnstart = (Button) findViewById(R.id.btnstart);
    Button btnstop = (Button) findViewById(R.id.btnstop);
    Button btncek = (Button) findViewById(R.id.btncek);

    final Connectionmanagerclient cmc = new Connectionmanagerclient();
    final Countermessage cont = new Countermessage();
    final Messagebuilder mb = new Messagebuilder(cmc,cont);


    cmc.setmReceivelistener(new Receivelistener() {

        @Override
        public void onReceivedatalistener(String arg0) {
            String[] head = arg0.split(":");
            if (head.length == 3) {
                for (int i = 0; i < Queuemanager.data.size(); i++) {
                    String[] header = Queuemanager.data.get(i).split(":");
                    if (header[0].equals(head[0])) {
                        cont.setJmlack(cont.getJmlack()+1);
                        cont.setJmlsyn(cont.getJmlsyn()-1);
                        Log.i("as", "sini");
                        Queuemanager.data.remove(i);
                        jmlack.setText(""+cont.getJmlack());
                        jmlsyn.setText(""+cont.getJmlsyn());

                    }
                }
            }
        }
    });



    btnstart.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            cmc.start();

            try {
                Thread.sleep(1000);
            } catch (Exception e) {
                e.printStackTrace();
            }

            mb.start();
        }
    });

    btnstop.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            cmc.stop();
        }
    });


    btncek.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
        }
    });


}

}

这是我的logcat

07-17 03:30:22.865: I/as(449): sini
07-17 03:30:22.865: W/System.err(449): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
07-17 03:30:22.875: W/System.err(449):  at android.view.ViewRoot.checkThread(ViewRoot.java:2802)
07-17 03:30:22.875: W/System.err(449):  at android.view.ViewRoot.invalidateChild(ViewRoot.java:607)
07-17 03:30:22.875: W/System.err(449):  at android.view.ViewRoot.invalidateChildInParent(ViewRoot.java:633)
07-17 03:30:22.885: W/System.err(449):  at android.view.ViewGroup.invalidateChild(ViewGroup.java:2505)
07-17 03:30:22.895: W/System.err(449):  at android.view.View.invalidate(View.java:5115)
07-17 03:30:22.895: W/System.err(449):  at android.widget.TextView.invalidateCursor(TextView.java:3625)
07-17 03:30:22.895: W/System.err(449):  at android.widget.TextView.spanChange(TextView.java:6221)
07-17 03:30:22.905: W/System.err(449):  at android.widget.TextView$ChangeWatcher.onSpanAdded(TextView.java:6346)
07-17 03:30:22.905: W/System.err(449):  at android.text.SpannableStringBuilder.sendSpanAdded(SpannableStringBuilder.java:906)
07-17 03:30:22.915: W/System.err(449):  at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:611)
07-17 03:30:22.915: W/System.err(449):  at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:514)
07-17 03:30:22.925: W/System.err(449):  at android.text.Selection.setSelection(Selection.java:74)
07-17 03:30:22.925: W/System.err(449):  at android.text.Selection.setSelection(Selection.java:85)
07-17 03:30:22.935: W/System.err(449):  at android.text.method.ArrowKeyMovementMethod.initialize(ArrowKeyMovementMethod.java:497)
07-17 03:30:22.935: W/System.err(449):  at android.widget.TextView.setText(TextView.java:2676)
07-17 03:30:22.935: W/System.err(449):  at android.widget.TextView.setText(TextView.java:2556)
07-17 03:30:22.935: W/System.err(449):  at android.widget.EditText.setText(EditText.java:75)
07-17 03:30:22.945: W/System.err(449):  at android.widget.TextView.setText(TextView.java:2531)
07-17 03:30:22.945: W/System.err(449):  at com.example.client.MainActivity$1.onReceivedatalistener(MainActivity.java:42)
07-17 03:30:22.955: W/System.err(449):  at com.mylibrary.client.Connectionmanagerclient.readline(Connectionmanagerclient.java:107)
07-17 03:30:22.955: W/System.err(449):  at com.mylibrary.client.Connectionmanagerclient$1.run(Connectionmanagerclient.java:56)
07-17 03:30:22.955: W/System.err(449):  at java.lang.Thread.run(Thread.java:1096)

我想在jmlsyn开启时自动更新jmlacksetMreceivelistener。任何人都可以帮我自动更新EditText吗?

(抱歉我的英语不好)

1 个答案:

答案 0 :(得分:0)

来自你的logcat:

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

表示您正在尝试更新非UI线程上的UI组件。

尝试将EditText与setText()相关的代码放在其中:

runOnUiThread(new Runnable() {
     @Override
     public void run() {

      //try to update the EditText here 

    }
});