此代码是按照本教程编写的:http ://www.techsono.com/consult/update-android-gui-timer/
我不明白为什么当我尝试在我的设备上运行它时,我会从这段代码中得到一个非法参数异常。我也不确定是什么导致了非法参数异常。我正在运行的代码是我尝试使客户端连接到 IP 地址上的服务器(此 IP 地址将被更改)以接收信息,然后更新 Android Gui 以显示所述信息。如果有帮助,开始布局中的文本视图是 gui 中正在更新的内容吗?
如果我对此的解释似乎有点令人困惑,我提前道歉,这对我来说非常令人困惑:3 我想我理解代码在做什么,但是对代码的任何澄清以及为什么它不起作用以及什么问题会很好.
这是我的代码:
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;
}
}
这是我的 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