我使用了一个URI的ArrayList,以便能够遍历图像并每次更新单个ImageView。当我接近这样做时,我得到一个NullPointerException,并且我认为它与相机旋转我的屏幕(破坏活动)有关,当它出现以前提到的图片时。
使用onSaveInstanceState和onResume时会出现什么样? -
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageURI(android.net.Uri)' on a null object reference
at plaidshorts.rafael.myapplication.Person1Screen$5.onClick(Person1Screen.java:124)
at android.view.View.performClick(View.java:5209)
at android.view.View$PerformClick.run(View.java:21174)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5477)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
第124行是:
mainView.setImageURI(happyList.get(0));
我的dispatchTakePictureIntent提示相机或图库两次(参见开头的循环)。然后,OnActivityResult获取URI并将其添加到happyList(在onCreate中实例化)。
private void dispatchTakePictureIntent() {
pictureCounter = -1;
for(int i = 0; i < 2; i++) {
pictureCounter++;
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
outputFileUri = Uri.fromFile(photoFile);
} catch (IOException ex) {
Log.w("error","IOException");
}catch (NullPointerException nullEx) {
Log.w("error","NullPointerException");
}
// Camera.
final List<Intent> cameraIntents = new ArrayList<Intent>();
final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
final PackageManager packageManager = getPackageManager();
final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
for (ResolveInfo res : listCam) {
final String packageName = res.activityInfo.packageName;
final Intent intent = new Intent(captureIntent);
intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
intent.setPackage(packageName);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
cameraIntents.add(intent);
}
// Filesystem.
final Intent galleryIntent = new Intent();
galleryIntent.setType("image/*");
galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
// Chooser of filesystem options.
final Intent chooserIntent = Intent.createChooser(galleryIntent, "Select Source");
// Add the camera options.
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[cameraIntents.size()]));
startActivityForResult(chooserIntent, REQUEST_TAKE_PHOTO);
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == REQUEST_TAKE_PHOTO) {
final boolean isCamera;
if (data == null) {
isCamera = true;
} else {
final String action = data.getAction();
if (action == null) {
isCamera = false;
} else {
isCamera = action.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
}
}
Uri selectedImageUri;
if (isCamera) {
selectedImageUri = outputFileUri;
} else {
selectedImageUri = data == null ? null : data.getData();
}
// mainView.setImageURI(selectedImageUri);
// Log.d("Before adding", selectedImageUri.toString());
happyList.add(selectedImageUri);
}
}
}
答案 0 :(得分:1)
ArrayList<URI> uris = ..
//inStop of your application
SharedPreference sp = context.getSharePreference("imagineThis",0);
SharedPreference.Editor ed = sp.edit();
byte i =0;
for(Uri u : uris){ i++;
ed.put(String.valueOf(i), u.toString());}
ed.commit();
//in onrestart of your app
uris.clear();//provided is not null;
SharedPreference sp = context.getSharePreference("imagineThis",0);
SharedPreference.Editor ed = sp.edit();
for(String s : ((Map<String,String>)sp.getAll()).values()){
uris.add(Uri.parse(s));
}
ed.clear();
ed.commit();
我认为你现在很好
答案 1 :(得分:1)
嘿,您只需覆盖活动中的onRetainNonConfigurationInstance并返回您的arraylist对象,并在onCreate()方法中检查您的对象是否为null,如果其null使用getLastNonConfigurationInstance()来检索对象
//PseudoCode
onRetainNonConfigurationInstance() {
return arraylistObject;
}
oncreate(Bundle onSavedInstance){
if(arraylistObject == null){
arraylistObject = (cast properly)getLastNonConfigurationInstance();
}
}
这是最简单的一种,但建议使用片段来保存旋转状态
答案 2 :(得分:1)
将您的数据存储在onSaveInstanceState()
中@Override
public void onSaveInstanceState (Bundle outState) {
// TODO Auto-generated method stub
super.onSaveInstanceState(outState);
outState.putSerializable("array", arrayDetails);
}
将这些代码行放在活动的OnCreate()方法中,以获取方向更改中的arrayList。
if (savedInstanceState != null) {
arrayDetails = (ArrayList<yourListType>)savedInstanceState.getSerializable("array");
}