2
    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){

                        }
                }
             }
         }

this shows the error

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

What can I do to set the text to the TextView?

the xml is given below

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
        <ProgressBar
            android:id="@+id/progressBar1"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:indeterminate="false"
            android:max="10"
            android:padding="4dip" >
        </ProgressBar>
        <Button
            android:id="@+id/button1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="startProgress"
            android:text="Start" />

        <EditText
            android:id="@+id/editText1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />

    </LinearLayout>
codeMagic
  • 44,549
  • 13
  • 77
  • 93
PranavPinarayi
  • 3,037
  • 5
  • 21
  • 32

2 Answers2

2

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

Only the UI thread can do changes on UI.

You are trying to modify UI from a non-UI thread. To do things on UI thread, use runOnUiThread() method.

                      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);
                      }
Hemanth
  • 2,717
  • 2
  • 21
  • 29
1

Call the txt.setText("ppp") method inside the runOnUiThread() method. Please note to call the runOnUiThread method using the activity context, as you are creating an inner class, the runOnUiThread method shall not work unless you call it using the activity.

Iqbal S
  • 1,156
  • 10
  • 16