我正在为我的应用程序使用Universal Image Loader API,并从服务器下载图像并将它们分配给字符串数组。
问题是,当我调试应用程序时,图像将显示在模拟器上,但是当我尝试运行它时,它会在这段代码上给出空指针异常。
@Override
public int getCount() {
return IMAGE_URLS.length;
}
按钮点击我将我的应用程序重定向到主页活动
public void onImageGridClick(View view) {
new DownloadJSON().execute();
Intent intent = new Intent(this, SimpleImageActivity.class);
intent.putExtra(Constants.Extra.FRAGMENT_INDEX, ImageGridFragment.INDEX);
startActivity(intent);
}
我在 onImageGridClick()上调用此asynctask。
这是我的asynctask
private class DownloadJSON extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
jsonobject = jsonParser.MakehttpRequest(URL_PHOTO_GALLERY, "POST");
try {
JSONArray arr = new JSONArray();
arr = jsonobject.getJSONArray("hrm_staff");
stringArray = new String[arr.length()];
Log.d("try jasonarray->", stringArray.toString());
for (int i = 0; i < arr.length(); i++) {
stringArray[i]= arr.getJSONObject(i).getString("ppic");
}
} catch (JSONException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void args) {
Images = stringArray;
}
}
这是我的ImageGridFragment类
public class ImageGridFragment extends AbsListViewBaseFragment {
public static final int INDEX = 1;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fr_image_grid, container, false);
listView = (GridView) rootView.findViewById(R.id.grid);
((GridView) listView).setAdapter(new ImageAdapter(getActivity()));
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
startImagePagerActivity(position);
}
});
return rootView;
}
private static class ImageAdapter extends BaseAdapter {
protected ImageLoader imageLoader;
private static final String[] IMAGE_URLS = Constants.IMAGES;
private LayoutInflater inflater;
private DisplayImageOptions options;
ImageAdapter(Context context) {
imageLoader = ImageLoader.getInstance();
imageLoader.init(ImageLoaderConfiguration.createDefault(context));
inflater = LayoutInflater.from(context);
options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.ic_stub)
.showImageForEmptyUri(R.drawable.ic_empty)
.showImageOnFail(R.drawable.ic_error)
.cacheInMemory(true)
.cacheOnDisk(true)
.considerExifParams(true)
.bitmapConfig(Bitmap.Config.RGB_565)
.build();
}
@Override
public int getCount() {
return IMAGE_URLS.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 ViewHolder holder;
View view = convertView;
if (view == null) {
view = inflater.inflate(R.layout.item_grid_image, parent, false);
holder = new ViewHolder();
assert view != null;
holder.imageView = (ImageView) view.findViewById(R.id.image);
holder.progressBar = (ProgressBar) view.findViewById(R.id.progress);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
Log.d("Image Urls", ""+IMAGE_URLS);
ImageLoader.getInstance()
.displayImage(IMAGE_URLS[position], holder.imageView, options, new SimpleImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {
holder.progressBar.setProgress(0);
holder.progressBar.setVisibility(View.VISIBLE);
}
@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
holder.progressBar.setVisibility(View.GONE);
}
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
holder.progressBar.setVisibility(View.GONE);
}
}, new ImageLoadingProgressListener() {
@Override
public void onProgressUpdate(String imageUri, View view, int current, int total) {
holder.progressBar.setProgress(Math.round(100.0f * current / total));
}
});
return view;
}
}
static class ViewHolder {
ImageView imageView;
ProgressBar progressBar;
}
}
和字符串数组声明的常量类
public final class Constants {
public static final String[] IMAGES = HomeActivity.Images;
private Constants() {
}
public static class Config {
public static final boolean DEVELOPER_MODE = false;
}
public static class Extra {
public static final String FRAGMENT_INDEX = "com.nostra13.example.universalimageloader.FRAGMENT_INDEX";
public static final String IMAGE_POSITION = "com.nostra13.example.universalimageloader.IMAGE_POSITION";
}
这是logcat:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nostra13.universalimageloader/com.nostra13.universalimageloader.sample.activity.SimpleImageActivity}: java.lang.NullPointerException: Attempt to get length of null array
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to get length of null array
at com.nostra13.universalimageloader.sample.fragment.ImageGridFragment$ImageAdapter.getCount(ImageGridFragment.java:92)
at android.widget.GridView.setAdapter(GridView.java:206)
at com.nostra13.universalimageloader.sample.fragment.ImageGridFragment.onCreateView(ImageGridFragment.java:53)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:551)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1237)
at android.app.Activity.performStart(Activity.java:6253)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
12-17 12:31:27.602 3656-3709 / com.nostra13.universalimageloader D / JSON Parser:
result: {"hrm_staff":[{"ppic":"http:\/\/www.usplhubli.96.lt\/hrm\/prof_pic\/sb.jpg"},{"ppic":"http:\/\/www.usplhubli.96.lt\/hrm\/prof_pic\/ulogo.png"},{"ppic":"http:\/\/www.usplhubli.96.lt\/hrm\/prof_pic\/p1.jpg"},{"ppic":"http:\/\/www.usplhubli.96.lt\/hrm\/prof_pic\/p3.jpg"},{"ppic":"http:\/\/www.usplhubli.96.lt\/hrm\/prof_pic\/p6.jpg"},{"ppic":"http:\/\/www.usplhubli.96.lt\/hrm\/prof_pic\/p4.jpg"},{"ppic":"http:\/\/www.usplhubli.96.lt\/hrm\/prof_pic\/p5.jpg"},{"ppic":"http:\/\/www.usplhubli.96.lt\/hrm\/prof_pic\/p8.png"},{"ppic":"http:\/\/www.usplhubli.96.lt\/hrm\/prof_pic\/p13.jpg"}],"success":1,"message":"display records"}
12-17 12:59:42.225 6343-6450/com.nostra13.universalimageloader D/try jasonarray->: [{"ppic":"http:\/\/www.usplhubli.96.lt\/hrm\/prof_pic\/sb.jpg"},{"ppic":"http:\/\/www.usplhubli.96.lt\/hrm\/prof_pic\/ulogo.png"},{"ppic":"http:\/\/www.usplhubli.96.lt\/hrm\/prof_pic\/p1.jpg"},{"ppic":"http:\/\/www.usplhubli.96.lt\/hrm\/prof_pic\/p3.jpg"},{"ppic":"http:\/\/www.usplhubli.96.lt\/hrm\/prof_pic\/p6.jpg"},{"ppic":"http:\/\/www.usplhubli.96.lt\/hrm\/prof_pic\/p4.jpg"},{"ppic":"http:\/\/www.usplhubli.96.lt\/hrm\/prof_pic\/p5.jpg"},{"ppic":"http:\/\/www.usplhubli.96.lt\/hrm\/prof_pic\/p8.png"},{"ppic":"http:\/\/www.usplhubli.96.lt\/hrm\/prof_pic\/p13.jpg"}]
任何帮助都会非常受欢迎。我对此感到震惊。
答案 0 :(得分:1)
基本上你没有使用ImageLoader的问题,这是正确的,但问题是下面一行:
private static final String[] IMAGE_URLS = Constants.IMAGES;
是否可以以字符串数组的形式将数据分配给 Constants.IMAGES 数组。
修改1:
初始化Imageloader:
imageLoader.init(ImageLoaderConfiguration.createDefault(context));
希望这是有道理的。