我正在尝试使用通用图像加载器在网格视图中获取图像。但是,一旦我创建了这段代码,日志开始就会出现一些错误
04-03 21:45:12.653: E/AndroidRuntime(1271): FATAL EXCEPTION: main
04-03 21:45:12.653: E/AndroidRuntime(1271): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nostra13.example.universalimageloader/com.nostra13.example.universalimageloader.ImageGridActivity}: java.lang.NullPointerException
04-03 21:45:12.653: E/AndroidRuntime(1271): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
04-03 21:45:12.653: E/AndroidRuntime(1271): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
04-03 21:45:12.653: E/AndroidRuntime(1271): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-03 21:45:12.653: E/AndroidRuntime(1271): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
04-03 21:45:12.653: E/AndroidRuntime(1271): at android.os.Handler.dispatchMessage(Handler.java:99)
04-03 21:45:12.653: E/AndroidRuntime(1271): at android.os.Looper.loop(Looper.java:123)
04-03 21:45:12.653: E/AndroidRuntime(1271): at android.app.ActivityThread.main(ActivityThread.java:3683)
04-03 21:45:12.653: E/AndroidRuntime(1271): at java.lang.reflect.Method.invokeNative(Native Method)
04-03 21:45:12.653: E/AndroidRuntime(1271): at java.lang.reflect.Method.invoke(Method.java:507)
04-03 21:45:12.653: E/AndroidRuntime(1271): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-03 21:45:12.653: E/AndroidRuntime(1271): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-03 21:45:12.653: E/AndroidRuntime(1271): at dalvik.system.NativeStart.main(Native Method)
04-03 21:45:12.653: E/AndroidRuntime(1271): Caused by: java.lang.NullPointerException
04-03 21:45:12.653: E/AndroidRuntime(1271): at com.nostra13.example.universalimageloader.ImageGridActivity$ImageAdapter.getCount(ImageGridActivity.java:78)
04-03 21:45:12.653: E/AndroidRuntime(1271): at android.widget.GridView.setAdapter(GridView.java:131)
04-03 21:45:12.653: E/AndroidRuntime(1271): at com.nostra13.example.universalimageloader.ImageGridActivity.onCreate(ImageGridActivity.java:59)
04-03 21:45:12.653: E/AndroidRuntime(1271): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-03 21:45:12.653: E/AndroidRuntime(1271): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
这里的代码是这样的 Constants.java
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
//import com.example.parseimage.JSONParser;
public final class Constants {
public static String url = "http://ensign119.com/assets/image11.php";
public static String[] IMAGES;
protected static final String TAG_PRODUCTS = "image_path";
protected static final String TAG_CID = "file_id";
public static final String TAG_NAME = "file_name";
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
JSONParser jParser = new JSONParser();
JSONArray products = null;
private Constants() {
JSONObject json = jParser.getJSONFromUrl(url);
Log.d("path_parsing", "before parsing");
try {
// Getting Array of Contacts
products = json.getJSONArray(TAG_PRODUCTS);
// looping through All Contacts
for(int i = products.length()-1; i >=0; i--){
JSONObject c = products.getJSONObject(i);
// Storing each json item in variable
String cid = c.getString(TAG_CID).toString();
String name = c.getString(TAG_NAME);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_CID, cid);
map.put(TAG_NAME, name);
// adding HashList to ArrayList
mylist.add(map);
Log.d("mylist_value", mylist.toString());
}
} catch (JSONException e) {
e.printStackTrace();
}
Log.d("path_parsing", "after parsing");
IMAGES = new String[mylist.size()];
for(int i=0; i<mylist.size(); i++){
IMAGES[i] = mylist.get(i).get("file_name");
}
for(String path: IMAGES){
Log.d("path", path.toString());
}
}
public static class Config {
public static final boolean DEVELOPER_MODE = false;
}
public static class Extra {
public static final String IMAGES = "com.nostra13.example.universalimageloader.IMAGES";
public static final String IMAGE_POSITION = "com.nostra13.example.universalimageloader.IMAGE_POSITION";
}
}
imageGridActivity.java
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import com.nostra13.example.universalimageloader.Constants.Extra;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
public class ImageGridActivity extends AbsListViewBaseActivity {
String[] imageUrls;
DisplayImageOptions options;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ac_image_grid);
Bundle bundle = getIntent().getExtras();
imageUrls = bundle.getStringArray(Extra.IMAGES);
options = new DisplayImageOptions.Builder()
.showStubImage(R.drawable.ic_stub)
.showImageForEmptyUri(R.drawable.ic_empty)
.showImageOnFail(R.drawable.ic_error)
.cacheInMemory()
.cacheOnDisc()
.bitmapConfig(Bitmap.Config.RGB_565)
.build();
listView = (GridView) findViewById(R.id.gridview);
((GridView) listView).setAdapter(new ImageAdapter());
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
startImagePagerActivity(position);
}
});
}
private void startImagePagerActivity(int position) {
Intent intent = new Intent(this, ImagePagerActivity.class);
intent.putExtra(Extra.IMAGES, imageUrls);
intent.putExtra(Extra.IMAGE_POSITION, position);
startActivity(intent);
}
public class ImageAdapter extends BaseAdapter {
@Override
public int getCount() {
return imageUrls.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ImageView imageView;
if (convertView == null) {
imageView = (ImageView) getLayoutInflater().inflate(R.layout.item_grid_image, parent, false);
} else {
imageView = (ImageView) convertView;
}
imageLoader.displayImage(imageUrls[position], imageView, options);
return imageView;
}
}
}
请帮我解决这个问题。我是android的新手。
答案 0 :(得分:0)
我猜你的问题来自这一行:
imageLoader.displayImage(imageUrls[position], imageView, options);
确认imageView
,imageUrls at position
(位置)和options
对象不是NULL
。
答案 1 :(得分:0)
stacktrace中的这一行:
04-03 21:45:12.653: E/AndroidRuntime(1271): Caused by: java.lang.NullPointerException
04-03 21:45:12.653: E/AndroidRuntime(1271): at com.nostra13.example.universalimageloader.ImageGridActivity$ImageAdapter.getCount(ImageGridActivity.java:78)
建议您的getCount
方法返回null,这表示imageUrls
为空,因为imageUrls.length
也将为空,为您提供NullPointerException
另一个问题是你在这里做了什么:
@Override
public Object getItem(int position) {
return null;
}
当Android尝试检索您的视图时,您将其赋予null,这在某些时候也会导致NullPointerException
。如果您不确定自己在做什么,请不要覆盖方法。如果您强制覆盖方法,请确保您确切知道操作系统需要什么。仅仅因为您没有在自己的代码中使用getItem()
,并不意味着Android不会使用它。
答案 2 :(得分:0)
假设您的imageUrls[]
数组不为空或为空,我认为您的问题位于getItem( int position )
。尝试更改如下。
public class ImageAdapter extends BaseAdapter {
@Override
public int getCount() {
return imageUrls.length;
}
@Override
public Object getItem(int position) {
return imageUrls.get(i); // change here like this
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ImageView imageView;
if (convertView == null) {
imageView = (ImageView) getLayoutInflater().inflate(R.layout.item_grid_image, parent, false);
} else {
imageView = (ImageView) convertView;
}
imageLoader.displayImage(imageUrls[position], imageView, options);
return imageView;
}
}
}