我得到了错误:
ImageLoader必须使用配置进行初始化,然后才能使用“非法状态异常”中的错误。
我正在努力将gridview放在片段中以显示图像。根据我的理解,imageLoader
应该首先通过扩展将要扩展的类AbsListViewBaseActivity来初始化BaseActivity
。
这两个类用于初始化imageLoader
。我很弄乱流程,并且在运行时遇到错误,并在两天内进行了处理。我将包括LogCat错误。
我自己的版本
- 主班
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);
}
}
程序的原始执行和流程
- 主班
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);
}
}
}
基础活动
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)
基于这种方法:
图片库
尝试在您的onCreateView内部实现此功能:
活动-
BaseActivity.imageLoader.init(ImageLoaderConfiguration.createDefault(getBaseContext()));
对于片段-
ImageLoader.getInstance().init(ImageLoaderConfiguration.createDefault(getActivity()));
问题内容: 为什么Kotlin对此抱怨: 编译器抱怨在Line中由处理程序再次发布。这在纯Java中确实有效: 问题答案: Kotlin认为一个属性在其初始化程序结束之前尚未初始化,因此即使在lambda中也无法在其自己的初始化程序中使用该属性。这种语义类似于其初始化程序内部局部变量使用的限制。 有几种解决方法: 使用对象表达式可以引用已声明的对象: } 这仅适用于接口作为lambda的替代品,并
问题内容: 我正在尝试建立一个可以相互转换的类型家族。例如,Float和Double可以通过其初始值设定项相互转换。我不想创建一个详尽的初始化列表,以显示每种类型都可以转换为每种其他类型。 我试图在Playground中做类似的事情,但是崩溃了: 我的最终目标不仅仅是做转换,但乘以通过像这样: 这样我就可以表示乘法。 有没有办法做到这一点?我认为问题的一部分是由看起来像这样的结构来解决的,但是我认
问题内容: 我很好奇,无论如何在init方法内部调用一个用于设置类实例属性的方法。本质上,我只是有一个类来对UIView进行子类化,并在init中添加了一些子视图,而其中一些子视图是该类的实例变量。 现在出现了问题,我无法在初始化类的内部属性之前调用super init(在super.init调用时未初始化属性’self.collectionView’),但是在调用之前,我也无法调用自定义方法来初
问题内容: 我不想拥有hibernate.cfg.xml文件。而是我想通过我的代码进行所有配置,如下所示。 我已经尝试了上述方法。但是我面临的问题是hibernate抛出一个异常,说“ ”。 保留文件真的是强制性的吗? 提前致谢 问题答案: 我相信这是由于您对对象的调用。尝试删除该文件,hibernate状态将不会查找不存在的文件。基本上,您是通过对象设置所有必需的属性,因此并不需要告诉Hiber
问题内容: 为什么必须在构造函数完成之前初始化最终变量? 当我编译这段代码时,我得到这样的错误 err:变量q可能尚未初始化 问题答案: 官方理由是,它是由定义的Java语言规范8.3.1.2: 必须在声明该类的每个构造函数的末尾绝对分配一个空白的最终实例变量;否则会发生编译时错误。 空白的final是其声明缺少初始化程序(即您所描述的内容)的final变量。