我正在尝试根据this教程实现一个包含从网络加载的图片的图库。 但我有一个空指针错误。图像网址已成功从数据库中提取。 继承我的主要课程
List<String> photos;
Gallery gallery;
ImageView image;
private static final String url_photos = "http://xxxxxxxxx/get_photos.php";
// JSON Node names
private static final String TAG_PHOTO = "name";
private static final String TAG_SUCCESS = "success";
private static final String TAG_RESPONSE = "photos";
private static final String KEY_DESCRIPTION = "advert_description";
private static final String KEY_ID = "advert_id";
private static final String KEY_RATE = "advert_rate";
private static final String KEY_LOCATION = "advert_location";
private static final String KEY_FUEL = "advert_fuel";
private static final String KEY_ENGINE = "advert_engine";
private static final String KEY_MODEL = "advert_model";
private static final String KEY_MAKE = "advert_make";
private static final String KEY_TRANSMISSION = "advert_transmission";
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_cardetails,
container, false);
photos = new ArrayList<>();
make = (TextView) rootView.findViewById(R.id.cardetails_textView_make);
model = (TextView) rootView
.findViewById(R.id.cardetails_textView_model);
transmission = (TextView) rootView
.findViewById(R.id.cardetails_textView_transmission);
engine = (TextView) rootView
.findViewById(R.id.cardetails_textView_engine);
seating = (TextView) rootView
.findViewById(R.id.cardetails_textView_seating);
location = (TextView) rootView
.findViewById(R.id.cardetails_textView_location);
rates = (TextView) rootView
.findViewById(R.id.cardetails_textView_rates);
description = (TextView) rootView
.findViewById(R.id.cardetails_textView_description);
fuel = (TextView) rootView.findViewById(R.id.cardetails_textView_fuel);
gallery = (Gallery) rootView
.findViewById(R.id.cardetails_gallery_photos);
image = (ImageView) rootView
.findViewById(R.id.cardetails_imageView_image);
new GetImages().execute();
Bundle bundle = this.getArguments();
if (bundle != null) {
advert_description = bundle.getString(KEY_DESCRIPTION);
advert_price = bundle.getString(KEY_RATE);
advert_location = bundle.getString(KEY_LOCATION);
advert_id = bundle.getInt(KEY_ID);
advert_transmission = bundle.getString(KEY_TRANSMISSION);
advert_fuel = bundle.getString(KEY_FUEL);
advert_engine = bundle.getString(KEY_ENGINE);
advert_model = bundle.getString(KEY_MODEL);
advert_make = bundle.getString(KEY_MAKE);
}
description.setText(advert_description);
rates.setText(advert_price);
location.setText(advert_location);
make.setText(advert_make);
model.setText(advert_model);
fuel.setText(advert_fuel);
engine.setText(advert_engine);
transmission.setText(advert_transmission);
rates.setText(advert_price);
return rootView;
}
protected class GetImages extends
AsyncTask<CarDetailsFragment, Void, CarDetailsFragment> {
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(getActivity());
progressDialog.setMessage("");
progressDialog.show();
}
@Override
protected CarDetailsFragment doInBackground(
CarDetailsFragment... params) {
photos = getPhotos(advert_id);
return null;
}
@Override
protected void onPostExecute(CarDetailsFragment params) {
super.onPostExecute(params);
progressDialog.dismiss();
Log.i("", photos.size() + "");
gallery.setAdapter(new ImageAdapter(getActivity()
.getApplicationContext(), photos));
}
}
public List<String> getPhotos(int id) {
InputStream is = null;
String result = "";
List<String> photos_url = new ArrayList<>();
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("advert_id", String
.valueOf(id)));
// http post
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url_photos);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in connection " + e.toString());
// results.setText("Error in connection");
}
// convert response to string
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
Log.i("", result);
try {
JSONObject json = new JSONObject(result);
// Checking for SUCCESS TAGlistener
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Products
JSONArray urls = json.getJSONArray(TAG_RESPONSE);
// looping through All Products
for (int i = 0; i < urls.length(); i++) {
JSONObject c = urls.getJSONObject(i);
// Storing each json item in variable
String url = c.getString(TAG_PHOTO);
photos_url.add(url);
}
}
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
return photos_url;
}
ImageAdapter类
public class ImageAdapter extends BaseAdapter {
private List<?> listData;
ImageLoader imageLoader;
private LayoutInflater layoutInflater;
private Context mContext;
private static final String URL = "http://safirisha.co.ke/photos/";
public ImageAdapter(Context context, List<?> listData) {
this.listData = listData;
layoutInflater = LayoutInflater.from(context);
imageLoader = new ImageLoader(context);
mContext = context;
}
@Override
public int getCount() {
return listData.size();
}
@Override
public Object getItem(int position) {
return listData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView image = new ImageView(mContext);
// i.setImageResource(mThumbIds[position]);
image.setAdjustViewBounds(true);
image.setLayoutParams(new Gallery.LayoutParams(50, 50));
// i.setBackgroundResource(R.drawable.picture_frame);
imageLoader.DisplayImage(URL + listData.get(position), image);
return null;
}
}
我的logcat
07-17 20:18:33.286: E/AndroidRuntime(23603): FATAL EXCEPTION: main
07-17 20:18:33.286: E/AndroidRuntime(23603): java.lang.NullPointerException
07-17 20:18:33.286: E/AndroidRuntime(23603): at android.widget.Gallery.setUpChild(Gallery.java:868)
07-17 20:18:33.286: E/AndroidRuntime(23603): at android.widget.Gallery.makeAndAddView(Gallery.java:847)
07-17 20:18:33.286: E/AndroidRuntime(23603): at android.widget.Gallery.layout(Gallery.java:645)
07-17 20:18:33.286: E/AndroidRuntime(23603): at android.widget.Gallery.onLayout(Gallery.java:346)
07-17 20:18:33.286: E/AndroidRuntime(23603): at android.view.View.layout(View.java:11278)
07-17 20:18:33.286: E/AndroidRuntime(23603): at android.view.ViewGroup.layout(ViewGroup.java:4224)
07-17 20:18:33.286: E/AndroidRuntime(23603): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:925)
07-17 20:18:33.286: E/AndroidRuntime(23603): at android.view.View.layout(View.java:11278)
07-17 20:18:33.286: E/AndroidRuntime(23603): at android.view.ViewGroup.layout(ViewGroup.java:4224)
07-17 20:18:33.286: E/AndroidRuntime(23603): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
07-17 20:18:33.286: E/AndroidRuntime(23603): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)
07-17 20:18:33.286: E/AndroidRuntime(23603): at android.widget.LinearLayout.onLayout(LinearLayout.java:1399)
07-17 20:18:33.286: E/AndroidRuntime(23603): at android.view.View.layout(View.java:11278)
07-17 20:18:33.286: E/AndroidRuntime(23603): at android.view.ViewGroup.layout(ViewGroup.java:4224)
07-17 20:18:33.286: E/AndroidRuntime(23603): at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
07-17 20:18:33.286: E/AndroidRuntime(23603): at android.view.View.layout(View.java:11278)
07-17 20:18:33.286: E/AndroidRuntime(23603): at android.view.ViewGroup.layout(ViewGroup.java:4224)
07-17 20:18:33.286: E/AndroidRuntime(23603): at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
07-17 20:18:33.286: E/AndroidRuntime(23603): at android.view.View.layout(View.java:11278)
07-17 20:18:33.286: E/AndroidRuntime(23603): at android.view.ViewGroup.layout(ViewGroup.java:4224)
07-17 20:18:33.286: E/AndroidRuntime(23603): at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:743)
07-17 20:18:33.286: E/AndroidRuntime(23603): at android.view.View.layout(View.java:11278)
07-17 20:18:33.286: E/AndroidRuntime(23603): at android.view.ViewGroup.layout(ViewGroup.java:4224)
07-17 20:18:33.286: E/AndroidRuntime(23603): at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
07-17 20:18:33.286: E/AndroidRuntime(23603): at android.view.View.layout(View.java:11278)
07-17 20:18:33.286: E/AndroidRuntime(23603): at android.view.ViewGroup.layout(ViewGroup.java:4224)
07-17 20:18:33.286: E/AndroidRuntime(23603): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
07-17 20:18:33.286: E/AndroidRuntime(23603): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)
07-17 20:18:33.286: E/AndroidRuntime(23603): at android.widget.LinearLayout.onLayout(LinearLayout.java:1399)
07-17 20:18:33.286: E/AndroidRuntime(23603): at android.view.View.layout(View.java:11278)
07-17 20:18:33.286: E/AndroidRuntime(23603): at android.view.ViewGroup.layout(ViewGroup.java:4224)
07-17 20:18:33.286: E/AndroidRuntime(23603): at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
07-17 20:18:33.286: E/AndroidRuntime(23603): at android.view.View.layout(View.java:11278)
07-17 20:18:33.286: E/AndroidRuntime(23603): at android.view.ViewGroup.layout(ViewGroup.java:4224)
07-17 20:18:33.286: E/AndroidRuntime(23603): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1489)
07-17 20:18:33.286: E/AndroidRuntime(23603): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
07-17 20:18:33.286: E/AndroidRuntime(23603): at android.os.Handler.dispatchMessage(Handler.java:99)
07-17 20:18:33.286: E/AndroidRuntime(23603): at android.os.Looper.loop(Looper.java:137)
07-17 20:18:33.286: E/AndroidRuntime(23603): at android.app.ActivityThread.main(ActivityThread.java:4440)
07-17 20:18:33.286: E/AndroidRuntime(23603): at java.lang.reflect.Method.invokeNative(Native Method)
07-17 20:18:33.286: E/AndroidRuntime(23603): at java.lang.reflect.Method.invoke(Method.java:511)
07-17 20:18:33.286: E/AndroidRuntime(23603): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
07-17 20:18:33.286: E/AndroidRuntime(23603): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
07-17 20:18:33.286: E/AndroidRuntime(23603): at dalvik.system.NativeStart.main(Native Method)
ImageLoader类用于延迟加载图像。我哪里出错?
答案 0 :(得分:1)
首先,尝试在方法getView中返回视图:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView image = new ImageView(mContext);
// i.setImageResource(mThumbIds[position]);
image.setAdjustViewBounds(true);
image.setLayoutParams(new Gallery.LayoutParams(50, 50));
// i.setBackgroundResource(R.drawable.picture_frame);
imageLoader.DisplayImage(URL + listData.get(position), image);
return image;
}
答案 1 :(得分:1)
你的方法
public View getView(int position, View convertView, ViewGroup parent)
不得返回null
。
答案 2 :(得分:0)
protected CarDetailsFragment doInBackground( CarDetailsFragment ... params){ photos = getPhotos(advert_id); return null; } 为什么你的返回null ??