AsyncTask 报错 Fatal exception : AsyncTask #1 Android

文章标签: android,asynctask
2014-11-20 9:39:49     人阅读    

新手在学习使用 AsyncTask  的时候报错了,Fatal Exception
报错的代码如下:

String username = usr_number.getText().toString();
        String password = password_main.getText().toString();
        String[] Loginvalues = {username,password};

        new CheckUsername().execute(Loginvalues);

完整的类代码

public class CheckUsername extends AsyncTask<String[], Integer, String[]>{

    @Override
    protected String[] doInBackground(String[]... params) {
        // TODO Auto-generated method stub
        String username = params[0].toString();
        String password = params[1].toString();
        System.out.println(username);
        System.out.println(password);
        return null;
    }

}

控制台输出的错误代码如下:
05-09 03:11:04.428: E/AndroidRuntime(1139): FATAL EXCEPTION: AsyncTask #1
05-09 03:11:04.428: E/AndroidRuntime(1139): Process: com.hexwhale.pocketcollege, PID: 1139
05-09 03:11:04.428: E/AndroidRuntime(1139): java.lang.RuntimeException: An error occured while executing doInBackground()
05-09 03:11:04.428: E/AndroidRuntime(1139):     at android.os.AsyncTask$3.done(AsyncTask.java:300)
05-09 03:11:04.428: E/AndroidRuntime(1139):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
05-09 03:11:04.428: E/AndroidRuntime(1139):     at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
05-09 03:11:04.428: E/AndroidRuntime(1139):     at java.util.concurrent.FutureTask.run(FutureTask.java:242)
05-09 03:11:04.428: E/AndroidRuntime(1139):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
05-09 03:11:04.428: E/AndroidRuntime(1139):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
05-09 03:11:04.428: E/AndroidRuntime(1139):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
05-09 03:11:04.428: E/AndroidRuntime(1139):     at java.lang.Thread.run(Thread.java:841)
05-09 03:11:04.428: E/AndroidRuntime(1139): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
05-09 03:11:04.428: E/AndroidRuntime(1139):     at com.hexwhale.pocketcollege.CheckUsername.doInBackground(CheckUsername.java:11)
05-09 03:11:04.428: E/AndroidRuntime(1139):     at com.hexwhale.pocketcollege.CheckUsername.doInBackground(CheckUsername.java:1)
05-09 03:11:04.428: E/AndroidRuntime(1139):     at android.os.AsyncTask$2.call(AsyncTask.java:288)
05-09 03:11:04.428: E/AndroidRuntime(1139):     at java.util.concurrent.FutureTask.run(FutureTask.java:237)

处理方法

解决很简单,简单替换下代码


 

public class CheckUsername extends AsyncTask<String, Integer, String[]>{

@Override
protected String[] doInBackground(String... params) {
    // TODO Auto-generated method stub
    String username = params[0].toString();
    String password = params[1].toString();
    System.out.println(username);
    System.out.println(password);
    return null;
}

}


原文地址:http://www.itmmd.com/201411/196.html
该文章由 萌萌的IT人 整理发布,转载须标明出处。

andorid在 Fragment中调用摄像机   上一篇
下一篇  Android 5.0 Material-styled tabs
精彩回复
我要追加问题,请求站长解决!
姓名:       

《程序员app》专门为程序员量身定做!