画廊部队关闭使用片段 [英] Gallery Force Close using Fragment
问题描述
我得到一个强制关闭,当我启动画廊片段。这不是从<一的code href=\"http://stackoverflow.com/questions/20768766/gallery-in-a-fragment/20768841?noredirect=1#comment31140028_20768841/\">Gallery因为 Ranghunandan 教我什么是错在那里。我认为,(但我不知道),它的ImageAdapter内。我也将张贴LogCat中
下面是在Java code
进口java.lang.ref.WeakReference;
进口android.app.Activity;
进口android.content.Context;
进口android.content.res.Resources;
进口android.graphics.Bitmap;
进口android.graphics.BitmapFactory;
进口android.os.AsyncTask;
进口android.support.v4.util.LruCache;
进口android.util.Log;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.BaseAdapter;
进口android.widget.GridView;
进口android.widget.ImageView;公共类ImageAdapter延伸BaseAdapter { 私有静态最后弦乐TAG =ImageAdapter; 私人语境mContext; 公共静态最终整数[] = TATTOOS {
R.drawable.tattoo1,R.drawable.tattoo2,
R.drawable.tattoo3,R.drawable.tattoo4,
R.drawable.tattoo5,R.drawable.tattoo6,
R.drawable.tattoo7,R.drawable.tattoo8,
R.drawable.tattoo9,R.drawable.tattoo10,
/ * R.drawable.tattoo11,R.drawable.tattoo12,
R.drawable.tattoo13,R.drawable.tattoo14,
R.drawable.tattoo15,R.drawable.tattoo16,
R.drawable.tattoo17,R.drawable.tattoo18,
R.drawable.tattoo19,R.drawable.tattoo20,
R.drawable.tattoo21,R.drawable.tattoo22,
R.drawable.tattoo23,R.drawable.tattoo24,
R.drawable.tattoo25,R.drawable.tattoo26,
R.drawable.tattoo27,R.drawable.tattoo28,
R.drawable.tattoo29,R.drawable.tattoo30,
R.drawable.tattoo31,R.drawable.tattoo32,
R.drawable.tattoo33,R.drawable.tattoo34,
R.drawable.tattoo35,R.drawable.tattoo36,
R.drawable.tattoo37,R.drawable.tattoo38,
R.drawable.tattoo39,R.drawable.tattoo40,
R.drawable.tattoo41,R.drawable.tattoo42,
R.drawable.tattoo43,R.drawable.tattoo44,
R.drawable.tattoo45,R.drawable.tattoo46,
R.drawable.tattoo47,R.drawable.tattoo48,
R.drawable.tattoo49,R.drawable.tattoo50,
R.drawable.tattoo51,R.drawable.tattoo52,
R.drawable.tattoo53,R.drawable.tattoo54,
R.drawable.tattoo55,R.drawable.tattoo56,
R.drawable.tattoo57,R.drawable.tattoo58,
R.drawable.tattoo59,R.drawable.tattoo60,
R.drawable.tattoo61,R.drawable.tattoo62,
R.drawable.tattoo63,R.drawable.tattoo64,
R.drawable.tattoo65,R.drawable.tattoo66,
R.drawable.tattoo67,R.drawable.tattoo68,
R.drawable.tattoo69,R.drawable.tattoo70,
R.drawable.tattoo71,R.drawable.tattoo72,
R.drawable.tattoo73,R.drawable.tattoo74,
R.drawable.tattoo75,R.drawable.tattoo76,
R.drawable.tattoo77,R.drawable.tattoo78,
R.drawable.tattoo79,R.drawable.tattoo80,
R.drawable.tattoo81,R.drawable.tattoo82 * /
}; 私人位图mHolder = NULL;
私有静态最终诠释CACHE_SIZE = 30 * 1024 * 1024; // 8 MIB缓存
/ **缓存来存储所有德codeD图像* /
私人LruCache&LT;整数,位图&GT; mBitmapsCache =新LruCache&LT;整数,位图&GT;(CACHE_SIZE){ @覆盖
保护INT整型尺寸(最终整数关键,最终的位图值){
返回value.getByteCount();
} @覆盖
保护无效entryRemoved(最终驱逐布尔值,最终整数关键,最终的位图的属性oldValue,最终的位图newValue)以{
如果(!oldValue.equals(mHolder)){
oldValue.recycle();
}
}
}; //构造
公共ImageAdapter(上下文C){
mContext = C;
mHolder = BitmapFactory.de codeResource(c.getResources(),R.drawable.ic_launcher,NULL);
} @覆盖
公众诠释的getCount(){
返回TATTOOS.length;
} @覆盖
公共对象的getItem(INT位置){
返回TATTOOS [位置]
} @覆盖
众长getItemId(INT位置){
返回的位置;
} @覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
//这实际上是一个坏的解决方案,因为每次convertView被重用,你仍然会初始化新ImageView的,这是错误的
// ImageView的ImageView的=新ImageView的(this.mContext);
//新BitmapWorkerTask(ImageView的).execute(纹身[位置]);
//返回ImageView的; //更好的解决方案
ImageView的ImageView的; 如果(convertView == NULL){
ImageView的=新ImageView的(this.mContext); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(新GridView.LayoutParams(280,280));
}其他{
ImageView的=(ImageView的)convertView;
//已存在于存储器的再利用的ImageView
} 最后的位图itemBitmap = mBitmapsCache.get(TATTOOS [位置]); 如果(itemBitmap == NULL || itemBitmap.isRecycled()){
Log.e(TAG,位置+错过,推出去code为+ TATTOOS [位置]);
imageView.setImageBitmap(mHolder);
mBitmapsCache.put(TATTOOS [位置],mHolder);
新BitmapWorkerTask(mBitmapsCache,mContext.getResources(),这个).execute(TATTOOS [位置]);
}其他{
Log.e(TAG,位置+就是这里+ TATTOOS [位置]);
imageView.setImageBitmap(itemBitmap);
} 返回ImageView的;
} / **的AsyncTask从资源解码图像* /
静态类BitmapWorkerTask扩展的AsyncTask&LT;整数,太虚,位图&GT; {
私人int数据= 0;
私人最终LruCache&LT;整数,位图&GT; mCache;
私人最终资源MRES;
私人最终BaseAdapter mAdapter; 公共BitmapWorkerTask(LruCache&LT;整数,位图&GT;缓存,资源资源,BaseAdapter适配器){
// 在这儿无事可做
mCache =缓存;
MRES =资源;
mAdapter =适配器;
} //在后台德code图像。
@覆盖
保护位图doInBackground(整数... PARAMS){
数据=参数[0];
返回德codeSampledBitmapFromResource(MRES,数据,300,300);
} //完成后,看是否ImageView的仍然是围绕并设置位图。
@覆盖
保护无效onPostExecute(位图位图){
mCache.put(数据,位图);
mAdapter.notifyDataSetChanged();
}
} 公共静态位图德codeSampledBitmapFromResource(资源RES,INT渣油,
INT reqWidth,诠释reqHeight){ //首先去code。与inJustDe codeBounds = true来检查尺寸
最后BitmapFactory.Options选项=新BitmapFactory.Options();
options.inJustDe codeBounds = TRUE;
BitmapFactory.de codeResource(RES,渣油,期权); //计算inSampleSize
options.inSampleSize = calculateInSampleSize(选项,reqWidth,reqHeight); //德code位与inSampleSize集
options.inJustDe codeBounds = FALSE;
options.outHeight = 300;
options.outWidth = 300;
返回BitmapFactory.de codeResource(RES,渣油,期权);
} 公共静态INT calculateInSampleSize(BitmapFactory.Options选项,诠释reqWidth,诠释reqHeight){
//原始高度和图像宽度
最终诠释身高= options.outHeight;
最终诠释宽度= options.outWidth;
INT inSampleSize = 1; 如果(高度&GT; reqHeight ||宽度GT; reqWidth){ //计算的高度和宽度的比率要求的高度和宽度
最终诠释heightRatio = Math.round((浮点)高度/(浮点)reqHeight);
最终诠释widthRatio = Math.round((浮点)宽/(浮点)reqWidth); //选择最小比率inSampleSize值,这将保证
//使用两个维度大于或等于所述最终图像
//请求的高度和宽度。
inSampleSize = heightRatio&LT; widthRatio? heightRatio:widthRatio;
} 返回inSampleSize;
}
}
这里的LogCat中
13 12-25:55:13.722:D / AndroidRuntime(7065):关闭VM
12-25 13:55:13.722:W / dalvikvm(7065):主题ID = 1:螺纹未捕获的异常退出(组= 0x419c5b90)
12-25 13:55:13.752:D / dalvikvm(7065):GC_FOR_ALLOC释放179K,3%的自由9923K / 10136K,暂停为20ms,20ms的总
12-25 13:55:13.752:E / AndroidRuntime(7065):致命异常:主要
12-25 13:55:13.752:E / AndroidRuntime(7065):工艺:info.androidhive.slidingmenu,PID:7065
12-25 13:55:13.752:E / AndroidRuntime(7065):android.util.SuperNotCalledException:片段GalleryActivity {4238f5d8#0 ID = 0x7f0a0008}没有通过调用super.onActivityCreated()
12-25 13:55:13.752:E / AndroidRuntime(7065):在android.app.Fragment.performActivityCreated(Fragment.java:1710)
12-25 13:55:13.752:E / AndroidRuntime(7065):在android.app.FragmentManagerImpl.moveToState(FragmentManager.java:908)
12-25 13:55:13.752:E / AndroidRuntime(7065):在android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
12-25 13:55:13.752:E / AndroidRuntime(7065):在android.app.BackStackRecord.run(BackStackRecord.java:684)
12-25 13:55:13.752:E / AndroidRuntime(7065):在android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
12-25 13:55:13.752:E / AndroidRuntime(7065):在android.app.FragmentManagerImpl $ 1.run(FragmentManager.java:443)
12-25 13:55:13.752:E / AndroidRuntime(7065):在android.os.Handler.handleCallback(Handler.java:733)
12-25 13:55:13.752:E / AndroidRuntime(7065):在android.os.Handler.dispatchMessage(Handler.java:95)
12-25 13:55:13.752:E / AndroidRuntime(7065):在android.os.Looper.loop(Looper.java:137)
12-25 13:55:13.752:E / AndroidRuntime(7065):在android.app.ActivityThread.main(ActivityThread.java:4998)
12-25 13:55:13.752:E / AndroidRuntime(7065):在java.lang.reflect.Method.invokeNative(本机方法)
12-25 13:55:13.752:E / AndroidRuntime(7065):在java.lang.reflect.Method.invoke(Method.java:515)
12-25 13:55:13.752:E / AndroidRuntime(7065):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:777)
12-25 13:55:13.752:E / AndroidRuntime(7065):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
12-25 13:55:13.752:E / AndroidRuntime(7065):在dalvik.system.NativeStart.main(本机方法)
12-25 14:00:13.902:I /流程(7065):发送信号。 PID:7065 SIG:9
在logcat的告诉你,你没有调用超 onActivityCreated();
错误的是不是在你的适配器。看到这一行中的堆栈跟踪:
android.util.SuperNotCalledException:片段GalleryActivity {4238f5d8#0 ID = 0x7f0a0008}没有通过调用super.onActivityCreated()
该onActivityCreated方法需要这一呼吁,应该是这样的:
@覆盖
onActivityCreated(捆绑savedInstanceState){
super.onActivityCreated(savedInstanceState);
//这里做什么
}
I'm getting a Force Close when I launch the Gallery Fragment. It's not the code from the Gallery since Ranghunandan showed me what was wrong there. I think (but I'm not sure) it's within the ImageAdapter. I'll also post the LogCat
Here's the Java Code
import java.lang.ref.WeakReference;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.support.v4.util.LruCache;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class ImageAdapter extends BaseAdapter {
private static final String TAG = "ImageAdapter";
private Context mContext;
public static final Integer[] TATTOOS = {
R.drawable.tattoo1, R.drawable.tattoo2,
R.drawable.tattoo3, R.drawable.tattoo4,
R.drawable.tattoo5, R.drawable.tattoo6,
R.drawable.tattoo7, R.drawable.tattoo8,
R.drawable.tattoo9, R.drawable.tattoo10,
/* R.drawable.tattoo11, R.drawable.tattoo12,
R.drawable.tattoo13, R.drawable.tattoo14,
R.drawable.tattoo15, R.drawable.tattoo16,
R.drawable.tattoo17, R.drawable.tattoo18,
R.drawable.tattoo19, R.drawable.tattoo20,
R.drawable.tattoo21, R.drawable.tattoo22,
R.drawable.tattoo23, R.drawable.tattoo24,
R.drawable.tattoo25, R.drawable.tattoo26,
R.drawable.tattoo27, R.drawable.tattoo28,
R.drawable.tattoo29, R.drawable.tattoo30,
R.drawable.tattoo31, R.drawable.tattoo32,
R.drawable.tattoo33, R.drawable.tattoo34,
R.drawable.tattoo35, R.drawable.tattoo36,
R.drawable.tattoo37, R.drawable.tattoo38,
R.drawable.tattoo39, R.drawable.tattoo40,
R.drawable.tattoo41, R.drawable.tattoo42,
R.drawable.tattoo43, R.drawable.tattoo44,
R.drawable.tattoo45, R.drawable.tattoo46,
R.drawable.tattoo47, R.drawable.tattoo48,
R.drawable.tattoo49, R.drawable.tattoo50,
R.drawable.tattoo51, R.drawable.tattoo52,
R.drawable.tattoo53, R.drawable.tattoo54,
R.drawable.tattoo55, R.drawable.tattoo56,
R.drawable.tattoo57, R.drawable.tattoo58,
R.drawable.tattoo59, R.drawable.tattoo60,
R.drawable.tattoo61, R.drawable.tattoo62,
R.drawable.tattoo63, R.drawable.tattoo64,
R.drawable.tattoo65, R.drawable.tattoo66,
R.drawable.tattoo67, R.drawable.tattoo68,
R.drawable.tattoo69, R.drawable.tattoo70,
R.drawable.tattoo71, R.drawable.tattoo72,
R.drawable.tattoo73, R.drawable.tattoo74,
R.drawable.tattoo75, R.drawable.tattoo76,
R.drawable.tattoo77, R.drawable.tattoo78,
R.drawable.tattoo79, R.drawable.tattoo80,
R.drawable.tattoo81, R.drawable.tattoo82*/
};
private Bitmap mHolder = null;
private static final int CACHE_SIZE = 30 * 1024 * 1024; // 8 MiB cache
/** Cache to store all decoded images */
private LruCache<Integer, Bitmap> mBitmapsCache = new LruCache<Integer, Bitmap>(CACHE_SIZE) {
@Override
protected int sizeOf(final Integer key, final Bitmap value) {
return value.getByteCount();
}
@Override
protected void entryRemoved(final boolean evicted, final Integer key, final Bitmap oldValue, final Bitmap newValue) {
if (!oldValue.equals(mHolder)) {
oldValue.recycle();
}
}
};
// Constructor
public ImageAdapter(Context c){
mContext = c;
mHolder = BitmapFactory.decodeResource(c.getResources(), R.drawable.ic_launcher, null);
}
@Override
public int getCount() {
return TATTOOS.length;
}
@Override
public Object getItem(int position) {
return TATTOOS[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//This actually is a bad solution, because every time convertView is reused, you will still initialize new ImageView, which is wrong
//ImageView imageView = new ImageView(this.mContext);
//new BitmapWorkerTask(imageView).execute(Tattoos[position]);
//return imageView;
//Better solution
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(this.mContext);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(280, 280));
} else {
imageView = (ImageView) convertView;
//re-use ImageView that already exists in memory
}
final Bitmap itemBitmap = mBitmapsCache.get(TATTOOS[position]);
if (itemBitmap == null || itemBitmap.isRecycled()) {
Log.e(TAG, position + " is missed, launch decode for " + TATTOOS[position]);
imageView.setImageBitmap(mHolder);
mBitmapsCache.put(TATTOOS[position], mHolder);
new BitmapWorkerTask(mBitmapsCache, mContext.getResources(), this).execute(TATTOOS[position]);
} else {
Log.e(TAG, position + " is here for " + TATTOOS[position]);
imageView.setImageBitmap(itemBitmap);
}
return imageView;
}
/** AsyncTask for decoding images from resources */
static class BitmapWorkerTask extends AsyncTask<Integer, Void, Bitmap> {
private int data = 0;
private final LruCache<Integer, Bitmap> mCache;
private final Resources mRes;
private final BaseAdapter mAdapter;
public BitmapWorkerTask(LruCache<Integer, Bitmap> cache, Resources res, BaseAdapter adapter) {
// nothing to do here
mCache = cache;
mRes = res;
mAdapter = adapter;
}
// Decode image in background.
@Override
protected Bitmap doInBackground(Integer... params) {
data = params[0];
return decodeSampledBitmapFromResource(mRes, data, 300, 300);
}
// Once complete, see if ImageView is still around and set bitmap.
@Override
protected void onPostExecute(Bitmap bitmap) {
mCache.put(data, bitmap);
mAdapter.notifyDataSetChanged();
}
}
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
options.outHeight = 300;
options.outWidth = 300;
return BitmapFactory.decodeResource(res, resId, options);
}
public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
// Calculate ratios of height and width to requested height and width
final int heightRatio = Math.round((float) height / (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
// Choose the smallest ratio as inSampleSize value, this will guarantee
// a final image with both dimensions larger than or equal to the
// requested height and width.
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
}
Here's the LogCat
12-25 13:55:13.722: D/AndroidRuntime(7065): Shutting down VM
12-25 13:55:13.722: W/dalvikvm(7065): threadid=1: thread exiting with uncaught exception (group=0x419c5b90)
12-25 13:55:13.752: D/dalvikvm(7065): GC_FOR_ALLOC freed 179K, 3% free 9923K/10136K, paused 20ms, total 20ms
12-25 13:55:13.752: E/AndroidRuntime(7065): FATAL EXCEPTION: main
12-25 13:55:13.752: E/AndroidRuntime(7065): Process: info.androidhive.slidingmenu, PID: 7065
12-25 13:55:13.752: E/AndroidRuntime(7065): android.util.SuperNotCalledException: Fragment GalleryActivity{4238f5d8 #0 id=0x7f0a0008} did not call through to super.onActivityCreated()
12-25 13:55:13.752: E/AndroidRuntime(7065): at android.app.Fragment.performActivityCreated(Fragment.java:1710)
12-25 13:55:13.752: E/AndroidRuntime(7065): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:908)
12-25 13:55:13.752: E/AndroidRuntime(7065): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
12-25 13:55:13.752: E/AndroidRuntime(7065): at android.app.BackStackRecord.run(BackStackRecord.java:684)
12-25 13:55:13.752: E/AndroidRuntime(7065): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
12-25 13:55:13.752: E/AndroidRuntime(7065): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
12-25 13:55:13.752: E/AndroidRuntime(7065): at android.os.Handler.handleCallback(Handler.java:733)
12-25 13:55:13.752: E/AndroidRuntime(7065): at android.os.Handler.dispatchMessage(Handler.java:95)
12-25 13:55:13.752: E/AndroidRuntime(7065): at android.os.Looper.loop(Looper.java:137)
12-25 13:55:13.752: E/AndroidRuntime(7065): at android.app.ActivityThread.main(ActivityThread.java:4998)
12-25 13:55:13.752: E/AndroidRuntime(7065): at java.lang.reflect.Method.invokeNative(Native Method)
12-25 13:55:13.752: E/AndroidRuntime(7065): at java.lang.reflect.Method.invoke(Method.java:515)
12-25 13:55:13.752: E/AndroidRuntime(7065): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
12-25 13:55:13.752: E/AndroidRuntime(7065): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
12-25 13:55:13.752: E/AndroidRuntime(7065): at dalvik.system.NativeStart.main(Native Method)
12-25 14:00:13.902: I/Process(7065): Sending signal. PID: 7065 SIG: 9
The logcat tells you, that you're not calling the super onActivityCreated();
the error is not in your Adapter. See this line in your stacktrace:
android.util.SuperNotCalledException: Fragment GalleryActivity{4238f5d8 #0 id=0x7f0a0008} did not call through to super.onActivityCreated()
The onActivityCreated Method needs this call and should look like this:
@Override
onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// do something here
}
这篇关于画廊部队关闭使用片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!