J'ai eu l'erreur:
ImageLoader doit être initialisé avec la configuration avant l'utilisation de l'erreur provenant d'une exception d'état illégale.
J'ai du mal à afficher les images dans mon approche consistant à insérer la grille dans des fragments. En ce qui concerne ma compréhension, la imageLoader
devrait d'abord être initialisée en étendant la classe AbsListViewBaseActivity qui étendra BaseActivity
.
Ces deux classes sont utilisées pour initialiser la imageLoader
. Je me suis bien débrouillé avec le flux, et je reçois des erreurs dans le runtime et je travaille avec ça depuis deux jours. Je vais inclure les erreurs LogCat.
Ma propre version
--Classe principale
import com.nostra13.universalimageloader.core.ImageLoader;
import com.folder.folder.AbsListViewBaseActivity;
public class CollectionDemoActivity extends FragmentActivity{
assignImageLoader newAssign;
static AbsListViewBaseActivity absList;
static ImageAdapter m3;
Oncreate()
{
newAssign = assignImageLoader;
m3 = ImageAdapter();
((GridView) absList.listView).setAdapter(m3);
}
}
/**
* A dummy fragment representing a section of the app, but that simply displays dummy text.
*/
public static class DemoObjectFragment extends Fragment {
public static final String ARG_OBJECT = "object";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.ac_image_grid, container, false);
List<Menu> itemsDatabase = db.getItemsAsArray("items");
List<String> items = new ArrayList<String>();
for (Menu cn : itemsDatabase) {
HashMap<String, String> map = new HashMap<String, String>();
items.add(cn.getImagePath().toString());
values2.add(map);
}
imageUrls = items.toArray(new String[items.size()]);
options2 = new DisplayImageOptions.Builder()
.showStubImage(R.drawable.ic_stub)
.showImageForEmptyUri(R.drawable.ic_empty)
.showImageOnFail(R.drawable.ic_error)
.cacheInMemory(true)
.cacheOnDisc(true)
.bitmapConfig(Bitmap.Config.RGB_565)
.build();
absList.listView = (GridView)rootView.findViewById(R.id.gridview);
((GridView) absList.listView).setAdapter(m3);
absList.listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//startImagePagerActivity(position);
//Toast.makeText(getBaseContext(), "Clieckeed", Toast.LENGTH_SHORT).show();
//Log.d("Inside Onclick:", view.toString());
}
});
return rootView;
}
}
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;
}
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
final ViewHolder holder;
// Assign Values
holder.name.setText(values2.get(position).get(TAG_NAME2 ).toString());
holder.price.setText("Price: " + values2.get(position).get(TAG_PRICE2).toString());
holder.br.setStepSize(1);
holder.br.setFocusable(false);
Log.d("Image Path Value in GridViewdd: ", values2.get(position).get(TAG_IMAGE_NAME2).toString());
newAssign.getImageLoader("Image Path", holder.image, options2);
return view;
}
class ViewHolder {
public TextView name;
public TextView price;
public ImageView image;
public RatingBar br;
}
}
static class assignImageLoader extends AbsListViewBaseActivity{
static ImageLoader mg;
assignImageLoader()
{
mg = imageLoader;
}
public void getImageLoader(String path, ImageView img, DisplayImageOptions options)
{
imageLoader.displayImage(path, img, options);
}
}
Mise en œuvre initiale et déroulement du programme
--Classe principale
public class ImageGridActivity extends AbsListViewBaseActivity {
onCreate()
{
((GridView) listView).setAdapter(new ImageAdapter());
}
public class ImageAdapter extends BaseAdapter {
public View getView(final int position, View convertView, ViewGroup parent) {
imageLoader.displayImage(path, holder.image, options2);
}
}
}
--AbsListViewBaseActivity
public class AbsListViewBaseActivity extends BaseActivity {
protected static final String STATE_PAUSE_ON_SCROLL = "STATE_PAUSE_ON_SCROLL";
protected static final String STATE_PAUSE_ON_FLING = "STATE_PAUSE_ON_FLING";
public AbsListView listView;
protected boolean pauseOnScroll = false;
protected boolean pauseOnFling = true;
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
pauseOnScroll = savedInstanceState.getBoolean(STATE_PAUSE_ON_SCROLL, false);
pauseOnFling = savedInstanceState.getBoolean(STATE_PAUSE_ON_FLING, true);
}
@Override
public void onResume() {
super.onResume();
applyScrollListener();
}
private void applyScrollListener() {
listView.setOnScrollListener(new PauseOnScrollListener(imageLoader, pauseOnScroll, pauseOnFling));
}
@Override
public void onSaveInstanceState(Bundle outState) {
outState.putBoolean(STATE_PAUSE_ON_SCROLL, pauseOnScroll);
outState.putBoolean(STATE_PAUSE_ON_FLING, pauseOnFling);
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem pauseOnScrollItem = menu.findItem(R.id.item_pause_on_scroll);
pauseOnScrollItem.setVisible(true);
pauseOnScrollItem.setChecked(pauseOnScroll);
MenuItem pauseOnFlingItem = menu.findItem(R.id.item_pause_on_fling);
pauseOnFlingItem.setVisible(true);
pauseOnFlingItem.setChecked(pauseOnFling);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.item_pause_on_scroll:
pauseOnScroll = !pauseOnScroll;
item.setChecked(pauseOnScroll);
applyScrollListener();
return true;
case R.id.item_pause_on_fling:
pauseOnFling = !pauseOnFling;
item.setChecked(pauseOnFling);
applyScrollListener();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
BaseActivity
public abstract class BaseActivity extends Activity {
public ImageLoader imageLoader = ImageLoader.getInstance();
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
//Added in the code by me for handling the init error
imageLoader.init(ImageLoaderConfiguration.createDefault(getBaseContext()));
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.item_clear_memory_cache:
imageLoader.clearMemoryCache();
return true;
case R.id.item_clear_disc_cache:
imageLoader.clearDiscCache();
return true;
default:
imageLoader.destroy();
return false;
}
}
}
Logcat:
07-19 11:28:26.909: E/AndroidRuntime(24023): FATAL EXCEPTION: main
07-19 11:28:26.909: E/AndroidRuntime(24023): Java.lang.IllegalStateException: ImageLoader must be init with configuration before using
07-19 11:28:26.909: E/AndroidRuntime(24023): at com.nostra13.universalimageloader.core.ImageLoader.checkConfiguration(ImageLoader.Java:325)
07-19 11:28:26.909: E/AndroidRuntime(24023): at com.nostra13.universalimageloader.core.ImageLoader.displayImage(ImageLoader.Java:170)
07-19 11:28:26.909: E/AndroidRuntime(24023): at com.nostra13.universalimageloader.core.ImageLoader.displayImage(ImageLoader.Java:134)
07-19 11:28:26.909: E/AndroidRuntime(24023): at com.jinisys.restoplusordering.CollectionDemoActivity$assignImageLoader.getImageLoader(CollectionDemoActivity.Java:448)
07-19 11:28:26.909: E/AndroidRuntime(24023): at com.jinisys.restoplusordering.CollectionDemoActivity$ImageAdapter.getView(CollectionDemoActivity.Java:424)
07-19 11:28:26.909: E/AndroidRuntime(24023): at Android.widget.AbsListView.obtainView(AbsListView.Java:2267)
07-19 11:28:26.909: E/AndroidRuntime(24023): at Android.widget.GridView.onMeasure(GridView.Java:1030)
07-19 11:28:26.909: E/AndroidRuntime(24023): at Android.view.View.measure(View.Java:15181)
07-19 11:28:26.909: E/AndroidRuntime(24023): at Android.view.ViewGroup.measureChildWithMargins(ViewGroup.Java:4814)
07-19 11:28:26.909: E/AndroidRuntime(24023): at Android.widget.FrameLayout.onMeasure(FrameLayout.Java:310)
07-19 11:28:26.909: E/AndroidRuntime(24023): at Android.view.View.measure(View.Java:15181)
07-19 11:28:26.909: E/AndroidRuntime(24023): at Android.support.v4.view.ViewPager.onMeasure(ViewPager.Java:1447)
07-19 11:28:26.909: E/AndroidRuntime(24023): at Android.view.View.measure(View.Java:15181)
07-19 11:28:26.909: E/AndroidRuntime(24023): at Android.view.ViewGroup.measureChildWithMargins(ViewGroup.Java:4814)
07-19 11:28:26.909: E/AndroidRuntime(24023): at Android.widget.FrameLayout.onMeasure(FrameLayout.Java:310)
07-19 11:28:26.909: E/AndroidRuntime(24023): at Android.view.View.measure(View.Java:15181)
07-19 11:28:26.909: E/AndroidRuntime(24023): at Android.widget.LinearLayout.measureVertical(LinearLayout.Java:833)
07-19 11:28:26.909: E/AndroidRuntime(24023): at Android.widget.LinearLayout.onMeasure(LinearLayout.Java:574)
07-19 11:28:26.909: E/AndroidRuntime(24023): at Android.view.View.measure(View.Java:15181)
07-19 11:28:26.909: E/AndroidRuntime(24023): at Android.view.ViewGroup.measureChildWithMargins(ViewGroup.Java:4814)
07-19 11:28:26.909: E/AndroidRuntime(24023): at Android.widget.FrameLayout.onMeasure(FrameLayout.Java:310)
07-19 11:28:26.909: E/AndroidRuntime(24023): at com.Android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.Java:2148)
07-19 11:28:26.909: E/AndroidRuntime(24023): at Android.view.ViewRootImpl.performMeasure(ViewRootImpl.Java:1848)
07-19 11:28:26.909: E/AndroidRuntime(24023): at Android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.Java:1100)
07-19 11:28:26.909: E/AndroidRuntime(24023): at Android.view.ViewRootImpl.performTraversals(ViewRootImpl.Java:1273)
07-19 11:28:26.909: E/AndroidRuntime(24023): at Android.view.ViewRootImpl.doTraversal(ViewRootImpl.Java:998)
07-19 11:28:26.909: E/AndroidRuntime(24023): at Android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.Java:4212)
07-19 11:28:26.909: E/AndroidRuntime(24023): at Android.view.Choreographer$CallbackRecord.run(Choreographer.Java:725)
07-19 11:28:26.909: E/AndroidRuntime(24023): at Android.view.Choreographer.doCallbacks(Choreographer.Java:555)
07-19 11:28:26.909: E/AndroidRuntime(24023): at Android.view.Choreographer.doFrame(Choreographer.Java:525)
07-19 11:28:26.909: E/AndroidRuntime(24023): at Android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.Java:711)
07-19 11:28:26.909: E/AndroidRuntime(24023): at Android.os.Handler.handleCallback(Handler.Java:615)
07-19 11:28:26.909: E/AndroidRuntime(24023): at Android.os.Handler.dispatchMessage(Handler.Java:92)
07-19 11:28:26.909: E/AndroidRuntime(24023): at Android.os.Looper.loop(Looper.Java:137)
07-19 11:28:26.909: E/AndroidRuntime(24023): at Android.app.ActivityThread.main(ActivityThread.Java:4745)
07-19 11:28:26.909: E/AndroidRuntime(24023): at Java.lang.reflect.Method.invokeNative(Native Method)
07-19 11:28:26.909: E/AndroidRuntime(24023): at Java.lang.reflect.Method.invoke(Method.Java:511)
07-19 11:28:26.909: E/AndroidRuntime(24023): at com.Android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.Java:786)
07-19 11:28:26.909: E/AndroidRuntime(24023): at com.Android.internal.os.ZygoteInit.main(ZygoteInit.Java:553)
07-19 11:28:26.909: E/AndroidRuntime(24023): at dalvik.system.NativeStart.main(Native Method)
Basant sur cette approche:
Essayez d'implémenter cela dans votre onCreateView:
BaseActivity.imageLoader.init(ImageLoaderConfiguration.createDefault(getBaseContext()));
Cela a fonctionné mieux pour moi.
Mettez ce qui suit dans la onCreate
de votre activité
ImageLoader.getInstance().init(ImageLoaderConfiguration.createDefault(MyActivity.this));
Je pense que vous utilisez l'API Universal Image Loader. Si oui, vous avez oublié de configurer le chargeur d'images dans la classe Application.
Ajoutez la fonction ci-dessous dans votre classe d'application:
public static void initImageLoader(Context context) {
// This configuration tuning is custom. You can tune every option, you may tune some of them,
// or you can create default configuration by the
// ImageLoaderConfiguration.createDefault(this);
// method.
//
ImageLoaderConfiguration.Builder config = new ImageLoaderConfiguration.Builder(context);
config.threadPriority(Thread.NORM_PRIORITY - 2);
config.denyCacheImageMultipleSizesInMemory();
config.diskCacheFileNameGenerator(new Md5FileNameGenerator());
config.diskCacheSize(50 * 1024 * 1024); // 50 MiB
config.tasksProcessingOrder(QueueProcessingType.LIFO);
config.writeDebugLogs(); // Remove for release app
// Initialize ImageLoader with configuration.
ImageLoader.getInstance().init(config.build());
}
Pour plus de détails, consultez cet exemple .
Il suffit de mettre le code suivant dans la classe BaseActivity ou MyApplication:
public ImageLoader getImageLoader() {
if (mImageLoader == null) {
mImageLoader = ImageLoader.getInstance();
mImageLoader.init(ImageLoaderConfiguration.createDefault(this));
}
return this.mImageLoader;
}
Ensuite, chaque fois que vous souhaitez utiliser le chargeur d'images, créez une nouvelle instance d'ImageLoader, comme ci-dessous:
private ImageLoader imageLoader = MyApplication.getInstance().getImageLoader();
Ou si vous prenez BaseActivitiy
entrez le code ici:
private ImageLoader imageLoader = BaseActivity.getInstance().getImageLoader();