AsyncTask使应用程序崩溃

时间:2013-12-02 21:09:05

标签: android android-asynctask

我有queryAppIcon()方法,用于在数组appIconDrawable中查询和存储图像。但是,我只是在应用程序崩溃之前弹出了几秒钟的空白图像(如果我使用appIconDrawable [1] = d来测试processFinish)并且如果我使用appIconDrawable [i] = d,应用只是崩溃。我猜它与我在queryAppIcon()中发送for循环的方式以及它与AsyncTask的连接方式有关,但我不确定它是什么

这是ViewActivity中的相关代码:

// global vars
final Drawable[] appIconDrawable = null;
int i;

public Drawable[] queryAppIcon() throws ParseException, IOException {
    ParseQuery<ParseObject> query = ParseQuery.getQuery("AndroidStoreContent");
    query.whereExists("appIcon");
    List<ParseObject> ParseResult = query.find();
    // initialize Drawable array
    final Drawable[] appIconDrawable = new Drawable[ParseResult.size()];

    for (i = 0; i < ParseResult.size(); i++) {
        ParseFile pf = (ParseFile) ParseResult.get(i).get("appIcon");
        startDownload(pf);
    }
    return appIconDrawable;
}

public void startDownload(ParseFile pf) {
    new DownloadImageTask(this).execute(pf);
}

public class DownloadImageTask extends AsyncTask<ParseFile, Void, Drawable> {

    private AsyncResponse ar;

    DownloadImageTask(AsyncResponse ar) {
        this.ar = ar;
    }

    @Override
    protected Drawable doInBackground(ParseFile... pf) {
        return fetchDrawable(pf[0]);
    }

    protected void onPostExecute(Drawable result) {
        ar.processFinish(result);
    }

    public Drawable fetchDrawable(ParseFile pf) {
        InputStream is;
        try {
            is = (InputStream) new URL(pf.getUrl()).getContent();
            return Drawable.createFromStream(is,null);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}

@Override
public void processFinish(Drawable d) {
    appIconDrawable[i] = d; // i also tried testing appIconDrawable[1] = d and the app loaded with all blank images and then crashes
}

这是界面AsyncResponse

public interface AsyncResponse {
    void processFinish(Drawable d);
}

LogCat(还有什么我应该发布的吗?):

12-02 12:58:05.782: E/AndroidRuntime(6598):     at com.geekyouup.android.newton.StoreListViewActivity.processFinish(StoreListViewActivity.java:200)
12-02 12:58:05.782: E/AndroidRuntime(6598):     at com.geekyouup.android.newton.StoreListViewActivity$DownloadImageTask.onPostExecute(StoreListViewActivity.java:181)
12-02 12:58:05.782: E/AndroidRuntime(6598):     at com.geekyouup.android.newton.StoreListViewActivity$DownloadImageTask.onPostExecute(StoreListViewActivity.java:1)
12-02 12:58:14.481: D/IPPolicy(1343): updateImsNotification. ComponentName: ComponentInfo{com.geekyouup.android.newton/com.geekyouup.android.newton.StoreListViewActivity} tickerText null
12-02 12:58:14.511: D/IPPolicy(1343): updateImsNotification. ComponentName: ComponentInfo{com.geekyouup.android.newton/com.geekyouup.android.newton.StoreListViewActivity} tickerText null
12-02 12:58:15.512: D/IPPolicy(1343): updateImsNotification. ComponentName: ComponentInfo{com.geekyouup.android.newton/com.geekyouup.android.newton.StoreListViewActivity} tickerText null
12-02 12:58:16.383: I/ActivityManager(725): Process com.geekyouup.android.newton (pid 6598) (adj 0) has died.

1 个答案:

答案 0 :(得分:1)

您似乎没有初始化成员变量appIconDrawable

这一行:

final Drawable[] appIconDrawable = new Drawable[ParseResult.size()];

定义局部变量。如果要初始化成员变量,那么它应该是:

appIconDrawable = new Drawable[ParseResult.size()];

我没有看到queryAppIcon的电话,我猜你在某个地方叫它......