将函数移出当前类后,AsyncTask doinBackground运行时错误

时间:2014-06-03 15:16:45

标签: android class android-asynctask httpclient

我有一个LoadImag类扩展了从在线资源引用的AsyncTask。在doinBackfround中,它调用当前类中的函数。一切都运行良好,但当我将该函数移出当前类时,它会显示运行时错误函数。我不明白为什么。 这是我的代码:

这是AsyncTask:

它位于CLASS FragmentOne.java

public class loadMovieView extends AsyncTask<String, String, Void> {
    @Override
    protected void onPreExecute() {
        Toast.makeText(getActivity().getApplicationContext(),
                "onPreExecute \n: preload bitmap in AsyncTask",
                Toast.LENGTH_LONG).show();
    }

    @Override
    protected Void doInBackground(String...url) {
        // TODO 
        try{
            for(String strUrl: url){
                scrBitmap = decodeImg(strUrl,250,380);
                publishProgress(strUrl);
            }
        }catch (Exception e) {
            Log.v("img_decode", e.getMessage().toString());
        }

        return null;
    }

    @Override
    protected void onProgressUpdate(String...arrUrl){
        Log.v("CurrentUrl", arrUrl.toString());
        horizontaLayout.addView(AddImagesToView(scrBitmap));
    }

}

以下代码是我在AsyncTask中使用的函数,decodeImg()和AddImagesToView();因为我需要经常使用decodeImg()函数,

所以我创建了一个名为ImageLoader.java的新类文件

public class ImageLoader {
ImageLoader imgeLoader;

public Bitmap decodeImg(String path, int reqWidth,
        int reqHeight) {
}

public Bitmap LoadImage(String url, BitmapFactory.Options options) {
    // TODO Auto-generated method stub
}

private InputStream OpenHttpConnection(String strUrl) throws IOException {
}
}

但现在我改变了

scrBitmap = decodeSampledBitmapFromUri(strUrl,250,380);

在AsyncTask中

ImageLoader imgLoader;
scrBitmap = imgLoader.decodeSampledBitmapFromUri(strUrl,250,380);

它现在不起作用。错误日志如下:

06-03 11:12:03.462: E/AndroidRuntime(19993): FATAL EXCEPTION: AsyncTask #2
06-03 11:12:03.462: E/AndroidRuntime(19993): Process: com.example.theatre_sliding, PID: 19993
06-03 11:12:03.462: E/AndroidRuntime(19993): java.lang.RuntimeException: An error occured while executing     doInBackground()
06-03 11:12:03.462: E/AndroidRuntime(19993): at android.os.AsyncTask$3.done(AsyncTask.java:300)
06-03 11:12:03.462: E/AndroidRuntime(19993): at  java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
06-03 11:12:03.462: E/AndroidRuntime(19993): at     java.util.concurrent.FutureTask.setException(FutureTask.java:222)
06-03 11:12:03.462: E/AndroidRuntime(19993): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
06-03 11:12:03.462: E/AndroidRuntime(19993): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
06-03 11:12:03.462: E/AndroidRuntime(19993): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
06-03 11:12:03.462: E/AndroidRuntime(19993): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
06-03 11:12:03.462: E/AndroidRuntime(19993): at java.lang.Thread.run(Thread.java:841)
06-03 11:12:03.462: E/AndroidRuntime(19993): Caused by: java.lang.NullPointerException
06-03 11:12:03.462: E/AndroidRuntime(19993): at com.example.theatre_sliding.FragmentOne$loadMovieView.doInBackground(FragmentOne.java:128)
06-03 11:12:03.462: E/AndroidRuntime(19993): at com.example.theatre_sliding.FragmentOne$loadMovieView.doInBackground(FragmentOne.java:1)
06-03 11:12:03.462: E/AndroidRuntime(19993): at android.os.AsyncTask$2.call(AsyncTask.java:288)
06-03 11:12:03.462: E/AndroidRuntime(19993): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
06-03 11:12:03.462: E/AndroidRuntime(19993): ... 4 more

我不知道为什么会这样,有人可以帮助我吗?感谢!!!

1 个答案:

答案 0 :(得分:0)

您尚未正确初始化对象。所以这段代码应该是:

ImageLoader imgLoader;

这样的事情:

ImageLoader imgLoader = new ImageLoader();