0

List View在我的应用程序中有一个,我通过Base Adapter. 他们是我面临的两个问题,并且提到了一些帖子,但所有建议的程序都与我遵循的相同。

问题是

  1. 我正在尝试从 JSON 给出的 URL 下载图像。一切正常,但图像永远不会设置为Image View.

  2. 我绑定了类Text to Speech中按钮的单击事件Base Adapter并将其从 java 类中释放,onDestroy但我仍然在 Log 中收到一个错误,说明和应用程序崩溃。这里在日志错误第 55 行是onDestroy.

这是我的代码

Java 文件

public class DisplayWeather extends Activity {

    String city, date, maximumTemp, minimumTemp, description, weatherImageUrl;
    ListView weatherList;
    List <Bean> bean;
    Bitmap myBitmap, newBitmap;
    CustomBaseAdapter baseAdapter;

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

        bean = new ArrayList<Bean>();
        weatherList = (ListView) findViewById(R.id.lvWeather);

        for(int i=0; i<WeatherHome.arrayList.size(); i++)
        {
            .
                    .

        }


        weatherList.setAdapter(new CustomBaseAdapter(this, bean));
    }


    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        if (baseAdapter.tts != null)
        {
            baseAdapter.tts.stop();
            baseAdapter.tts.shutdown();
        }
        super.onDestroy();
    }

基本适配器类

public class CustomBaseAdapter extends BaseAdapter implements OnInitListener {
    Context context;
    List<Bean> bean;
    ImageView weatherImage;
    TextView weatherDate, weatherCity, weatherMinimum, weatherMaximum, weatherDescription;
    Button buttonSpeak;
    String citySpeak, dateSpeak, descriptionSpeak, maximumSpeak, minimumSpeak, weatherURL;
    TextToSpeech tts;
    Bean userBean;
    Bitmap myBitmap;

    public CustomBaseAdapter(Context context, List<Bean> bean) {
        // TODO Auto-generated constructor stub
        this.context = context;
        this.bean = bean;
        tts = new TextToSpeech(context, null);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return bean.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return bean.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return bean.indexOf(getItem(position));
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

        if(convertView == null)
        {
            convertView = inflater.inflate(R.layout.custom_base_adapter, null);
            weatherImage = (ImageView) convertView.findViewById(R.id.displayImage);
convertView.findViewById(R.id.displayDate);
            buttonSpeak = (Button) convertView.findViewById(R.id.Speak);


        }

        weatherURL = userBean.getImageUrl();

        new ImageDownload().execute();

        Log.i("Executing Rest Line>>>", "Skippedddddd");
        buttonSpeak.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
            String cityName = weatherCity.getText().toString(); 
            String dateValue = weatherDate.getText().toString();
            String maximumValue = weatherMaximum.getText().toString();
            String minimumValue = weatherMinimum.getText().toString();
            String descriptionValue = weatherDescription.getText().toString();

            citySpeak = "Temprature for city "+cityName+"";
            dateSpeak = " on Date "+dateValue+"";
            maximumSpeak = "will be Maximum upto "+maximumValue+" degree ";
            minimumSpeak = " and Minimum upto"+minimumValue+" degree ";
            descriptionSpeak = "and The atmosphere seems to be "+descriptionValue+"";

            speakTempratureValues();
            }
        });

        return convertView;
    }

    private class ImageDownload extends AsyncTask<String, Void, Bitmap>{

        protected Bitmap doInBackground(String... arg0){

            try{
                Log.e("src",weatherURL);
                URL url = new URL(weatherURL);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setDoInput(true);
                connection.connect();
                InputStream input = connection.getInputStream();
                myBitmap = BitmapFactory.decodeStream(input);       
                Log.e("Bitmap","returned");
                return myBitmap;
            }
            catch(Exception e){
                e.printStackTrace();
                return null;
            }

        }

        protected void onPostExecute(Bitmap result){
             if(result!=null)
             {
                Log.i("OnPost>>>", ""+result);
                weatherImage.setImageBitmap(result);
             }

        }
    }

    protected void speakTempratureValues() {
        // TODO Auto-generated method stub
        tts.setSpeechRate(-4);
        tts.speak(citySpeak, TextToSpeech.QUEUE_FLUSH, null);
        tts.speak(dateSpeak, TextToSpeech.QUEUE_ADD, null);
        tts.speak(maximumSpeak, TextToSpeech.QUEUE_ADD, null);
        tts.speak(minimumSpeak, TextToSpeech.QUEUE_ADD, null);
        tts.speak(descriptionSpeak, TextToSpeech.QUEUE_ADD, null);
        tts.speak("Thank You", TextToSpeech.QUEUE_ADD, null);
    }


    @Override
    public void onInit(int status) {
        // TODO Auto-generated method stub
        if(status==TextToSpeech.SUCCESS){
            int result = tts.setLanguage(Locale.getDefault());

            if (result == TextToSpeech.LANG_MISSING_DATA
                    || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                Log.e("TTS", "This Language is not supported");
            }
            else{

                speakTempratureValues();
            }
        }
        else{
            Log.e("TTS", "Initialization Failed");
        }
    }

}
4

2 回答 2

1

看起来您可能在 AsyncTask 完成下载图像之前在 getView 方法中返回了您的 convertView。您可以改用线程,并使用线程连接方法,以便您的应用程序等待下载图像吗?对于 AsyncTask,您通常会使用进度对话框,直到任务完成,但我认为您不能在适配器内部执行此操作。

如何替换这个:

new ImageDownload().execute();

有了这个:

new Thread(new Runnable() {
public void run() {
  try{
            Log.e("src",weatherURL);
            URL url = new URL(weatherURL);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            myBitmap = BitmapFactory.decodeStream(input);       
            Log.e("Bitmap","returned");
            return myBitmap;
        }
        catch(Exception e){
            e.printStackTrace();
            return null;
        }}
}).join();

然后显然摆脱了 ImageDownload 类。我只是把这个扔给你,没有测试它或任何东西。我认为这应该让你更接近。

于 2014-04-17T20:37:02.063 回答
0

路易斯建议的是正确的方法。我相信这会奏效。它不适合您的原因不确定,但请尝试以下方式:

Runnable runnable = new Runnable() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    try{
                        Log.e("src",weatherURL);
                        URL url = new URL(weatherURL);
                        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                        connection.setDoInput(true);
                        connection.connect();
                        InputStream input = connection.getInputStream();
                        myBitmap = BitmapFactory.decodeStream(input);       
                        Log.e("Bitmap","returned"+myBitmap);

                        if(myBitmap!=null)
                     {
                        Log.i("OnPost>>>", ""+myBitmap);
                        weatherImage.setImageBitmap(myBitmap);
                     }
                    }
                    catch(Exception e){
                        e.printStackTrace();

                    }

                }
            };

            Thread t = new Thread(runnable);
            t.start();
            try {
                t.join();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

可能您缺少 start 方法,并且您可能没有调用它。我希望这项工作。

于 2014-04-18T09:13:55.763 回答