Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 从url下载图像时出错_Android_Image_Url_Download_Imageview - Fatal编程技术网

Android 从url下载图像时出错

Android 从url下载图像时出错,android,image,url,download,imageview,Android,Image,Url,Download,Imageview,我试图从JSON数据中获取url,并将其用于下载图像,将其设置为imageView,但出现以下错误: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageBitmap(android.graphics.Bitmap)' on a null object reference

我试图从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是正确的:

我的简化代码是:

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;
    }
}
公务舱旅行扩展了活动{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
mHandler.postDelayed(可运行,100);
}
private Runnable Runnable=new Runnable(){
公开募捐{
初始化窗口();
mHandler.postDelayed(可运行,100);
如果(gps>延迟){
mHandler.removeCallbacks(可运行);
}
}
};
私人PopupWindow pwindo;
私有void initiatePopupWindow(){
试一试{
LayoutFlater充气器=(LayoutFlater)context.getSystemService(context.LAYOUT\u充气器\u服务);
视图布局=充气机。充气(R.layout.screen_弹出,
(视图组)findViewById(R.id.popup_元素);
pwindo=新的PopupWindow(布局,700,1200,真);
pwindo.setBackgroundDrawable(新的BitmapDrawable());
pwindo.显示位置(布局,重心,0,0);
resId=getResources().getIdentifier(单词[number*6].toLowerCase(),“drawable”,getPackageName());
((ImageView)pwindo.getContentView().findViewById(R.id.ImageView)).setImageResource(resId);
((TextView)pwindo.getContentView().findViewById(R.id.txtView)).setText(单词[3+数字*6]);
字符串事件=((新事件()).getEventData(getApplicationContext(),单词[1+数字*6],单词[2+数字*6]);
浮点数=0;
试一试{
JSONObject jsonRootObject=新的JSONObject(事件);
JSONObject mainObj=jsonRootObject.getJSONObject(“分页”);
count=Float.valueOf(mainObj.optString(“对象计数”);
}catch(JSONException e){e.printStackTrace();}
如果(计数>0)
{
字符串说明=”;
JSONObject jsonRootObject=新的JSONObject(事件);
JSONArray JSONArray=jsonRootObject.optJSONArray(“事件”);
JSONObject JSONObject=jsonArray.getJSONObject(0);
JSONObject信息=JSONObject.getJSONObject(“名称”);
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);
新建LoadImagefromUrl().execute(文本);
}
}捕获(例外e){
e、 printStackTrace();
}
}
私有类LoadImagefromUrl扩展了AsyncTask{
ImageView ivPreview=null;
@凌驾
受保护位图doInBackground(对象…参数){
this.ivPreview=(ImageView)findViewById(R.id.eventsImage);
字符串url=(字符串)参数[0];
System.out.println(url);
返回加载位图(url);
}
@凌驾
受保护的void onPostExecute(位图结果){
super.onPostExecute(结果);
ivPreview.setImageBitmap(结果);
}
}
公共位图加载位图(字符串url){
URL newurl=null;
位图=空;
试一试{
newurl=新URL(URL);
bitmap=BitmapFactory.decodeStream(newurl.openConnection().getInputStream());
}捕获(格式错误){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
返回位图;
}
}
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>


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

错误消息告诉您问题的行号。那一行的内容是什么?由于您已经减少了问题中的代码,我们无法从您发布的内容中分辨出来。为什么您没有
this.ivPreview=(ImageView)findViewById(R.id.eventsImage)
主线程中
比在
doInBackground中