我想调用一些方法(另一个类)的onCreate()
方法。
我的代码:
new BitmapHandler().loadBitmap(gridViewItem.getImage(), holder.imageItem);
另一个班级的方法:
public void loadBitmap(int resId, ImageView imageView) {
final String imageKey = String.valueOf(resId);
final Bitmap bitmap = getBitmapFromMemCache(imageKey);
if (bitmap != null) {
imageView.setImageBitmap(bitmap);
} else {
BitmapWorkerTask task = new BitmapWorkerTask(imageView);
task.execute(resId);
}
}
要发生的方法NullPointerException
:
public Bitmap getBitmapFromMemCache(String key) {
return mMemoryCache.get(key); //this line occurs Exception
}
NullPointerException
的原因是因为我必须初始化onCreate()。
另一个班级onCreate()
方法:
@Override
public void onCreate(Bundle savedInstanceState) {
Log.e(TAG, "onCreate"); //never called
super.onCreate(savedInstanceState);
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
final int cacheSize = maxMemory / 8;
RetainFragment retainFragment = RetainFragment.findOrCreateRetainFragment(getFragmentManager());
mMemoryCache = retainFragment.mRetainedCache;
if (mMemoryCache == null) {
mMemoryCache = new LruCache<String, Bitmap> (cacheSize) {
@Override
protected int sizeOf(String key, Bitmap bitmap) {
return bitmap.getByteCount() / 1024;
}
};
retainFragment.mRetainedCache = mMemoryCache;
}
}
在这种情况下,如何拨打onCreate()
?或者应该在其他地方初始化?
谢谢大家和TGIF:)