[英]setText() doesn't change my textView()
I try to show a text that I receive from a bluetooth device. 我尝试显示从蓝牙设备收到的文本。 I receive the text, I change it to a string, I can show it in Logcat, but it doesn't appear in my textView... I tried different ways like place the setText() just after receiving the message, but nothing works... Any idea ?
我收到了文本,将其更改为字符串,可以在Logcat中显示它,但它不会出现在我的textView中...我尝试了不同的方式,例如在收到消息后立即放置setText(),但是没有任何效果... 任何想法 ?
My Function: 我的职能:
package com.example.blueconnect;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.UUID;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class MainActivity extends Activity {
BluetoothAdapter blueAdapter = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
blueAdapter = BluetoothAdapter.getDefaultAdapter();
if (blueAdapter == null) {
Log.e("Error","The device has no Bluetooth.");
}
else{
if (!blueAdapter.isEnabled()) blueAdapter.enable();
}
//Do the Bluetooth visible
/*Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
startActivity(discoverableIntent);
*/
//Start the server
AcceptThread blueNect = new AcceptThread();
blueNect.start();
}
private class AcceptThread extends Thread {
// The local server socket
private BluetoothServerSocket mmServerSocket;
public AcceptThread() {
}
public void run() {
BluetoothSocket socket = null;
BluetoothAdapter mAdapter = BluetoothAdapter.getDefaultAdapter();
String lineRead = "";
// Listen to the server socket if we're not connected
while (true) {
try {
//récupération de l'élément
TextView tvPD = (TextView) findViewById(R.id.tvPD);
Log.i("MESSAGE",lineRead);
tvPD.setText(lineRead);
// Create a new listening server socket
Log.d("TAG", ".....Initializing RFCOMM SERVER....");
// MY_UUID is the UUID you want to use for communication
mmServerSocket = mAdapter.listenUsingRfcommWithServiceRecord("ServeurBluetooth", UUID.fromString("30fa7085-2927-4f90-8da7-b9a86c398373"));
//mmServerSocket = mAdapter.listenUsingInsecureRfcommWithServiceRecord(NAME, MY_UUID); you can also try using In Secure connection...
// This is a blocking call and will only return on a
// successful connection or an exception
socket = mmServerSocket.accept();
} catch (Exception e) { }
try {
Log.d("TAG", "Closing Server Socket.....");
mmServerSocket.close();
InputStream tmpIn = null;
OutputStream tmpOut = null;
// Get the BluetoothSocket input and output streams
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
DataInputStream mmInStream = new DataInputStream(tmpIn);
DataOutputStream mmOutStream = new DataOutputStream(tmpOut);
//Read message
BufferedReader bReader2=new BufferedReader(new InputStreamReader(mmInStream));
lineRead=bReader2.readLine();
Log.i("MESSAGE",lineRead);
//send response to spp client
PrintWriter pWriter=new PrintWriter(new OutputStreamWriter(mmOutStream));
pWriter.write("Response String from SPP Server\r\n");
pWriter.flush();
Log.d("TAG", "Respond sent to the CLient");
pWriter.close();
// here you can use the Input Stream to take the string from the client whoever is connecting
//similarly use the output stream to send the data to the client
} catch (Exception e) {
//catch your exception here
}
}
}
}
}
My xml: 我的xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
tools:context=".MainActivity" >
<include
android:id="@+id/header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
layout="@layout/header" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/header"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/tvPD"
style="@style/TitleText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Devices" />
</RelativeLayout>
</RelativeLayout>
Hope you could help me... 希望你能帮我...
tvPD.setText(lineRead);
lineRead
is initialized to ""
and never modified. lineRead
初始化为""
并且从未修改。 That's why your TextView
shows always nothing. 这就是为什么您的
TextView
始终不显示任何内容的原因。
Edit: you are inside a Thread
different from the UI Thread
and all the modifications to UI Elements should be done on the UI Thread
. 编辑:您所在的
Thread
不同于UI Thread
并且对UI元素的所有修改都应在UI Thread
。 you have to use an Handler
or runOnUiThread
您必须使用
Handler
或runOnUiThread
there is a mistake in lineread in your textview your write lineRead 您的textview中的lineread有一个错误查看您的write lineRead
change this code: 更改此代码:
Log.i("MESSAGE", lineread);
tvPD.setText(lineRead);
by: 通过:
Log.i("MESSAGE", lineread);
tvPD.setText(lineread);
i hope that helps you 希望对您有帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.