无法在内部类线程中设置文本到EditText或TextView。

14 浏览
0 Comments

无法在内部类线程中设置文本到EditText或TextView。

这个问题已经有了答案

Android \"Only the original thread that created a view hierarchy can touch its views.\"

    package com.example.multicast;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetAddress;
    import java.net.MulticastSocket;
    import javax.security.auth.PrivateCredentialPermission;
    import android.os.Bundle;
    import android.app.Activity;
    import android.util.Log;
    import android.view.View;
    import android.widget.EditText;
    import android.widget.ProgressBar;
    import android.widget.TextView;
    import android.widget.Toast;
    public class MainActivity extends Activity {
         private EditText txt;
         private ProgressBar bar;
         @Override
             public void onCreate(Bundle savedInstanceState) {
                 super.onCreate(savedInstanceState);
                 setContentView(R.layout.activity_main);
                 txt=(EditText)findViewById(R.id.editText1);
                 bar = (ProgressBar) findViewById(R.id.progressBar1);
             }
             public void startProgress(View view) {
                 new Thread(new server()).start();
                 new Thread(new client()).start();
             }
             class client implements Runnable {
                 @Override
                 public void run() {
                    try{
                        Log.v("ppp","try");
                        MulticastSocket socket = new MulticastSocket(9999);
                        Log.v("ppp","aftrsokt");
                        InetAddress group = InetAddress.getByName("224.0.0.1");
                        Log.v("ppp","b4join");
                        socket.joinGroup(group);
                        Log.v("ppp","join");
                        DatagramPacket packet;
                        byte[] buf = new byte[1024];
                        String received= "";
                        packet = new DatagramPacket(buf, buf.length);
                        Log.v("ppp", "creatpacket");
                        socket.receive(packet);
                        Log.v("ppp", "rcvpackt");
                        received = new String(packet.getData());
                        Log.v("ppp", "rcvd"+received);
                        received =new String(buf,0,packet.getLength());
                        Log.v("ppp","rcvd"+received);
                        txt.setText("ppp");              ///ERROR
                        Log.v("ppp", "aftr txt.settext");
                        bar.setProgress(5);
                        System.out.println("Address: " + received);
                        Toast.makeText(MainActivity.this,"OnClickListener : " + 
                                            " "+received,
                        Toast.LENGTH_LONG).show();
                        socket.leaveGroup(group);
                        socket.close();
                        }
                    catch(Exception e){
                        Log.v("ppp","try"+e);
                        }
                    }
                }
             class server implements Runnable {
                byte [] buffer=new byte[1024];
                DatagramPacket packet;
                @Override
                public void run() {
                    try{
                        DatagramSocket socket = new DatagramSocket();
                        String ip="haaii";
                        Log.v("ppp","hi");
                        buffer = ip.getBytes();
                        packet = new DatagramPacket(buffer, buffer.length, InetAddress.getByName("224.0.0.1"),9999);
                        socket.send(packet);
                        socket.close();
                        Log.v("ppp","socketclose");
                        }
                        catch(Exception e){
                        }
                }
             }
         }

这里显示错误

android.view.ViewRoot$CalledFromWrongThreadException: 
Only the original thread that created a view hierarchy can touch its views.

我该怎么设置TextView的文本?

以下是xml代码

    
    
        
        
        

admin 更改状态以发布 2023年5月20日
0
0 Comments

runOnUiThread()方法内部调用txt.setText("ppp")方法。请注意要使用activity上下文调用runOnUiThread方法,因为你正在创建内部类,除非使用activity调用,否则runOnUiThread方法将不起作用。

0
0 Comments

只有创建视图层次结构的原始线程可以触摸其视图。

只有UI线程可以对UI进行更改。

您正在尝试从非UI线程修改UI。要在UI线程上执行操作,请使用runOnUiThread()方法。

                      try{
                        Log.v("ppp","try");
                        MulticastSocket socket = new MulticastSocket(9999);
                        Log.v("ppp","aftrsokt");
                        InetAddress group = InetAddress.getByName("224.0.0.1");
                        Log.v("ppp","b4join");
                        socket.joinGroup(group);
                        Log.v("ppp","join");
                        DatagramPacket packet;
                        byte[] buf = new byte[1024];
                        String received= "";
                        packet = new DatagramPacket(buf, buf.length);
                        Log.v("ppp", "creatpacket");
                        socket.receive(packet);
                        Log.v("ppp", "rcvpackt");
                        received = new String(packet.getData());
                        Log.v("ppp", "rcvd"+received);
                        received =new String(buf,0,packet.getLength());
                        Log.v("ppp","rcvd"+received);
                        runOnUiThread(new Runnable() {
                          @Override
                          public void run() {
                            txt.setText("ppp");
                            Log.v("ppp", "aftr txt.settext");
                            bar.setProgress(5);
                            System.out.println("Address: " + received);
                            Toast.makeText(MainActivity.this,"OnClickListener : " + " "+received,Toast.LENGTH_LONG).show();
                          }
                        });
                        socket.leaveGroup(group);
                        socket.close();
                      }
                      catch(Exception e){
                        Log.v("ppp","try"+e);
                      }

0