0
Google 정적지도의 URL에있는 이미지로 ImageView의 소스를 설정하려고합니다. 이 URL의 예는이 될 것입니다 ... LINKGoogle 정적지도로 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>
사람이에 도움이 되거 수 있습니까?
음, mapView는 아마도 null 일 수 있습니다. – Eran
분명히 분명한 사실이지만, findViewById (R.id.imageView1)는'null'을 반환합니다. 나는 왜 당신이 당신의 레이아웃에서 그것을 가지고 있지만 그것으로 놀고 그것이 작동해야하기 때문에 왜 모르겠다. –
ui 스레드에서 AsyncTask를 호출해야합니다. 하지만 doInbackground를 가지고 있습니다. – Raghunandan