Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid-Java Client-Server - Toast in AsyncTask crashing application
    text
    copied!<p>I have 2 applications which are communicating through an AsyncTask. The first application is a Java server, running on my PC. The second application is an Android Client.</p> <p>The server send to the client a message like that :</p> <blockquote> <p>" 1stMember , 2ndMember , 3rdMember , ... , 10thMember "</p> </blockquote> <p>Then, the Client retrieve this message in the innerclass <strong>connectTask</strong> from the client class. But the application is always crashing.</p> <p>This is my client <strong>MainActivity.java</strong> :</p> <pre><code>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&lt;String, String, TCPClient&gt; { protected TCPClient doInBackground(String... message) { // Creation of TCPClient object... tcpclient=new TCPClient(new TCPClient.OnMessageReceived() { public void messageReceived(String message) { publishProgress(message); /*==&gt; 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(); } } </code></pre> <p>Before crashing, the application still shows me the toast. <strong>Here is my Logcat :</strong></p> <p><img src="https://i.stack.imgur.com/nc1Pl.png" alt="enter image description here"> </p> <pre><code>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) </code></pre> <p>Here is the <strong>Debuger</strong>: <img src="https://i.stack.imgur.com/WFNTH.png" alt="enter image description here"></p> <p>When I do this in onProgressUpdate(String...values), it is not crashing :</p> <pre><code>protected void onProgressUpdate(String...values) { super.onProgressUpdate(values); Toast.makeText(getApplicationContext(),selectedvalues[3],Toast.LENGTH_SHORT).show(); } </code></pre> <p>But when I <strong>make more than 1 toast</strong>, then the <strong>application is crashing again</strong>.</p> <p>I don't understand what is wrong in my code. Can someone help me please ?</p> <p>In advance, thank you.</p> <p>Best regards,</p> <p>Tofuw</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload