我有两个通过AsyncTask进行通信的应用程序。第一个应用程序是在我的PC上运行的Java服务器。第二个应用程序是Android客户端。
服务器向客户端发送如下消息:
“1stMember,2ndMember,3rdMember,...,10thMember”
然后,客户端从客户端类的内部类 connectTask 中检索此消息。 但是应用程序总是崩溃。
这是我的客户 MainActivity.java :
public class MainActivity extends FragmentActivity
{
private TCPClient tcpclient;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Connect to server - See innerClass connectTask below
new connectTask().execute("");
}
public class connectTask extends AsyncTask<String, String, TCPClient>
{
protected TCPClient doInBackground(String... message)
{
// Creation of TCPClient object...
tcpclient=new TCPClient(new TCPClient.OnMessageReceived()
{
public void messageReceived(String message)
{
publishProgress(message);
/*==> publishProgress receive message, then notify onProgressUpdate*/
}
});
tcpclient.runClient();
return null;
}
protected void onProgressUpdate(String...values)
{
super.onProgressUpdate(values);
showMySelectedValues(values[0].toString());
}
}
public void showMySelectedValues(String values)
{
String[]selectedvalues=values.split(",");
Log.i("LENGTH OF selectedvalues... : ",""+selectedvalues.length);
Toast.makeText(getApplicationContext(),selectedvalues[3],Toast.LENGTH_SHORT).show(); Toast.makeText(getApplicationContext(),selectedvalues[5],Toast.LENGTH_SHORT).show();
}
}
在崩溃之前,应用程序仍然向我展示吐司。 这是我的Logcat:
12-11 16:09:33.070: D/dalvikvm(11798): GC_FOR_ALLOC freed 5K, 12% free 9455K/10695K, paused 28ms, total 28ms
12-11 16:09:35.914: I/LENGTH OF selectedvalues... :(11798): 12
12-11 16:09:35.945: I/LENGTH OF selectedvalues... :(11798): 12
12-11 16:09:35.953: I/LENGTH OF selectedvalues... :(11798): 12
12-11 16:09:35.976: I/LENGTH OF selectedvalues... :(11798): 1
12-11 16:09:35.976: D/AndroidRuntime(11798): Shutting down VM
12-11 16:09:35.976: W/dalvikvm(11798): threadid=1: thread exiting with uncaught exception (group=0x4104f2a0)
12-11 16:09:35.984: E/AndroidRuntime(11798): FATAL EXCEPTION: main
12-11 16:09:35.984: E/AndroidRuntime(11798): java.lang.ArrayIndexOutOfBoundsException: length=1; index=3
12-11 16:09:35.984: E/AndroidRuntime(11798): at com.fiamm.tcpandroid.MainActivity.showMySelectedValues(MainActivity.java:154)
12-11 16:09:35.984: E/AndroidRuntime(11798): at com.fiamm.tcpandroid.MainActivity$connectTask.onProgressUpdate(MainActivity.java:145)
12-11 16:09:35.984: E/AndroidRuntime(11798): at com.fiamm.tcpandroid.MainActivity$connectTask.onProgressUpdate(MainActivity.java:1)
12-11 16:09:35.984: E/AndroidRuntime(11798): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:647)
12-11 16:09:35.984: E/AndroidRuntime(11798): at android.os.Handler.dispatchMessage(Handler.java:99)
12-11 16:09:35.984: E/AndroidRuntime(11798): at android.os.Looper.loop(Looper.java:137)
12-11 16:09:35.984: E/AndroidRuntime(11798): at android.app.ActivityThread.main(ActivityThread.java:4895)
12-11 16:09:35.984: E/AndroidRuntime(11798): at java.lang.reflect.Method.invokeNative(Native Method)
12-11 16:09:35.984: E/AndroidRuntime(11798): at java.lang.reflect.Method.invoke(Method.java:511)
12-11 16:09:35.984: E/AndroidRuntime(11798): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
12-11 16:09:35.984: E/AndroidRuntime(11798): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
12-11 16:09:35.984: E/AndroidRuntime(11798): at dalvik.system.NativeStart.main(Native Method)
这是 Debuger :
当我在onProgressUpdate(String ... values)中执行此操作时,它不会崩溃:
protected void onProgressUpdate(String...values)
{
super.onProgressUpdate(values);
Toast.makeText(getApplicationContext(),selectedvalues[3],Toast.LENGTH_SHORT).show();
}
但是当我制作超过1个吐司时,应用程序再次崩溃。
我不明白我的代码有什么问题。有人能帮帮我吗?
事先,谢谢。
致以最诚挚的问候,
Tofuw
答案 0 :(得分:1)
java.lang.ArrayIndexOutOfBoundsException: length=1; index=3
长度为1.数组索引从0开始。
selectedvalues
的长度为1.您正尝试访问selectedvalues[3]
。