我有一个片段,其中我有图像按钮“产品图像”,我正在将图像加载到具有图库意图的图像按钮并使用Glide库加载该图像,以便保留我正在使用onSavedInstanceState的图像( )但问题是,当我将方向更改为横向时,它会保留图像,但是当我再次切换回纵向模式时,它不会保留图像并给我一个空指针执行,如果有一些技术性的东西,我我做错了请指导,因为我对Android开发有点新意。 这是我的fragemnt代码。
public class NoteEditFragment extends Fragment {
ImageButton productImage;
private static final int REQUEST_OPEN_RESULT_CODE = 0;
private Uri mImageUri;
EditText productName;
EditText productPrice;
EditText productDescription;
AlertDialog confirmDialogObject;
Uri myuri;
public NoteEditFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View FragmentLayout = inflater.inflate(R.layout.fragment_note_edit, container, false);
productImage = (ImageButton) FragmentLayout.findViewById(R.id.editNoteButton);
if(savedInstanceState != null){
myuri = Uri.parse(savedInstanceState.getString("imageuri"));
Glide.with(this)
.load(myuri)
.into(productImage);
}
productName = (EditText) FragmentLayout.findViewById(R.id.EditNoteTitle);
Intent intent = getActivity().getIntent();
});
productImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent1 = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent1.addCategory(Intent.CATEGORY_OPENABLE);
intent1.setType("image/*");
startActivityForResult(intent1, REQUEST_OPEN_RESULT_CODE);
}
});
return FragmentLayout;
}
@Override
public void onSaveInstanceState(Bundle outState) {
if(mImageUri != null){
outState.putString("imageuri",mImageUri.toString());}
super.onSaveInstanceState(outState);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent resultData) {
if(requestCode == REQUEST_OPEN_RESULT_CODE && resultCode == Activity.RESULT_OK ) {
if(resultData != null) {
mImageUri = resultData.getData();
Glide.with(this)
.load(mImageUri)
.into(productImage);
}
}
}
这是我得到的错误
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ibrahim_01.notebookapp, PID: 4102
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ibrahim_01.notebookapp/com.example.ibrahim_01.notebookapp.NoteDetailActivity}: java.lang.NullPointerException: uriString
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4077)
at android.app.ActivityThread.-wrap15(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1350)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
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)
Caused by: java.lang.NullPointerException: uriString
at android.net.Uri$StringUri.<init>(Uri.java:475)
at android.net.Uri$StringUri.<init>(Uri.java)
at android.net.Uri.parse(Uri.java:437)
at com.example.ibrahim_01.notebookapp.NoteEditFragment$override.onCreateView(NoteEditFragment.java:66)
at com.example.ibrahim_01.notebookapp.NoteEditFragment$override.access$dispatch(NoteEditFragment.java)
at com.example.ibrahim_01.notebookapp.NoteEditFragment.onCreateView(NoteEditFragment.java:0)
at android.app.Fragment.performCreateView(Fragment.java:2220)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:973)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1148)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1130)
at android.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1953)
at android.app.FragmentController.dispatchActivityCreated(FragmentController.java:152)
at android.app.Activity.performCreateCommon(Activity.java:6232)
at android.app.Activity.performCreate(Activity.java:6239)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4077)
at android.app.ActivityThread.-wrap15(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1350)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
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)
答案 0 :(得分:3)
第一次旋转时,会设置字段(imageUri)。 onSaveInstanceState()
会将此字段保留在包中旋转。
现在在横向中,您将从onCreate()
中的包中读取并获取您存储的值。但请注意,imageUri
将为空,因为您将其存储到myUri
。
第二次轮换时,onSaveInstanceState()
将不会保留imageUri
,因为它为空。但现在在肖像中,它会尝试读取它(bundle不为null),返回null(因为键“imageuri”没有值),并且你在Uri.parse中获得了空指针。
您可以通过删除字段myUri
并将onCreate()
中的字符串值分配给imageUri
来解决。我想 - 我正在手机上看这个!
答案 1 :(得分:-1)
覆盖方法 onCreate 并添加setRetainInstance(true),如下所示:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}