我只为照片构建自定义图库。我用这个类从sdcard拍照:
public class Photos {
public static final String PHOTO_BUCKET_NAME =
Environment.getExternalStorageState().toString() + "/DCIM/Camera";
public static final int PHOTO_BUCKET_ID = getBucketId(PHOTO_BUCKET_NAME);
public static int getBucketId(String path) {
return path.toLowerCase().hashCode();
}
public static List<String> getPhotos(Context context) {
final String[] projection = {MediaStore.Images.Media.DATA};
final String selection = MediaStore.Images.Media.BUCKET_ID;
Cursor c = context.getContentResolver().query(Images.Media.EXTERNAL_CONTENT_URI, projection, selection, null, null);
List<String> result = new ArrayList<String>(c.getCount());
if (c.moveToFirst()) {
int dataColumn = c.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
do {
String data = c.getString(dataColumn);
result.add(data);
} while (c.moveToNext());
}
c.close();
return result;
}
将适配器设置为gallery:
public class ImageAdapter extends BaseAdapter {
int GalItemBg;
private Context mContext;
private List<String> photoList;
public ImageAdapter(Context c) {
mContext = c;
photoList = Photos.getPhotos(c);
TypedArray typArray = obtainStyledAttributes(R.styleable.GalleryTheme);
GalItemBg = typArray.getResourceId(R.styleable.GalleryTheme_android_galleryItemBackground, 0);
typArray.recycle();
}
@Override
public int getCount() {
return photoList.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(mContext);
Bitmap bm = BitmapFactory.decodeFile(photoList.get(position));
i.setImageBitmap(bm);
i.setLayoutParams(new Gallery.LayoutParams(80,70));
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setBackgroundResource(GalItemBg);
return i;
}
}
} 没什么特别的。当我运行我的应用程序时,一切都很好。之后,我启动相机并拍摄一些照片,这次我的应用程序在后台运行。当我回到我的应用程序时,我需要看到新照片。怎么做?
尝试: 在onResume方法中更新适配器,如下所示:
Gallery g;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
g = (Gallery) findViewById(R.id.gallery);
g.setAdapter(new ImageAdapter(this));
}
@Override
public void onResume() {
g.setAdapter(new ImageAdapter(this));
}
应用程序崩溃。可能是我做错了什么?
logcat的:
02-09 08:12:33.719: ERROR/AndroidRuntime(276): Uncaught handler: thread main exiting due to uncaught exception
02-09 08:12:34.129: ERROR/AndroidRuntime(276): java.lang.RuntimeException: Unable to resume activity {ru.home.tabexample/ru.home.tabexample.TabAct}: java.lang.RuntimeException: Unable to resume activity {ru.home.tabexample/ru.home.tabexample.FirstTab}: android.app.SuperNotCalledException: Activity {ru.home.tabexample/ru.home.tabexample.FirstTab} did not call through to super.onResume()
02-09 08:12:34.129: ERROR/AndroidRuntime(276): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2950)
02-09 08:12:34.129: ERROR/AndroidRuntime(276): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2965)
02-09 08:12:34.129: ERROR/AndroidRuntime(276): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2516)
02-09 08:12:34.129: ERROR/AndroidRuntime(276): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
02-09 08:12:34.129: ERROR/AndroidRuntime(276): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
02-09 08:12:34.129: ERROR/AndroidRuntime(276): at android.os.Handler.dispatchMessage(Handler.java:99)
02-09 08:12:34.129: ERROR/AndroidRuntime(276): at android.os.Looper.loop(Looper.java:123)
02-09 08:12:34.129: ERROR/AndroidRuntime(276): at android.app.ActivityThread.main(ActivityThread.java:4363)
02-09 08:12:34.129: ERROR/AndroidRuntime(276): at java.lang.reflect.Method.invokeNative(Native Method)
02-09 08:12:34.129: ERROR/AndroidRuntime(276): at java.lang.reflect.Method.invoke(Method.java:521)
02-09 08:12:34.129: ERROR/AndroidRuntime(276): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
02-09 08:12:34.129: ERROR/AndroidRuntime(276): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
02-09 08:12:34.129: ERROR/AndroidRuntime(276): at dalvik.system.NativeStart.main(Native Method)
02-09 08:12:34.129: ERROR/AndroidRuntime(276): Caused by: java.lang.RuntimeException: Unable to resume activity {ru.home.tabexample/ru.home.tabexample.FirstTab}: android.app.SuperNotCalledException: Activity {ru.home.tabexample/ru.home.tabexample.FirstTab} did not call through to super.onResume()
02-09 08:12:34.129: ERROR/AndroidRuntime(276): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2950)
02-09 08:12:34.129: ERROR/AndroidRuntime(276): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:170)
02-09 08:12:34.129: ERROR/AndroidRuntime(276): at android.app.LocalActivityManager.dispatchResume(LocalActivityManager.java:518)
02-09 08:12:34.129: ERROR/AndroidRuntime(276): at android.app.ActivityGroup.onResume(ActivityGroup.java:58)
02-09 08:12:34.129: ERROR/AndroidRuntime(276): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1149)
02-09 08:12:34.129: ERROR/AndroidRuntime(276): at android.app.Activity.performResume(Activity.java:3763)
02-09 08:12:34.129: ERROR/AndroidRuntime(276): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2937)
02-09 08:12:34.129: ERROR/AndroidRuntime(276): ... 12 more
02-09 08:12:34.129: ERROR/AndroidRuntime(276): Caused by: android.app.SuperNotCalledException: Activity {ru.home.tabexample/ru.home.tabexample.FirstTab} did not call through to super.onResume()
02-09 08:12:34.129: ERROR/AndroidRuntime(276): at android.app.Activity.performResume(Activity.java:3765)
02-09 08:12:34.129: ERROR/AndroidRuntime(276): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2937)
02-09 08:12:34.129: ERROR/AndroidRuntime(276): ... 18 more
答案 0 :(得分:3)
你必须在onResume方法中调用super.onResume。
您可以在例外消息中看到此消息:android.app.SuperNotCalledException: Activity {ru.home.tabexample/ru.home.tabexample.FirstTab} did not call through to super.onResume()
活动基类还使用onResume方法重新初始化东西。如果在类中实现它但忽略了super.onResume()调用,则无法在基类中进行初始化。