Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java setText()不';t更改我的文本视图()_Java_Android_Bluetooth_Textview - Fatal编程技术网

Java setText()不';t更改我的文本视图()

Java setText()不';t更改我的文本视图(),java,android,bluetooth,textview,Java,Android,Bluetooth,Textview,我尝试显示从蓝牙设备接收到的文本。 我收到文本,我将其更改为字符串,我可以在Logcat中显示它,但它不会出现在我的文本视图中。。。 我尝试了不同的方法,比如在收到消息后放置setText(),但没有任何效果。。。有什么想法吗 我的职能: package com.example.blueconnect; import java.io.BufferedReader; import java.io.DataInputStream; import java.io.DataOutputStream;

我尝试显示从蓝牙设备接收到的文本。 我收到文本,我将其更改为字符串,我可以在Logcat中显示它,但它不会出现在我的文本视图中。。。 我尝试了不同的方法,比如在收到消息后放置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
始终不显示任何内容


编辑:您在一个
线程中
UI线程
不同,对UI元素的所有修改都应该在
UI线程
上完成。您必须使用
处理程序
runOnUiThread

文本中的lineread有错误查看您的write lineread

更改此代码:

   Log.i("MESSAGE", lineread);       
   tvPD.setText(lineRead);
作者:


我希望这能帮助你

你在哪里调用
run()
?这是谁的run()方法?你在哪里和什么时候设置lineRead?我发布了我的全部代码,也许它能帮助你。@underround 72正如我所怀疑的那样。无法从后台线程更新ui。您需要在ui线程上更新ui。@underround72 move tvPD.setText(lineRead);在Log.i之后(“消息”,lineRead)@奥德布格,你能重复一下吗?我应该搬什么,去哪里,为什么?@E.Odebugg这有什么区别?初始化后,您可以将text设置为textview。@Raghunandan他在评论中说,在try语句开始后,他从bluetooth获取读取值。因此,如果在值存储在lineRead上之后调用setText,他将在TextView上看到它。我说“在Log.I(“MESSAGE”,lineRead);”因为他说他看到了日志,所以我知道lineRead在这之后有值point@E.Odebugg代码片段位于一个无限循环中,而它的大写字母仅为
R
@黑带是对的。请参见此
字符串lineRead=“”位他记录的变量不存在。。。只有
lineRead
,没有
lineRead
,因此它甚至不应该编译,或者它是粘贴代码中的一个打字错误
   Log.i("MESSAGE", lineread);       
   tvPD.setText(lineread);