【问题标题】:Error downloading image from url [duplicate]从网址下载图片时出错[重复]
【发布时间】:2016-04-01 07:43:08
【问题描述】:

我正在尝试从 JSON 数据中获取 url 并使用它来下载图像并将其设置为 imageView,但我收到以下错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageBitmap(android.graphics.Bitmap)' on a null object reference
                                                                         at com.example.seemore.travel$LoadImagefromUrl.onPostExecute(travel.java:308)
                                                                         at com.example.seemore.travel$LoadImagefromUrl.onPostExecute(travel.java:294)

但我确信我得到了正确的 url,因为我打印出来后是:https://img.evbuc.com/https%3A%2F%2Fimg.evbuc.com%2Fhttp%253A%252F%252Fcdn.evbuc.com%252Fimages%252F17170274%252F154231754459%252F1%252Foriginal.jpg%3Frect%3D0%252C84%252C1016%252C508%26s%3Dc24b1445c142e35c5fb65b08e7051304?h=200&w=450&s=7c716f6315512667258d7cee526783b6

我的简化代码是:

public class travel extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mHandler.postDelayed(runnable, 100);
    }

    private Runnable runnable = new Runnable() {
        public void run() {
        initiatePopupWindow();
        mHandler.postDelayed(runnable, 100);
        if (gps > latEnd){
            mHandler.removeCallbacks(runnable);
        }
    }
};

private PopupWindow pwindo;

private void initiatePopupWindow() {
    try {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.screen_popup,
                    (ViewGroup) findViewById(R.id.popup_element));
        pwindo = new PopupWindow(layout, 700, 1200, true);
        pwindo.setBackgroundDrawable(new BitmapDrawable());
        pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);
        resId  = getResources().getIdentifier(words[number*6].toLowerCase(), "drawable", getPackageName());
        ((ImageView)pwindo.getContentView().findViewById(R.id.imageView)).setImageResource(resId);
        ((TextView)pwindo.getContentView().findViewById(R.id.txtView)).setText(words[3 + number * 6]);
        String event= ((new events()).getEventData(getApplicationContext(), words[1 + number * 6],words[2 + number * 6]));
        float count=0;
        try {
            JSONObject  jsonRootObject = new JSONObject(event);
            JSONObject mainObj=jsonRootObject.getJSONObject("pagination");
            count = Float.valueOf(mainObj.optString("object_count"));
            } catch (JSONException e) {e.printStackTrace();}
        if (count>0)
        {
            String description = "";
            JSONObject  jsonRootObject = new JSONObject(event);
            JSONArray jsonArray = jsonRootObject.optJSONArray("events");
            JSONObject jsonObject = jsonArray.getJSONObject(0);
            JSONObject information = jsonObject.getJSONObject("name");
            String text = information.getString("text"); 
            ((TextView)pwindo.getContentView().findViewById(R.id.events)).setText(text);
            information = jsonObject.getJSONObject("logo");
            text = information.getString("url");
            Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG).show();
            (TextView)pwindo.getContentView().findViewById(R.id.events)).setText(text);
               new LoadImagefromUrl( ).execute(text);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    private class LoadImagefromUrl extends AsyncTask< Object, Void, Bitmap > {
        ImageView ivPreview = null;

        @Override
        protected Bitmap doInBackground( Object... params ) {
            this.ivPreview = (ImageView) findViewById(R.id.eventsImage);
            String url = (String) params[0];
            System.out.println(url);
            return loadBitmap( url );
        }

        @Override
        protected void onPostExecute( Bitmap result ) {
            super.onPostExecute( result );
            ivPreview.setImageBitmap( result );
        }
    }

    public Bitmap loadBitmap( String url ) {
        URL newurl = null;
        Bitmap bitmap = null;
        try {
            newurl = new URL( url );
            bitmap = BitmapFactory.decodeStream( newurl.openConnection( ).getInputStream( ) );
        } catch ( MalformedURLException e ) {
            e.printStackTrace( );
        } catch ( IOException e ) {

            e.printStackTrace( );
        }
        return bitmap;
    }
}

xml文件是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/popup_element"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#444444"
android:orientation="vertical"
android:padding="10sp" >

<TextView
    android:id="@+id/txtView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="5sp" />
<ImageView
    android:id="@+id/imageView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
    <TextView
    android:id="@+id/taxi"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />
    <TextView
        android:id="@+id/weather"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
    <TextView
        android:id="@+id/restaurant"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
    <TextView
        android:id="@+id/events"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
    <ImageView
        android:id="@+id/eventsImage"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

【问题讨论】:

  • 错误信息告诉你问题的行号。那一行的内容是什么?由于您在问题中减少了代码,因此我们无法从您发布的内容中判断出来。
  • 为什么你在main thread 中没有this.ivPreview = (ImageView) findViewById(R.id.eventsImage); 而不是在doInBackground

标签: android image url download imageview


【解决方案1】:

改为在 Activity 中声明您的 ImageView ivPreview 变量。 onPostExecute 在运行线程的 Activity 上执行,因此 ivPreview 将为空。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-26
    • 1970-01-01
    • 2020-05-23
    • 2019-02-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多