Question

This code was written following this tutorial: http://www.techsono.com/consult/update-android-gui-timer/

I don't understand why I'm getting an illegal argument exception from this code when i try and run it on my device. I'm not certain whats causing the illegal argument exception either. The code I'm running is my attempt to make a client that connects to a server at an IP address (this IP address will be changed) in order to receive information then update the Android Gui to show said information. Text views in the starting layout are whats being updated in the gui if it helps?

I apologize in advance if my explanation of this seems a bit confusing, its just very confusing to me :3 i think I understand what the codes doing but any clarification on the code and why it isn't working and whats the problem would be great.

heres my code:

package com.example.clienttest;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.widget.TextView;

public class ClientTestMain extends Activity {

final Handler myHandler = new Handler();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_client_test_main);


    Timer myTimer = new Timer();
    myTimer.schedule(new TimerTask(){
        @Override
        public void run(){
            UpdateGUI();}},0,0);
        }


final Runnable myRunnable = new Runnable(){
            @Override
            public void run(){

                TextView Header = (TextView) findViewById(R.id.textView4);
                TextView Time = (TextView) findViewById(R.id.textView4);
                TextView TrackId = (TextView) findViewById(R.id.textView4);
                TextView Latitude = (TextView) findViewById(R.id.textView4);
                TextView Longitude = (TextView) findViewById(R.id.textView4);
                TextView Depth = (TextView) findViewById(R.id.textView4);
                TextView Speed = (TextView) findViewById(R.id.textView4);
                TextView Heading = (TextView) findViewById(R.id.textView4);

                try{
                    while(true){
                    Socket infoSocket = new Socket("128.23.1.0", 2000);
                    InputStreamReader stream = new InputStreamReader(infoSocket.getInputStream());
                    BufferedReader reader = new BufferedReader(stream);
                    String message = reader.readLine();
                    String[] op = message.split(",");
                    Header.setText(op[0]);
                    Time.setText(op[1]);
                    TrackId.setText(op[2]);
                    Latitude.setText(op[3]);
                    Longitude.setText(op[4]);
                    Depth.setText(op[5]);
                    Speed.setText(op[6]);
                    Heading.setText(op[7]);
                    TimeUnit.SECONDS.sleep(1);
                    }
                }catch(IOException ex){
                    ex.printStackTrace();
                }catch (InterruptedException e) {
                }

            }};



  private void UpdateGUI(){
      myHandler.post(myRunnable);
  }



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.client_test_main, menu);
    return true;
}

}

Heres my LogCat:

09-04 12:04:23.453: D/AndroidRuntime(18628): Shutting down VM
09-04 12:04:23.453: W/dalvikvm(18628): threadid=1: thread exiting with uncaught exception    (group=0x40bea1f8)
09-04 12:04:23.453: E/AndroidRuntime(18628): FATAL EXCEPTION: main
09-04 12:04:23.453: E/AndroidRuntime(18628): java.lang.RuntimeException: Unable to start  activity ComponentInfo{com.example.clienttest/com.example.clienttest.ClientTestMain}: java.lang.IllegalArgumentException
09-04 12:04:23.453: E/AndroidRuntime(18628):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
09-04 12:04:23.453: E/AndroidRuntime(18628):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
09-04 12:04:23.453: E/AndroidRuntime(18628):    at android.app.ActivityThread.access$600(ActivityThread.java:128)
09-04 12:04:23.453: E/AndroidRuntime(18628):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
09-04 12:04:23.453: E/AndroidRuntime(18628):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-04 12:04:23.453: E/AndroidRuntime(18628):    at android.os.Looper.loop(Looper.java:137)
09-04 12:04:23.453: E/AndroidRuntime(18628):    at android.app.ActivityThread.main(ActivityThread.java:4514)
09-04 12:04:23.453: E/AndroidRuntime(18628):    at java.lang.reflect.Method.invokeNative(Native Method)
09-04 12:04:23.453: E/AndroidRuntime(18628):    at java.lang.reflect.Method.invoke(Method.java:511)
09-04 12:04:23.453: E/AndroidRuntime(18628):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
09-04 12:04:23.453: E/AndroidRuntime(18628):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
09-04 12:04:23.453: E/AndroidRuntime(18628):    at dalvik.system.NativeStart.main(Native Method)
09-04 12:04:23.453: E/AndroidRuntime(18628): Caused by: java.lang.IllegalArgumentException
09-04 12:04:23.453: E/AndroidRuntime(18628):    at java.util.Timer.schedule(Timer.java:479)
09-04 12:04:23.453: E/AndroidRuntime(18628):    at com.example.clienttest.ClientTestMain.onCreate(ClientTestMain.java:28)
09-04 12:04:23.453: E/AndroidRuntime(18628):    at android.app.Activity.performCreate(Activity.java:4465)
09-04 12:04:23.453: E/AndroidRuntime(18628):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
09-04 12:04:23.453: E/AndroidRuntime(18628):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
09-04 12:04:23.453: E/AndroidRuntime(18628):    ... 11 more
09-04 12:09:29.781: I/Process(18628): Sending signal. PID: 18628 SIG: 9
Was it helpful?

Solution

You cannot pass period value = 0

Throws

IllegalArgumentException if delay < 0 or period <= 0.

IllegalStateException if the Timer has been canceled, or if the task has been scheduled or canceled.

myTimer.schedule(new TimerTask(){
        @Override
        public void run(){
            UpdateGUI();}},0,1000); //UPDATE HERE
        }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top