简体   繁体   English

调试AsyncTask-奇怪的行为,调试跳入代码

[英]Debugging AsyncTask - strange behaviour, debug jumping into the code

I'm debugging an Asynctask that simply downloads a file: here the code: 我正在调试一个仅下载文件的Asynctask:这里的代码:

public class AsyncDownloadFilesTask extends AsyncTask<String, Integer, Boolean> {

public AsyncResponse<Boolean> delegate=null;
protected Boolean doInBackground(String... params) {
    android.os.Debug.waitForDebugger();
    try {   
        URL url = new URL(params[0]);
        int count;
        String fileName = new String(params[1]);
        URLConnection connessione = url.openConnection();
        connessione.connect();

        int lenghtOfFile = connessione.getContentLength();
        InputStream input = new BufferedInputStream(url.openStream());

        OutputStream output = new FileOutputStream(fileName);

        long total = 0;

        byte data[] = new byte[1024];
        while ((count = input.read(data)) != -1) {
            total += count;
            publishProgress((int)((total*100)/lenghtOfFile));
            output.write(data, 0, count);
        }

        output.flush();
        output.close();
        input.close();
        return Boolean.valueOf(true);

    } catch (Exception e) {
        return null;

    }
}

protected void onPostExecute(Boolean result) {
    delegate.processFinish(result);
}
}

I obtain a strange behaviour: when execution arrive to return Boolean.valueOf(true); 我得到一个奇怪的行为:当执行到达时返回Boolean.valueOf(true); it skips to return null; 跳过返回null; into the catch block, but Exception e is null, and then debugger goto line 1 of AsyncTask, that is simply package com.example.compa.asynctasks; 进入catch块,但Exception e为null,然后调试器转到AsyncTask的第1行,即打包com.example.compa.asynctasks; Then execution goes on (executing onPostExecute method) and, of course, returned result is null 然后继续执行(执行onPostExecute方法),当然,返回的结果为null

What happens? 怎么了? Why debug jump in this way? 为什么以这种方式调试跳转?

Task download correctly the file. 任务正确下载文件。

Here code of the Activity that instantiates and calls Async Task 这是实例化并调用异步任务的活动的代码

package com.example.compa.activities;

import android.app.Activity;
import ...

public class CoverActivity extends Activity implements AsyncResponse<Boolean>{

ImageView coverImg;
Drawable d;
CompassesFileManager cfm;
int coverId;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cover);
    coverId = getIntent().getExtras().getInt("coverId");
    cfm = new CompassesFileManager(this);

    ImageView coverImg = (ImageView)findViewById(R.id.cover_image);
    d = cfm.getCover(coverId);
    if (d!=null){
        coverImg.setImageDrawable(d);
    } else {
        AsyncDownloadFilesTask task = new AsyncDownloadFilesTask();
        task.delegate = this;
        task.execute(cfm.getCoverURL(coverId), cfm.getCoverFileName(coverId));
    }

}

@Override
public void processFinish(Boolean output) {
    if (output){
        Drawable d = cfm.getCover(coverId);
        coverImg.setImageDrawable(d);
    } else {
        finish();
    }

}
}

Stacktrace of error: 错误的堆栈跟踪:

02-21 19:37:29.520: E/AndroidRuntime(407): FATAL EXCEPTION: main
02-21 19:37:29.520: E/AndroidRuntime(407): java.lang.NullPointerException
02-21 19:37:29.520: E/AndroidRuntime(407):  at com.example.compa.asynctasks.AsyncDownloadFilesTask.onPostExecute(AsyncDownloadFilesTask.java:65)
02-21 19:37:29.520: E/AndroidRuntime(407):  at com.example.compa.asynctasks.AsyncDownloadFilesTask.onPostExecute(AsyncDownloadFilesTask.java:1)
02-21 19:37:29.520: E/AndroidRuntime(407):  at android.os.AsyncTask.finish(AsyncTask.java:631)
02-21 19:37:29.520: E/AndroidRuntime(407):  at android.os.AsyncTask.access$600(AsyncTask.java:177)
02-21 19:37:29.520: E/AndroidRuntime(407):  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
02-21 19:37:29.520: E/AndroidRuntime(407):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-21 19:37:29.520: E/AndroidRuntime(407):  at android.os.Looper.loop(Looper.java:176)
02-21 19:37:29.520: E/AndroidRuntime(407):  at android.app.ActivityThread.main(ActivityThread.java:5419)
02-21 19:37:29.520: E/AndroidRuntime(407):  at java.lang.reflect.Method.invokeNative(Native Method)
02-21 19:37:29.520: E/AndroidRuntime(407):  at java.lang.reflect.Method.invoke(Method.java:525)
02-21 19:37:29.520: E/AndroidRuntime(407):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
02-21 19:37:29.520: E/AndroidRuntime(407):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
02-21 19:37:29.520: E/AndroidRuntime(407):  at dalvik.system.NativeStart.main(Native Method)

line: 02-21 19:37:29.520: E/AndroidRuntime(407): at com.example.compa.asynctasks.AsyncDownloadFilesTask.onPostExecute(AsyncDownloadFilesTask.java:65) is the last one of AsyncDownloadFilesTask class, and is a closing bracket, } 行:02-21 19:37:29.520:E / AndroidRuntime(407):位于com.example.compa.asynctasks.AsyncDownloadFilesTask.onPostExecute(AsyncDownloadFilesTask.java:65)是AsyncDownloadFilesTask类的最后一个,并且是一个括弧,}

Thank you 谢谢

I don't have enough points to comment, but it looks like delegate is null in your onPostExecute 我没有足够的要评论的地方,但是看来onPostExecute中的委托为null

delegate.processFinish(result); // delegate is null

if that's not the case, you're code stub above doesn't define it though. 如果不是这种情况,则上面的代码存根并没有定义它。

I solved on my own. 我自己解决了。

1st, I move call to the Async Task in the onStart() method, instead of onCreate() 1,我将调用移到onStart()方法而不是onCreate()中的异步任务上

2nd, I made a mistake, in change line 2,我在改行中犯了一个错误

ImageView coverImg = (ImageView)findViewById(R.id.cover_image);

in

coverImg = (ImageView)findViewById(R.id.cover_image);

to avoid a stupid null pointer (I already declared coverImg)! 避免一个愚蠢的空指针(我已经声明了CoverImg)!

Anyway, I still don't understand debug's behaviour, but I solved my problem. 无论如何,我仍然不了解调试的行为,但是我解决了我的问题。

Thank you everybody 谢谢大家

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM