java– 为什么这个后台任务因致命异常而失败?

java– 为什么这个后台任务因致命异常而失败?,第1张

概述我的MainActivity中的以下代码在重写的doInBackground()方法中失败并出现致命异常:publicclassMainActivityextendsActionBarActivity{...publicvoidonFetchBtnClicked(Viewv){if(v.getId()==R.id.FetchBtn){Toast.makeText(getApp

我的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();                }        }    }

具有异常上下文的smIDgin的特定异常消息如下:

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.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.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.call(AsyncTask.java:288)            at java.util.concurrent.FutureTask.run(FutureTask.java:237)            at androID.os.AsyncTask$SerialExecutor.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失败了.我的网址是由于打击造成的“坏”(在C#中,我会使用逐字字符串),还是……?

UPDATE

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

我实际上切换到了完全不同的代码,这里可以看到[为什么我得到,“不兼容的类型:对象无法转换为字符串”这个? (它正在抛出networkonmainthreadException),但是当失败时我决定再给这个代码一个机会,但是输入了“catch(IOException e)”块.

有人知道为什么吗?

解决方法:

这个例外:

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

告诉您正在访问索引0处的空数组,在此行中:

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

注意,在任务之后你不要在任何地方使用链接变量(也不是params).

总结

以上是内存溢出为你收集整理的java – 为什么这个后台任务因致命异常而失败?全部内容,希望文章能够帮你解决java – 为什么这个后台任务因致命异常而失败?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1113312.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-29
下一篇2022-05-29

发表评论

登录后才能评论

评论列表(0条)

    保存