ahmad316948

عدم دریافت تصاویر از دیتابیس (وقتی میخاد تصاویر رو دانلود کنه برنامه کرش می کنه)

ahmad316948 ۱۳۹۷/۰۶/۲۲ مباحث عمومی برنامه‌نویسی

سلام. من دارم یک برنامه شبیه به دیوار می نویسم. تو قسمت نمایش آگاهی ها (اکتیوتی ads_agahi )، تصاویر مربوط به آگهی ها دانلود نمیشن. بلافاصله برنامه کرش می کند.

دوستان میتونید برای راهنمایی به پی وی پیام بدین: Ahmad3169@ 

اینم سورس کد صفحه نمایش آگهی ها (سورس کد مربوط به دانلود تصاویر تو همین سورس کد هست)

package com.example.np.myapp;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class ads_agahi extends AppCompatActivity {

    private String url_ads;
    private int current_page = 0;

    private ListView lv;

    private List<HashMap<String, Object>> all_ads = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ads_agahi);

        Bundle address = getIntent().getExtras();

        url_ads = address.getString("url");

        lv = (ListView) findViewById(R.id.ads_list);

        make_ads_list();
    }

    public void make_ads_list() {
        try {
            DownloadTask dl = new DownloadTask();

            dl.execute(url_ads + current_page);

            current_page++;


        } catch (Exception e) {

            Log.i("Ahmad moenee", "error in ads in make_ads_list() ->" + e.toString());

        }
    }

    private class DownloadTask extends AsyncTask<String, Void, String> {


        @Override
        protected String doInBackground(String... params) {
            String temp = "";

            try {
                JSONDownloader jd = new JSONDownloader();

                temp = jd.downloadURL(params[0]);


            } catch (Exception e) {

                Log.i("Ahmad moenee", "error in DownloadTask  ->" + e.toString());

            }

            return (temp);
        }

        @Override
        protected void onPostExecute(String s) {
            ListViewLoaderTask loader = new ListViewLoaderTask();

            loader.execute(s);


        }
    }

    private class ListViewLoaderTask extends AsyncTask<String, Void, SimpleAdapter> {

        @Override
        protected SimpleAdapter doInBackground(String... params) {
            try {
                AdsParser parser = new AdsParser();
                all_ads.addAll(parser.parse(params[0]));
            } catch (Exception e) {

                Log.i("Ahmad moenee", "error in ListViewLoaderTask ->" + e.toString());

            }

            String[] from = {"image", "title", "intro", "date", "cat"};

            int[] to = {R.id.ads_img, R.id.ads_title, R.id.ads_intro,
                    R.id.ads_date, R.id.ads_cat};

            SimpleAdapter adb = new SimpleAdapter(
                    getBaseContext(), all_ads, R.layout.ads_list_row, from, to
            );

            return adb;
        }


        @Override
        protected void onPostExecute(SimpleAdapter adapter) {

            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("image_path");

                HashMap<String, Object> forDownload = new HashMap<>();

                forDownload.put("image_path", imgURL);
                forDownload.put("position", i);

                ImageDownloaderTask imgDownloader = new ImageDownloaderTask();

                imgDownloader.execute(forDownload);

            }

        }
    }

    private class ImageDownloaderTask extends
           AsyncTask<HashMap<String, Object>, Void, HashMap<String, Object>> {


        @Override
        protected HashMap<String, Object> doInBackground(HashMap<String, Object>... params) {
            InputStream mySteam;

            String imgURL = (String) params[0].get("image_path");
            int position = (Integer) params[0].get("position");

            try {

                URL url = new URL(imgURL);

                HttpURLConnection connection = (HttpURLConnection) url.openConnection();

                connection.setDoInput(true);

                connection.connect();

                mySteam = connection.getInputStream();

                File cacheDirectory = getBaseContext().getCacheDir();

                File temp = new File(cacheDirectory.getPath()
                        + "/image_" + position + "_" + current_page + ".png");

                FileOutputStream outStream = new FileOutputStream(temp);

                Bitmap b = BitmapFactory.decodeStream(mySteam);

                b.compress(Bitmap.CompressFormat.PNG, 100, outStream);

                outStream.flush();

                outStream.close();

                HashMap<String, Object> bitmap = new HashMap<>();

                bitmap.put("image", temp.getPath());
                bitmap.put("position", position);

                return (bitmap);

            } catch (Exception e) {
               Log.i("Ahmad moenee", "error in ImageDonloaderTask ->" + e.toString());


            }


            return null;
        }


        @Override
        protected void onPostExecute(HashMap<String, Object> result) {
            String image = (String) result.get("image");

            int position = (Integer) result.get("position");

            SimpleAdapter adb = (SimpleAdapter) lv.getAdapter();

            HashMap<String, Object> hm = (HashMap<String, Object>)
                    adb.getItem(position);

            hm.put("image", image);

            adb.notifyDataSetChanged();

        }

    }


    public void onbackToHomeClick(View v) {

        finish();
    }
}

پاسخ‌ها به این تاپیک
ahmad316948
ahmad316948
۱۳۹۷/۰۶/۲۲

اینم ارور:

--------- beginning of crash
09-13 12:07:00.295 2451-2451/com.example.np.myapp E/AndroidRuntime: FATAL EXCEPTION: main
                                                                    Process: com.example.np.myapp, PID: 2451
                                                                    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.util.HashMap.get(java.lang.Object)' on a null object reference
                                                                        at com.example.np.myapp.ads_agahi$ImageDownloaderTask.onPostExecute(ads_agahi.java:203)
                                                                        at com.example.np.myapp.ads_agahi$ImageDownloaderTask.onPostExecute(ads_agahi.java:145)
                                                                        at android.os.AsyncTask.finish(AsyncTask.java:667)
                                                                        at android.os.AsyncTask.-wrap1(AsyncTask.java)
                                                                        at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:684)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                        at android.os.Looper.loop(Looper.java:154)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)