EditText没有显示文本

时间:2011-11-03 19:24:13

标签: android user-interface

我在EditText上尝试显示文本,但没有显示任何问题,代码是这样的:

public void run() 
{
    String mensaje;
    while(true)
    {
      try 
      {
        mensaje = b.readLine();
        Log.d("mensaje", mensaje);
       _et_ip.setText(mensaje);
      } 
      catch (IOException e){e.printStackTrace();}
   }
 }

主类实现了runnable

public class ClienteTcpAnroid extends Activity implements Runnable

和Log.d显示消息正确,但_et_ip.setText(mensaje)没有显示消息。

我正在使用android模拟器

非常感谢你

这是所有代码:

public class ClienteTcpAnroid extends Activity implements Runnable
{

private static  String myIP;
private static String myPort;
String mensaje;
Thread threadProcesaAlarmas;
public TextView _tv_estado;
    public EditText _et_ip;
    public EditText _et_port;
    public Button botonConectar;
    BufferedReader b;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        _tv_estado = (TextView)findViewById(R.id.tv_estado);
        _et_ip = (EditText)findViewById(R.id.et_ip);
        _et_port = (EditText)findViewById(R.id.et_port);
        botonConectar = (Button)findViewById(R.id.button1);

        botonConectar.setOnClickListener(new View.OnClickListener() 
        {            
         public void onClick(View v)
             { 
                Socket mySocket;
            PrintStream p;
            try 
            {
                mySocket = new Socket("192.168.1.35", 12345);
    b = new BufferedReader(new InputStreamReader(mySocket.getInputStream()));
                threadProcesaAlarmas = new Thread(ClienteTcpAnroid.this);
                threadProcesaAlarmas.start();
            } 
            catch (IOException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }); 
}

@Override
public void run() 
{
    String mensaje;
    while(true)
    {
        try 
        {
            mensaje = b.readLine();
            Log.d("mensaje", mensaje);
            _et_ip.setText(mensaje);
        } 
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}
}

非常感谢你

这是日志:

11-03 20:01:08.507: D/mensaje(957): mensaje0  
11-03 20:01:08.507: W/dalvikvm(957): threadid=9: thread exiting with uncaught exception (group=0x40015560)
11-03 20:01:08.536: E/AndroidRuntime(957): FATAL EXCEPTION: Thread-10
11-03 20:01:08.536: E/AndroidRuntime(957):   android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
11-03 20:01:08.536: E/AndroidRuntime(957):  at android.view.ViewRoot.checkThread(ViewRoot.java:2932)
11-03 20:01:08.536: E/AndroidRuntime(957):  at android.view.ViewRoot.invalidateChild(ViewRoot.java:642)
11-03 20:01:08.536: E/AndroidRuntime(957):  at android.view.ViewRoot.invalidateChildInParent(ViewRoot.java:668)
11-03 20:01:08.536: E/AndroidRuntime(957):  at android.view.ViewGroup.invalidateChild(ViewGroup.java:2511)
11-03 20:01:08.536: E/AndroidRuntime(957):  at android.view.View.invalidate(View.java:5255)
11-03 20:01:08.536: E/AndroidRuntime(957):  at android.widget.TextView.invalidateCursor(TextView.java:3683)
11-03 20:01:08.536: E/AndroidRuntime(957):  at android.widget.TextView.spanChange(TextView.java:6390)
11-03 20:01:08.536: E/AndroidRuntime(957):  at android.widget.TextView$ChangeWatcher.onSpanAdded(TextView.java:6515)
11-03 20:01:08.536: E/AndroidRuntime(957):  at android.text.SpannableStringBuilder.sendSpanAdded(SpannableStringBuilder.java:906)
11-03 20:01:08.536: E/AndroidRuntime(957):  at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:611)
11-03 20:01:08.536: E/AndroidRuntime(957):  at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:514)
11-03 20:01:08.536: E/AndroidRuntime(957):  at android.text.Selection.setSelection(Selection.java:74)
11-03 20:01:08.536: E/AndroidRuntime(957):  at android.text.Selection.setSelection(Selection.java:85)
11-03 20:01:08.536: E/AndroidRuntime(957):  at android.text.method.ArrowKeyMovementMethod.initialize(ArrowKeyMovementMethod.java:268)
11-03 20:01:08.536: E/AndroidRuntime(957):  at android.widget.TextView.setText(TextView.java:2712)
11-03 20:01:08.536: E/AndroidRuntime(957):  at android.widget.TextView.setText(TextView.java:2592)
11-03 20:01:08.536: E/AndroidRuntime(957):  at android.widget.EditText.setText(EditText.java:78)
11-03 20:01:08.536: E/AndroidRuntime(957):  at android.widget.TextView.setText(TextView.java:2567)
11-03 20:01:08.536: E/AndroidRuntime(957):  at packag.Android.ClienteTcpAnroid.run(ClienteTcpAnroid.java:80)
11-03 20:01:08.536: E/AndroidRuntime(957):  at java.lang.Thread.run(Thread.java:1019)
11-03 20:01:08.566: W/ActivityManager(77):   Force finishing activity packag.Android/.ClienteTcpAnroid
11-03 20:01:08.767: W/IInputConnectionWrapper(957): showStatusIcon on inactive InputConnection

谢谢凯文

1 个答案:

答案 0 :(得分:1)

行。问题#1,你正在吃你的例外。我很确定(虽然它可能会改变吗?)e.printStackTrace()什么都不做。您需要使用Log记录它们,或者使用RuntimeException进行换行和抛出(我更喜欢失败而不是静静地忽略,所以我选择了后者)。

你有2种方法可以做到这一点(好吧,有几种,但我们将讨论2)。

1)在AsyncTask中执行背景内容。我会选择那个。比创建/管理自己的线程容易得多。

2)保留您的代码,但是当您更新UI时,请使用在UI线程中创建的处理程序来执行此操作。

public class ClienteTcpAnroid extends Activity implements Runnable
{

    private static String myIP;
    private static String myPort;
    String mensaje;
    Thread threadProcesaAlarmas;
    public TextView _tv_estado;
    public EditText _et_ip;
    public EditText _et_port;
    public Button botonConectar;
    BufferedReader b;
    private Handler handler;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        _tv_estado = (TextView) findViewById(R.id.tv_estado);
        _et_ip = (EditText) findViewById(R.id.et_ip);
        _et_port = (EditText) findViewById(R.id.et_port);
        botonConectar = (Button) findViewById(R.id.button1);

        botonConectar.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
            {
                Socket mySocket;
                PrintStream p;
                try
                {
                    mySocket = new Socket("192.168.1.35", 12345);
                    b = new BufferedReader(new InputStreamReader(mySocket.getInputStream()));
                    threadProcesaAlarmas = new Thread(ClienteTcpAnroid.this);
                    threadProcesaAlarmas.start();
                }
                catch (IOException e)
                {
                    throw new RuntimeException(e);
                }
            }
        });

        handler = new Handler();
    }

    @Override
    public void run()
    {
        while (true)
        {
            try
            {
                final String mensaje = b.readLine();
                Log.d("mensaje", mensaje);
                handler.post(new Runnable()
                {
                    public void run()
                    {
                        _et_ip.setText(mensaje);
                    }
                });
            }
            catch (IOException e)
            {
                throw new RuntimeException(e);
            }

        }
    }
}

我认为套接字读取是阻塞的。当然,你有一个“无限循环”,但这不是一个大问题,因为它阻止了。我唯一建议的是在onPause中找出如何切断它。

同样顺便说一下,忘了AsyncTask。直到现在才意识到你要做的确切事情。线程是要走的路。好吧,我认为服务是可行的方法,但这样可行。