我正在尝试使用URL
库从Universal Image Loader
检索图像。我得到nullpointer exception
。我使用SimpleAdapter
来显示Listview Image
。我做了以下编码。我对这个图书馆非常陌生,因此一步一步指导我。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu_items);
i=getIntent();
imageLoader.getInstance();
ListView salad_list=(ListView)findViewById(R.id.salads);
ListAdapter k=new SimpleAdapter(MenuItemsActivity.this,items,R.layout.menulist,new String[]{"Item_Name","Desc","Currency","Price"},new int[]{R.id.cat_name,R.id.textView1,R.id.textView2,R.id.textView3})
{
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final View v = super.getView(position, convertView, parent);
// TextView picpath=(TextView)v.findViewById(R.id.hide2);
String s="http://166.62.17.208//images_large/caeser-salad.jpg";
ImageView picture=(ImageView)v.findViewById(R.id.imageView1);
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
.threadPriority(Thread.NORM_PRIORITY - 2)
.denyCacheImageMultipleSizesInMemory()
.discCacheFileNameGenerator(new Md5FileNameGenerator())
.tasksProcessingOrder(QueueProcessingType.LIFO)
.enableLogging()
.build();
ImageLoader.getInstance().init(config);
DisplayImageOptions options = new DisplayImageOptions.Builder().cacheOnDisc()
.build();
imageLoader.displayImage(s, picture, options);
return super.getView(position, convertView, parent);
}
};
salad_list.setAdapter(k);
}
我的错误日志如下:
E/AndroidRuntime(3340): FATAL EXCEPTION: main
E/AndroidRuntime(3340): Process: com.alrimal, PID: 3340
E/AndroidRuntime(3340): java.lang.NullPointerException
E/AndroidRuntime(3340): at com.alrimal.MenuItemsActivity$2.getView(MenuItemsActivity.java:166)
E/AndroidRuntime(3340): at android.widget.AbsListView.obtainView(AbsListView.java:2263)
E/AndroidRuntime(3340): at android.widget.ListView.measureHeightOfChildren(ListView.java:1263)
E/AndroidRuntime(3340): at android.widget.ListView.onMeasure(ListView.java:1175)
E/AndroidRuntime(3340): at android.view.View.measure(View.java:16458)
E/AndroidRuntime(3340): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
E/AndroidRuntime(3340): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
E/AndroidRuntime(3340): at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
E/AndroidRuntime(3340): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
E/AndroidRuntime(3340): at android.view.View.measure(View.java:16458)
E/AndroidRuntime(3340): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
E/AndroidRuntime(3340): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
E/AndroidRuntime(3340): at android.view.View.measure(View.java:16458)
E/AndroidRuntime(3340): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
E/AndroidRuntime(3340): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
E/AndroidRuntime(3340): at android.view.View.measure(View.java:16458)
E/AndroidRuntime(3340): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
E/AndroidRuntime(3340): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
E/AndroidRuntime(3340): at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
E/AndroidRuntime(3340): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
E/AndroidRuntime(3340): at android.view.View.measure(View.java:16458)
E/AndroidRuntime(3340): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
E/AndroidRuntime(3340): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
E/AndroidRuntime(3340): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2289)
E/AndroidRuntime(3340): at android.view.View.measure(View.java:16458)
E/AndroidRuntime(3340): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1914)
E/AndroidRuntime(3340): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1111)
E/AndroidRuntime(3340): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1293)
E/AndroidRuntime(3340): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
E/AndroidRuntime(3340): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5582)
E/AndroidRuntime(3340): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
E/AndroidRuntime(3340): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
E/AndroidRuntime(3340): at android.view.Choreographer.doFrame(Choreographer.java:532)
E/AndroidRuntime(3340): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
E/AndroidRuntime(3340): at android.os.Handler.handleCallback(Handler.java:733)
E/AndroidRuntime(3340): at android.os.Handler.dispatchMessage(Handler.java:95)
E/AndroidRuntime(3340): at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(3340): at android.app.ActivityThread.main(ActivityThread.java:4998)
E/AndroidRuntime(3340): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(3340): at java.lang.reflect.Method.invoke(Method.java:515)
E/AndroidRuntime(3340): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
E/AndroidRuntime(3340): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
E/AndroidRuntime(3340): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:6)
您应该在执行任何操作之前配置配置,明智的选择是设置Application类以执行此操作并在清单中提及其名称作为应用程序名称。
public class UILInitiator extends Application {
private static Context context;
@Override
public void onCreate() {
super.onCreate();
context = this;
File cacheDir = StorageUtils.getOwnCacheDirectory(
getApplicationContext(),
"/sdcard/Android/data/random_folder_name_for_cache");
DisplayImageOptions options = new DisplayImageOptions.Builder()
.cacheInMemory(true).cacheOnDisc(true).build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
getApplicationContext()).defaultDisplayImageOptions(options)
.discCache(new FileCountLimitedDiscCache(cacheDir, 100))
.build();
ImageLoader.getInstance().init(config);
}
然后,只使用imageLoader将图像显示为:
ImageView thumbnailImage = (ImageView) view.findViewById(R.id.overview_image);
String imageUrl = "url for image ";
ImageLoader imageLoader = ImageLoader.getInstance();
imageLoader.displayImage(imageUrl, thumbnailImage);
答案 1 :(得分:0)
您可能没有初始化imageLoader
变量。
这样做:
ImageLoader imageLoader = ImageLoader.getInstance().init(config);
然后使用imageLoader.displayImage(s, picture, options);
编辑1:
ImageLoader imageLoader;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu_items);
i=getIntent();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
.threadPriority(Thread.NORM_PRIORITY - 2)
.denyCacheImageMultipleSizesInMemory()
.discCacheFileNameGenerator(new Md5FileNameGenerator())
.tasksProcessingOrder(QueueProcessingType.LIFO)
.enableLogging()
.build();
imageLoader = ImageLoader.getInstance().init(config);
ListView salad_list=(ListView)findViewById(R.id.salads);
ListAdapter k=new SimpleAdapter(MenuItemsActivity.this,items,R.layout.menulist,new String[]{"Item_Name","Desc","Currency","Price"},new int[]{R.id.cat_name,R.id.textView1,R.id.textView2,R.id.textView3})
{
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final View v = super.getView(position, convertView, parent);
// TextView picpath=(TextView)v.findViewById(R.id.hide2);
String s="http://166.62.17.208//images_large/caeser-salad.jpg";
ImageView picture=(ImageView)v.findViewById(R.id.imageView1);
DisplayImageOptions options = new DisplayImageOptions.Builder().cacheOnDisc()
.build();
imageLoader.displayImage(s, picture, options);
return super.getView(position, convertView, parent);
}
};
salad_list.setAdapter(k);
}