简体   繁体   中英

Android-Java Client-Server - Toast in AsyncTask crashing application

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.

The server send to the client a message like that :

" 1stMember , 2ndMember , 3rdMember , ... , 10thMember "

Then, the Client retrieve this message in the innerclass connectTask from the client class. But the application is always crashing.

This is my client 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();

    }   
}

Before crashing, the application still shows me the toast. Here is my 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)

Here is the Debuger : 在此处输入图片说明

When I do this in onProgressUpdate(String...values), it is not crashing :

protected void onProgressUpdate(String...values)
{
    super.onProgressUpdate(values);
    Toast.makeText(getApplicationContext(),selectedvalues[3],Toast.LENGTH_SHORT).show();
}

But when I make more than 1 toast , then the application is crashing again .

I don't understand what is wrong in my code. Can someone help me please ?

In advance, thank you.

Best regards,

Tofuw

 java.lang.ArrayIndexOutOfBoundsException: length=1; index=3

Length is 1. Array index starts from 0.

The length of selectedvalues is 1. You are trying to access selectedvalues[3] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM