我正在尝试从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)
我的缩减代码是:
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>
答案 0 :(得分:0)
在Activity中声明您的 ImageView ivPreview 变量。 onPostExecute在运行该线程的Activity上执行,因此 ivPreview 将为null。