【问题标题】:Why does this background task fail with a fatal exception?为什么这个后台任务失败并出现致命异常?
【发布时间】:2014-05-11 11:37:18
【问题描述】:

我的 MainActivity 中的以下代码因覆盖的 doInBackground() 方法中的致命异常而失败:

public class MainActivity extends ActionBarActivity {

    . . .

    public void onFetchBtnClicked(View v){
        if(v.getId() == R.id.FetchBtn){
            Toast.makeText(getApplicationContext(), "You mashed the button, dude.", Toast.LENGTH_SHORT).show();
            new NetworkTask().execute();
        }
    }

    public static class NetworkTask extends AsyncTask<String, Void, HttpResponse> {

        @Override
        protected HttpResponse doInBackground(String... params) {
            String link = params[0];
            HttpGet request = new HttpGet("http://localhost:28642/api/deliveries/Count");
            AndroidHttpClient client = AndroidHttpClient.newInstance("Android");
            try {
                return client.execute(request);
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            } finally {
                client.close();
            }
        }

        @Override
        protected void onPostExecute(HttpResponse result) {
            FileOutputStream f = null;
            //Do something with result
            if (result != null)
                    /* result.getEntity().writeTo(new FileOutputStream(f)); */
                try {
                    result.getEntity().writeTo(f);
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }

    }

具体的异常消息,以及一些预异常上下文如下:

03-31 18:04:26.350    1401-1401/hhs.app I/Choreographer﹕ Skipped 33 frames!  The application may be doing too much work on its main thread.
03-31 18:05:00.030    1401-1420/hhs.app W/dalvikvm﹕ threadid=12: thread exiting with uncaught exception (group=0xb2a7aba8)
03-31 18:05:00.250    1401-1420/hhs.app E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
    Process: hhs.app, PID: 1401
    java.lang.RuntimeException: An error occured while executing doInBackground()
            at android.os.AsyncTask$3.done(AsyncTask.java:300)
            at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
            at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
            at java.util.concurrent.FutureTask.run(FutureTask.java:242)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:841)
     Caused by: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
            at hhs.app.MainActivity$NetworkTask.doInBackground(MainActivity.java:68)
            at hhs.app.MainActivity$NetworkTask.doInBackground(MainActivity.java:64)
            at android.os.AsyncTask$2.call(AsyncTask.java:288)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:841)

这有什么问题 - 它在 doInBackground() 的 git-go 中失败。我的 URL “坏”是​​由于重击(在 C# 中,我会使用逐字字符串),还是...?

更新

我删除了有问题的行,但它仍然失败,抛出 IOException。

我实际上已经将齿轮切换到完全不同的代码,可以在这里看到 [为什么我会得到“不兼容的类型:对象不能转换为字符串”? (这是抛出 NetworkOnMainThreadException),但是当失败时,我决定再给这段代码一次机会,但是输入了“catch (IOException e)”块。

有人知道为什么吗?

【问题讨论】:

  • 因为参数数组中没有参数。

标签: java android android-asynctask android-studio androidhttpclient


【解决方案1】:

您忘记在此处将任何参数传递给您的AsyncTask

 new NetworkTask().execute();

这就是为什么你的数组在这里是空的:

 String link = params[0];

==> 在执行 AsyncTask 时传递一个参数:

 new NetworkTask().execute(someLink); //execute AsyncTask with one param

更新: 实际上,您似乎没有在 doInBackground 方法中对 String link 执行任何操作。所以也许你可以摆脱那条线?

【讨论】:

    【解决方案2】:

    这个例外:

    Caused by: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
    

    告诉你你正在访问索引 0 处的一个空数组,在这一行:

    protected HttpResponse doInBackground(String... params) {
         String link = params[0];
    

    注意,在赋值之后你不会在任何地方使用链接变量(也不是参数)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-01
      • 1970-01-01
      • 2019-07-14
      • 2019-12-15
      • 2018-06-08
      • 2015-06-04
      • 2021-04-01
      • 2022-11-08
      相关资源
      最近更新 更多