Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.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
Java 尝试使用Google静态地图设置ImageView时出现NullPointerException_Java_Android_Android Imageview_Google Static Maps - Fatal编程技术网

Java 尝试使用Google静态地图设置ImageView时出现NullPointerException

Java 尝试使用Google静态地图设置ImageView时出现NullPointerException,java,android,android-imageview,google-static-maps,Java,Android,Android Imageview,Google Static Maps,我试图将ImageView的源设置为位于Google静态地图URL的图像。这个url的一个例子是 我已经阅读了多篇关于这个问题的StackOverflow帖子,所有这些问题都是用一些代码来回答的,这些代码对其他人有用,但出于某种原因,我没有。这是我的密码。(我遗漏了与问题无关的部分)。NullPointerException发生在第mapView.setImageBitmap(结果)行 主类 public class CoffeeResultActivity extends Activity {

我试图将ImageView的源设置为位于Google静态地图URL的图像。这个url的一个例子是

我已经阅读了多篇关于这个问题的StackOverflow帖子,所有这些问题都是用一些代码来回答的,这些代码对其他人有用,但出于某种原因,我没有。这是我的密码。(我遗漏了与问题无关的部分)。NullPointerException发生在第
mapView.setImageBitmap(结果)行

主类

public class CoffeeResultActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    expListView = (ExpandableListView) findViewById(R.id.lvExp);
    String userChoice = (String) getIntent().getExtras().get("userChoice");

    //CALL THE ASYNC TASK HERE
    new RetreiveSearchResults().execute(userChoice);

}

class RetreiveSearchResults extends AsyncTask<String, Void, Void> {

    ProgressDialog progressDialog;

    @Override
    protected void onPreExecute() {
        progressDialog = new ProgressDialog(CoffeeResultActivity.this);
        progressDialog.setMessage("Searching for caffeine...");
        progressDialog.setIndeterminate(false);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setCancelable(true);
        progressDialog.show();
    }

    @Override
    protected Void doInBackground(String... terms) {        

        /*
            .........
        */

        try {


            //GETS IMAGE
            ImageLoadingTask task = new ImageLoadingTask();
                                            task.execute("http://maps.google.com/maps/api/staticmap?center=37.386052,-122.083851&zoom=13&markers=size:mid%7Ccolor:blue%7C37.386052,-122.083851&path=color:0x0000FF80%7Cweight:5%7C37.40276,-122.06360&size=220x150&sensor=false");

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   


        return null;
    }

    protected void onPostExecute(Void result) {

        listAdapter = new ExpandableListAdapter(CoffeeResultActivity.this, businessNames, businessInfo);
        listAdapter.notifyDataSetChanged();

        // setting list adapter
        expListView.setAdapter(listAdapter);

        progressDialog.dismiss();   

    }
}

public class ImageLoadingTask extends AsyncTask<String, Void, Bitmap> {
    @Override
    protected Bitmap doInBackground(String... stringURL) {
        Bitmap bmp = null;
        try {
            URL url = new URL(stringURL[0]);

            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setDoInput(true);
            conn.connect();
            InputStream is = conn.getInputStream();
            BitmapFactory.Options options = new BitmapFactory.Options();

            bmp = BitmapFactory.decodeStream(is, null, options);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return bmp;
    }

    @Override
    protected void onPostExecute(Bitmap result) {

        ImageView mapView = (ImageView) findViewById(R.id.imageView1);
        mapView.setImageBitmap(result);
        super.onPostExecute(result);
    }

}

}
public class CoffeeResultActivity扩展活动{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
expListView=(ExpandableListView)findViewById(R.id.lvExp);
字符串userChoice=(字符串)getIntent().getExtras().get(“userChoice”);
//在这里调用异步任务
新建RetreiveSearchResults().execute(用户选择);
}
类RetreiveSearchResults扩展了AsyncTask{
进行对话进行对话;
@凌驾
受保护的void onPreExecute(){
progressDialog=新建progressDialog(CoffeeResultActivity.this);
setMessage(“搜索咖啡因…”);
progressDialog.setUndeterminate(false);
progressDialog.setProgressStyle(progressDialog.STYLE_微调器);
progressDialog.setCancelable(真);
progressDialog.show();
}
@凌驾
受保护的Void doInBackground(字符串…术语){
/*
.........
*/
试一试{
//获取图像
ImageLoadingTask任务=新建ImageLoadingTask();
任务。执行(“http://maps.google.com/maps/api/staticmap?center=37.386052,-122.083851&zoom=13&markers=size:mid%7Ccolor:blue%7C37.386052,-122.083851&path=color:0x0000FF80%7Cweight:5%7C37.40276,-122.06360&size=220x150&sensor=false”);
}捕获(JSONException e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}   
返回null;
}
受保护的void onPostExecute(void结果){
listAdapter=新的可扩展listAdapter(CoffeeResultActivity.this、BusinessName、businessInfo);
listAdapter.notifyDataSetChanged();
//设置列表适配器
expListView.setAdapter(listAdapter);
progressDialog.disclose();
}
}
公共类ImageLoadingTask扩展异步任务{
@凌驾
受保护位图doInBackground(字符串…字符串URL){
位图bmp=null;
试一试{
URL URL=新URL(stringURL[0]);
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
conn.setDoInput(真);
连接();
InputStream is=conn.getInputStream();
BitmapFactory.Options=new-BitmapFactory.Options();
bmp=BitmapFactory.decodeStream(is,null,options);
}捕获(例外e){
e、 printStackTrace();
}
返回bmp;
}
@凌驾
受保护的void onPostExecute(位图结果){
ImageView mapView=(ImageView)findViewById(R.id.imageView1);
setImageBitmap(结果);
super.onPostExecute(结果);
}
}
}
布局XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="55dip"
    android:orientation="vertical" >

<TextView
    android:id="@+id/lblListItem"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="15dp"
    android:paddingTop="10dp"
    android:paddingBottom="10dp"
    android:linksClickable="true"
    android:paddingLeft="8dp" />

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher" />

</LinearLayout>


有人能解释一下吗?

为什么不使用图像加载器库作为答案呢。您可以找到一个.jar库,可以直接粘贴到libs文件夹中。它也支持缓存

将图像从url加载到imageview的示例用法

AQuery aq = new AQuery(context);
aq.id(R.id.imageview).image(url);

mapView可能是空的。这很明显,但最有可能是:
findViewById(R.id.imageView1)
返回
null
。我不知道为什么,因为您的布局中已经有了它,但可以随意使用它,它应该可以工作。必须在ui线程上调用AsyncTask。但是你有它在Backgroundi put
mapView=(ImageView)findViewById(R.id.imageView1)在onCreate中运行。谢谢
AQuery aq = new AQuery(context);
aq.id(R.id.imageview).image(url);