2013-12-23 62 views
0

我试图将ImageView的源设置为位于Google静态地图的URL上的图像。此URL的一个例子是这样... LINK尝试使用Google静态地图设置ImageView时出现NullPointerException

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 

我看了一下这个问题多StackOverflow的岗位,所有这些问题都得到了回答的代码段,对于其他人的工作,但由于某种原因不是我。这是我的代码。 (我遗漏了与该问题无关的部分)。该NullPointerException异常发生在行mapView.setImageBitmap(result);

主类

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); 
    } 

} 

} 

布局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> 

任何人都可以提供一些线索到这个?

+0

那么,mapView可能是空的。 – Eran

+0

这是一个显而易见的事情,但最有可能的是:'findViewById(R.id.imageView1)'返回'null'。我不知道为什么,因为你已经在你的布局中得到它,但玩弄它,它应该工作。 –

+0

必须在ui线程上调用AsyncTask。但你有它doInbackground – Raghunandan

回答

0

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

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

AQuery aq = new AQuery(context); 
aq.id(R.id.imageview).image(url); 
相关问题