[英]Android: SetText in handler doesn't change text in TextView
[英]setText() doesn't change my textView()
我嘗試顯示從藍牙設備收到的文本。 我收到了文本,將其更改為字符串,可以在Logcat中顯示它,但它不會出現在我的textView中...我嘗試了不同的方式,例如在收到消息后立即放置setText(),但是沒有任何效果... 任何想法 ?
我的職能:
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
}
}
}
}
}
我的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>
希望你能幫我...
tvPD.setText(lineRead);
lineRead
初始化為""
並且從未修改。 這就是為什么您的TextView
始終不顯示任何內容的原因。
編輯:您所在的Thread
不同於UI Thread
並且對UI元素的所有修改都應在UI Thread
。 您必須使用Handler
或runOnUiThread
您的textview中的lineread有一個錯誤查看您的write lineRead
更改此代碼:
Log.i("MESSAGE", lineread);
tvPD.setText(lineRead);
通過:
Log.i("MESSAGE", lineread);
tvPD.setText(lineread);
希望對您有幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.