Reputation: 2713
I´m trying show text on a EditText, but no show anythigs, the code is this:
public void run()
{
String mensaje;
while(true)
{
try
{
mensaje = b.readLine();
Log.d("mensaje", mensaje);
_et_ip.setText(mensaje);
}
catch (IOException e){e.printStackTrace();}
}
}
The main class implements runnable
public class ClienteTcpAnroid extends Activity implements Runnable
and Log.d show the message correct but _et_ip.setText(mensaje) no show the message.
I´m using android emulator
Thanks you very much
This is all code:
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();
}
}
}
}
Thanks you very much
This is log:
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
Thanks kevin
Upvotes: 0
Views: 677
Reputation: 17302
OK. Issue #1, you're eating your Exceptions. I'm pretty sure (although maybe its changed?) e.printStackTrace() does nothing. You either need to log them with Log, or wrap and throw with RuntimeException (I prefer to fail than quietly ignore, so I chose the latter).
You have 2 ways of doing this (well, several, but we'll discuss 2).
1) Do the background stuff in an AsyncTask. I'd pick that. Much easier than creating/managing your own thread.
2) Keep your code, but when you update the UI, do it with a Handler that was created in the UI thread.
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);
}
}
}
}
I assume the socket read is blocking. You have an "infinite loop", of course, but that's not a big deal since it blocks. The only thing I'd suggest is figuring how to cut that off in onPause.
Also BTW, forget AsyncTask here. Didn't realize the exact thing you were trying to do till just now. Thread is the way to go. Well, I think a Service is the way to go, but this will work.
Upvotes: 1