帮助异步任务,崩溃.execute()

时间:2010-10-10 18:18:29

标签: android android-asynctask

执行我的异步任务时,它会完全崩溃应用程序 这是该类的代码。它位于我的主要活动类中。 我是新手,我很抱歉,如果我做了一些荒谬的事情,我不完全理解它。

修改

    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);
    }
}

2 个答案:

答案 0 :(得分:1)

titlets.setText(ttl.toString());

不要在UI线程中触摸不同线程中的UI元素。您可以使用Activity :: runInUiThread(Runnable r)或Handler :: post(Runnable r)。在第二种情况下,处理程序应该与UI线程配对。

答案 1 :(得分:0)

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
    }
}

编辑我的不好,用onPostExecute修复了错误。在使用

时,您需要使用Void而不是Long
extends AsyncTask<String,Void,Void>

表示输入参数为String,Progress参数类型(在onProgressUpdate中),第三个用于Result。