如何解决Android中被拒绝的执行异常

如何解决Android中被拒绝的执行异常,android,android-asynctask,Android,Android Asynctask,拒绝执行异常: 无法接受任务时执行者引发的异常 执行 AsyncTask一次支持10个并发线程和10个排队线程 您正在调用imageLoaderTask.execute(hm)在for循环中的次数过多会导致问题,这是一种不好的做法 相反,您可以只启动一个线程,让它在一个循环中收集所有映像,并将它们全部添加到中的适配器 onPostExecute如果您的执行器调用shutdown方法,然后您为执行指定新任务(Runnable),也会发生此异常。 喜欢 mThreadPoolExecutor.sh

拒绝执行异常: 无法接受任务时执行者引发的异常 执行

AsyncTask一次支持10个并发线程和10个排队线程

您正在调用
imageLoaderTask.execute(hm)
for循环中的次数过多
会导致问题,这是一种不好的做法

相反,您可以只启动一个线程,让它在一个循环中收集所有映像,并将它们全部添加到中的适配器
onPostExecute

如果您的执行器调用shutdown方法,然后您为执行指定新任务(Runnable),也会发生此异常。 喜欢

mThreadPoolExecutor.shutdown();
...
...
...
执行(新的Runnable(){…});

请发布您的完整信息logcat@rajshree发布日志看起来您启动了太多异步任务。您可能从其他地方启动了AsyncTasks?您能帮我获取完整代码吗?Satishkumar我已经发布了日志,请解决我的错误..:)对于您的代码..将imageLoaderTask.execute(hm)放在for之后,并传递SimpleAdapter的对象,而不是hm..imageLoaderTask.execute(适配器)…和ImageLoaderTask的doInBackground中的den使用适配器对象使用for循环..thnkyou先生,我尝试…:)当我使用imagelaodertask时,它的给定错误。执行(适配器)将ImageLoaderTask和doInBackground中的HashMap更改为SimpleAdapter…它不工作,先生..:(你能通过编辑我的代码给我答案吗…:(
public class TopMovie extends Activity {
    GridView lv;
    Vibrator vibrator;
    private Object params;
    public static String movie_Id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        Log.i("Category", MainActivity.movie_Category);
        setContentView(R.layout.new_movie);
        LayoutInflater inflater = getLayoutInflater();
        View view = inflater.inflate(R.layout.customtoast,
                (ViewGroup) findViewById(R.id.custom_toast_layout));
        Toast toast = new Toast(getApplicationContext());
        toast.setDuration(Toast.LENGTH_LONG);
        toast.setGravity(Gravity.CENTER_HORIZONTAL, 0, 0);
        toast.setView(view);
        toast.show();

        vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
        lv = (GridView) findViewById(R.id.grid_view);

        // URL to the JSON data
        String strUrl = "http://vaibhavtech.com/work/android/movie_list.php?category="
                + MainActivity.movie_Category + "&sub_category=other";
        DownloadTask downloadTask = new DownloadTask();
        downloadTask.execute(strUrl);
        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                // // TODO Auto-generated method stub
                vibrator.vibrate(40);
                LayoutInflater inflater = getLayoutInflater();
                View view = inflater.inflate(R.layout.customtoast,
                        (ViewGroup) findViewById(R.id.custom_toast_layout));
                Toast toast = new Toast(getApplicationContext());
                toast.setDuration(Toast.LENGTH_LONG);
                toast.setGravity(Gravity.CENTER_HORIZONTAL, 0, 0);
                toast.setView(view);
                toast.show();
                MainActivity.movie_Id = ((TextView) arg1
                        .findViewById(R.id.tv_girdview_content_id)).getText()
                        .toString();
                Log.i("Name is", MainActivity.movie_Id);
                startActivity(new Intent(TopMovie.this, MovieDescription.class));
            }
        });
    }

    private String downloadUrl(String strUrl) throws IOException {
        String data = "";
        InputStream iStream = null;
        try {
            URL url = new URL(strUrl);

            // Creating an http connection to communicate with url
            HttpURLConnection urlConnection = (HttpURLConnection) url
                    .openConnection();

            // Connecting to url
            urlConnection.connect();

            // Reading data from url
            iStream = urlConnection.getInputStream();

            BufferedReader br = new BufferedReader(new InputStreamReader(
                    iStream));

            StringBuffer sb = new StringBuffer();
            String line = "";

            while ((line = br.readLine()) != null) {
                sb.append(line);
            }

            data = sb.toString();

            br.close();
        } catch (Exception e) {
            Log.d("Exception while downloading url", e.toString());
        } finally {
            iStream.close();
        }

        return data;
    }

    /** AsyncTask to download json data */
    private class DownloadTask extends AsyncTask<String, Integer, String> {
        String data = null;

        @Override
        protected String doInBackground(String... url) {
            try {
                data = downloadUrl(url[0]);
            } catch (Exception e) {
                Log.d("Background Task", e.toString());
            }
            return data;
        }

        @Override
        protected void onPostExecute(String result) {

            // The parsing of the xml data is done in a non-ui thread
            ListViewLoaderTask listViewLoaderTask = new ListViewLoaderTask();

            // Start parsing xml data
            listViewLoaderTask.execute(result);
        }
    }

    /** AsyncTask to parse json data and load ListView */
    private class ListViewLoaderTask extends
            AsyncTask<String, Void, SimpleAdapter> {

        JSONObject jObject;

        // Doing the parsing of xml data in a non-ui thread
        @Override
        protected SimpleAdapter doInBackground(String... strJson) {
            try {
                jObject = new JSONObject(strJson[0]);
                MovieParser countryJsonParser = new MovieParser();
                countryJsonParser.parse(jObject);
            } catch (Exception e) {
                Log.d("JSON Exception1", e.toString());
            }

            // Instantiating json parser class
            MovieParser countryJsonParser = new MovieParser();

            // A list object to store the parsed countries list
            List<HashMap<String, Object>> countries = null;

            try {
                // Getting the parsed data as a List construct
                countries = countryJsonParser.parse(jObject);
            } catch (Exception e) {
                Log.d("Exception", e.toString());
            }

            // Keys used in Hashmap
            String[] from = { "image", "id", "year", "duration", "name" };

            // Ids of views in listview_layout
            // int[] to = {
            // R.id.iv_radio_data_image,R.id.tv_radio_data_id,R.id.tv_radio_data_like,R.id.tv_radio_data_rating,R.id.tv_radio_data_listner,R.id.tv_radio_data_radio_url,R.id.tv_radio_data_name};
            int[] to = { R.id.iv_girdview_content_image,
                    R.id.tv_girdview_content_id, R.id.tv_girdview_content_like,
                    R.id.tv_girdview_content_listner,
                    R.id.tv_girdview_content_name };

            SimpleAdapter adapter = new SimpleAdapter(getBaseContext(),
                    countries, R.layout.grid_view_content, from, to);

            return adapter;
        }

        /** Invoked by the Android on "doInBackground" is executed */
        @Override
        protected void onPostExecute(SimpleAdapter adapter) {

            // Setting adapter for the listview
            lv.setAdapter(adapter);

            for (int i = 0; i < adapter.getCount(); i++) {
                HashMap<String, Object> hm = (HashMap<String, Object>) adapter
                        .getItem(i);
                String imgUrl = (String) hm.get("flag_path");
                ImageLoaderTask imageLoaderTask = new ImageLoaderTask();

                HashMap<String, Object> hmDownload = new HashMap<String, Object>();
                hm.put("flag_path", imgUrl);
                hm.put("position", i);

                // Starting ImageLoaderTask to download and populate image in
                // the listview
                imageLoaderTask.execute(hm);
            }
        }
    }

    /** AsyncTask to download and load an image in ListView */
    private class ImageLoaderTask extends AsyncTask<HashMap<String, Object>, Void, HashMap<String, Object>> {

        @Override
        protected HashMap<String, Object> doInBackground(HashMap<String, Object>... hm) {

            InputStream iStream = null;
            String imgUrl = (String) hm[0].get("flag_path");
            int position = (Integer) hm[0].get("position");

            URL url;
            try {
                url = new URL(imgUrl);

                // Creating an http connection to communicate with url
                HttpURLConnection urlConnection = (HttpURLConnection) url
                        .openConnection();

                // Connecting to url
                urlConnection.connect();

                // Reading data from url
                iStream = urlConnection.getInputStream();

                // Getting Caching directory
                File cacheDirectory = getBaseContext().getCacheDir();

                // Temporary file to store the downloaded image
                File tmpFile = new File(cacheDirectory.getPath() + "/wpta_"
                        + position + ".png");

                // The FileOutputStream to the temporary file
                FileOutputStream fOutStream = new FileOutputStream(tmpFile);

                // Creating a bitmap from the downloaded inputstream
                Bitmap b = BitmapFactory.decodeStream(iStream);

                // Writing the bitmap to the temporary file as png file
                b.compress(Bitmap.CompressFormat.PNG, 100, fOutStream);

                // Flush the FileOutputStream
                fOutStream.flush();

                // Close the FileOutputStream
                fOutStream.close();

                // Create a hashmap object to store image path and its position
                // in the listview
                HashMap<String, Object> hmBitmap = new HashMap<String, Object>();

                // Storing the path to the temporary image file
                hmBitmap.put("image", tmpFile.getPath());

                // Storing the position of the image in the listview
                hmBitmap.put("position", position);

                // Returning the HashMap object containing the image path and
                // position
                return hmBitmap;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(HashMap<String, Object> result) {
            // Getting the path to the downloaded image
            String path = (String) result.get("image");

            // Getting the position of the downloaded image
            int position = (Integer) result.get("position");

            // Getting adapter of the listview
            SimpleAdapter adapter = (SimpleAdapter) lv.getAdapter();

            // Getting the hashmap object at the specified position of the
            // listview
            HashMap<String, Object> hm = (HashMap<String, Object>) adapter
                    .getItem(position);

            // Overwriting the existing path in the adapter
            hm.put("image", path);
            adapter.notifyDataSetChanged();
        }
    }
}
RejectedExecutionException: Task rejected from java.util.concurrent.ThreadPoolExecutorRunning, pool size = 128, active threads = 128, queued tasks = 10, completed tasks = 4]

10-26 08:39:30.797: E/AndroidRuntime(4404): FATAL EXCEPTION: main
10-26 08:39:30.797: E/AndroidRuntime(4404): java.util.concurrent.RejectedExecutionException: Task android.os.AsyncTask$3@411eb538 rejected from java.util.concurrent.ThreadPoolExecutor@40ce4100[Running, pool size = 128, active threads = 128, queued tasks = 10, completed tasks = 4]
10-26 08:39:30.797: E/AndroidRuntime(4404):     at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:1979)
10-26 08:39:30.797: E/AndroidRuntime(4404):     at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:786)
10-26 08:39:30.797: E/AndroidRuntime(4404):     at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1307)
10-26 08:39:30.797: E/AndroidRuntime(4404):     at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:589)
10-26 08:39:30.797: E/AndroidRuntime(4404):     at android.os.AsyncTask.execute(AsyncTask.java:534)
10-26 08:39:30.797: E/AndroidRuntime(4404):     at target.mymovie.TopMovie$ListViewLoaderTask.onPostExecute(TopMovie.java:236)
10-26 08:39:30.797: E/AndroidRuntime(4404):     at target.mymovie.TopMovie$ListViewLoaderTask.onPostExecute(TopMovie.java:1)
10-26 08:39:30.797: E/AndroidRuntime(4404):     at android.os.AsyncTask.finish(AsyncTask.java:631)
10-26 08:39:30.797: E/AndroidRuntime(4404):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
10-26 08:39:30.797: E/AndroidRuntime(4404):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
10-26 08:39:30.797: E/AndroidRuntime(4404):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-26 08:39:30.797: E/AndroidRuntime(4404):     at android.os.Looper.loop(Looper.java:137)
10-26 08:39:30.797: E/AndroidRuntime(4404):     at android.app.ActivityThread.main(ActivityThread.java:5041)
10-26 08:39:30.797: E/AndroidRuntime(4404):     at java.lang.reflect.Method.invokeNative(Native Method)
10-26 08:39:30.797: E/AndroidRuntime(4404):     at java.lang.reflect.Method.invoke(Method.java:511)
10-26 08:39:30.797: E/AndroidRuntime(4404):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-26 08:39:30.797: E/AndroidRuntime(4404):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-26 08:39:30.797: E/AndroidRuntime(4404):     at dalvik.system.NativeStart.main(Native Method)
10-26 08:39:33.097: I/Process(4404): Sending signal. PID: 4404 SIG: 9
10-26 08:39:34.427: E/Trace(4575): error opening trace file: No such file or directory (2)
10-26 08:39:34.977: D/dalvikvm(4575): GC_FOR_ALLOC freed 67K, 8% free 2494K/2684K, paused 33ms, total 36ms
10-26 08:39:34.997: I/dalvikvm-heap(4575): Grow heap (frag case) to 4.016MB for 1536016-byte allocation
10-26 08:39:35.107: D/dalvikvm(4575): GC_FOR_ALLOC freed 2K, 5% free 3992K/4188K, paused 104ms, total 104ms
10-26 08:39:35.157: D/dalvikvm(4575): GC_CONCURRENT freed <1K, 5% free 3992K/4188K, paused 4ms+14ms, total 52ms
10-26 08:39:35.767: D/dalvikvm(4575): GC_CONCURRENT freed 4K, 4% free 4428K/4608K, paused 73ms+4ms, total 144ms
10-26 08:39:36.067: D/gralloc_goldfish(4575): Emulator without GPU emulation detected.