views:

89

answers:

2

Hello, when my Async task is executed it completely crashes the app Here is the code to the class. It sits inside of my main activity class. I'm new to threading, so sorry if I've done something ridiculous lol, I don't fully understand it.

EDIT:

    private class TeamSearchTask extends AsyncTask<String,Void,Void> {

    CharSequence nfo;
    String [] matches;
    protected Void doInBackground(String... teamNumber) 
    {
            //Team information ------------------------------------------------------------------------------------
            //Array of team data
            String [] data = APIconnection.getTeams(teamNumber[0], "");

            //Display basic team info
            nfo = ("\nFormal Team Name:\n" + data[1] + 
                    "\n\nLocation:\n" + data [3] + ", " + data[4] + ", " + data[5] +
                    "\n\nRookie Year:\n" + data[6] +
                    "\n\nRobot Name:\n" + data[7] +
                    "\n\nWebsite:\n" + data[8] + "\n\n\n\n\n\n\n\n\n"); 

            //Make match archive --------------------------------------------------------------------------------------

            String [] events = APIconnection.getEventIdsByYear(year1);
            String [] matches = new String [(events.length*11)];;

            for (int i = 0; i<events.length; i++)
            {
                matches[(i*11) + i] = APIconnection.getMatches2(teamNumber[0], events[i] ,"","")[i];            
            }
            return null;
    }

    protected void onProgressUpdate(Void...voids ) 
    {}

    protected void onPostExecute(Void result) {
        info.setText(nfo);
        matchArchive(matches);
    }
}
A: 
titlets.setText(ttl.toString());

Don't touch UI elements in different thread then UI-thread. You can use Activity::runInUiThread(Runnable r) or Handler::post(Runnable r). In second case, handler should be paired with UI-thread.

Damian Kołakowski
A: 
private class TeamSearchTask extends AsyncTask<String,Void,Void> {
    private String[] data;
    protected Void doInBackground(String... teamNumber) {
        // Do your background work! No UI-stuff here!!
        data = APIconnection.getTeams(teamNumber[0], "");

        return null;
    }
    protected void onPostExecute(Void result) {
        // Do all UI related stuff here, it's executed when the doInBackground is finished
    }
}

edit: My bad, fixed the error with onPostExecute. You need to use Void instead of Long as you use

extends AsyncTask<String,Void,Void>

which means input parameter is String, Progress parameter type (in onProgressUpdate) and the 3rd one is for the Result.

Tseng
Even the other XML data I have to get?
Nick
No, just the Java code of course (the one you did in doInBackground) which is related to your task
Tseng