这里我试图保存我的片段状态。但我得到一个空指针异常 在保存状态的同时。可以提供任何帮助,谢谢。 我已粘贴代码和错误以供参考
List_View_Fragment.java
package com.example.testfragment;
import java.util.ArrayList;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ListView;
public class List_View_Fragment extends Fragment {
ListView list;
EditText et;
EditText_Adapter aa;
ArrayList<String> Items;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// if(savedInstanceState != null){
// Toast.makeText(getActivity(), "save instance is not null", Toast.LENGTH_LONG).show();
// }
};
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
Items = new ArrayList<String>();
Items.add("");
Items.add("");
Items.add("");
Items.add("");
View view = inflater.inflate(R.layout.list_view, container, false);
list = (ListView) view.findViewById(R.id.MyList);
if (aa == null) {
aa = new EditText_Adapter(view.getContext(),
R.layout.list_view_edit_text, Items);
}
list.setAdapter(aa);
return view;
}
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
List_View_Fragment lf = (List_View_Fragment)getFragmentManager().findFragmentById(R.id.fragment_container);
**List_View_Fragment.SavedState myFragmentState = getFragmentManager().saveFragmentInstanceState(lf);
outState.putParcelable("key", myFragmentState);**
};
}
错误
06-17 11:37:56.235: E/AndroidRuntime(31080): FATAL EXCEPTION: main
06-17 11:37:56.235: E/AndroidRuntime(31080): java.lang.StackOverflowError
06-17 11:37:56.235: E/AndroidRuntime(31080): at android.app.Fragment.performSaveInstanceState(Fragment.java:1840)
06-17 11:37:56.235: E/AndroidRuntime(31080): at android.app.FragmentManagerImpl.saveFragmentBasicState(FragmentManager.java:1578)
06-17 11:37:56.235: E/AndroidRuntime(31080): at android.app.FragmentManagerImpl.saveFragmentInstanceState(FragmentManager.java:590)
06-17 11:37:56.235: E/AndroidRuntime(31080): at com.example.testfragment.List_View_Fragment.onSaveInstanceState(List_View_Fragment.java:51)
06-17 11:37:56.235: E/AndroidRuntime(31080): at android.app.Fragment.performSaveInstanceState(Fragment.java:1840)
06-17 11:37:56.235: E/AndroidRuntime(31080): at android.app.FragmentManagerImpl.saveFragmentBasicState(FragmentManager.java:1578)
06-17 11:37:56.235: E/AndroidRuntime(31080): at android.app.FragmentManagerImpl.saveFragmentInstanceState(FragmentManager.java:590)
06-17 11:37:56.235: E/AndroidRuntime(31080): at com.example.testfragment.List_View_Fragment.onSaveInstanceState(List_View_Fragment.java:51)
06-17 11:37:56.235: E/AndroidRuntime(31080): at android.app.Fragment.performSaveInstanceState(Fragment.java:1840)
06-17 11:37:56.235: E/AndroidRuntime(31080): at android.app.FragmentManagerImpl.saveFragmentBasicState(FragmentManager.java:1578)
06-17 11:37:56.235: E/AndroidRuntime(31080): at android.app.FragmentManagerImpl.saveFragmentInstanceState(FragmentManager.java:590)
06-17 11:37:56.235: E/AndroidRuntime(31080): at com.example.testfragment.List_View_Fragment.onSaveInstanceState(List_View_Fragment.java:51)
06-17 11:37:56.235: E/AndroidRuntime(31080): at android.app.Fragment.performSaveInstanceState(Fragment.java:1840)
06-17 11:37:56.235: E/AndroidRuntime(31080): at android.app.FragmentManagerImpl.saveFragmentBasicState(FragmentManager.java:1578)
06-17 11:37:56.235: E/AndroidRuntime(31080): at android.app.FragmentManagerImpl.saveFragmentInstanceState(FragmentManager.java:590)
06-17 11:37:56.235: E/AndroidRuntime(31080): at com.example.testfragment.List_View_Fragment.onSaveInstanceState(List_View_Fragment.java:51)
06-17 11:37:56.235: E/AndroidRuntime(31080): at android.app.Fragment.performSaveInstanceState(Fragment.java:1840)
06-17 11:37:56.235: E/AndroidRuntime(31080): at android.app.FragmentManagerImpl.saveFragmentBasicState(FragmentManager.java:1578)
06-17 11:37:56.235: E/AndroidRuntime(31080): at android.app.FragmentManagerImpl.saveFragmentInstanceState(FragmentManager.java:590)
06-17 11:37:56.235: E/AndroidRuntime(31080): at com.example.testfragment.List_View_Fragment.onSaveInstanceState(List_View_Fragment.java:51)
06-17 11:37:56.235: E/AndroidRuntime(31080): at android.app.Fragment.performSaveInstanceState(Fragment.java:1840)
06-17 11:37:56.235: E/AndroidRuntime(31080): at android.app.FragmentManagerImpl.saveFragmentBasicState(FragmentManager.java:1578)
06-17 11:37:56.235: E/AndroidRuntime(31080): at android.app.FragmentManagerImpl.saveFragmentInstanceState(FragmentManager.java:590)
06-17 11:37:56.235: E/AndroidRuntime(31080): at com.example.testfragment.List_View_Fragment.onSaveInstanceState(List_View_Fragment.java:51)
06-17 11:37:56.235: E/AndroidRuntime(31080): at android.app.Fragment.performSaveInstanceState(Fragment.java:1840)
06-17 11:37:56.235: E/AndroidRuntime(31080): at android.app.FragmentManagerImpl.saveFragmentBasicState(FragmentManager.java:1578)
06-17 11:37:56.235: E/AndroidRuntime(31080): at android.app.FragmentManagerImpl.saveFragmentInstanceState(FragmentManager.java:590)
06-17 11:37:56.235: E/AndroidRuntime(31080): at com.example.testfragment.List_View_Fragment.onSaveInstanceState(List_View_Fragment.java:51)
06-17 11:37:56.235: E/AndroidRuntime(31080): at android.app.Fragment.performSaveInstanceState(Fragment.java:1840)
06-17 11:37:56.235: E/AndroidRuntime(31080): at android.app.FragmentManagerImpl.saveFragmentBasicState(FragmentManager.java:1578)
06-17 11:37:56.235: E/AndroidRuntime(31080): at android.app.FragmentManagerImpl.saveFragmentInstanceState(FragmentManager.java:590)
06-17 11:37:56.235: E/AndroidRuntime(31080): at com.example.testfragment.List_View_Fragment.onSaveInstanceState(List_View_Fragment.java:51)
06-17 11:37:56.235: E/AndroidRuntime(31080): at android.app.Fragment.performSaveInstanceState(Fragment.java:1840)
06-17 11:37:56.235: E/AndroidRuntime(31080): at android.app.FragmentManagerImpl.saveFragmentBasicState(FragmentManager.java:1578)
06-17 11:37:56.235: E/AndroidRuntime(31080): at android.app.FragmentManagerImpl.saveFragmentInstanceState(FragmentManager.java:590)
06-17 11:37:56.235: E/AndroidRuntime(31080): at com.example.testfragment.List_View_Fragment.onSaveInstanceState(List_View_Fragment.java:51)
06-17 11:37:56.235: E/AndroidRuntime(31080): at android.app.Fragment.performSaveInstanceState(Fragment.java:1840)
06-17 11:37:56.235: E/AndroidRuntime(31080): at android.app.FragmentManagerImpl.saveFragmentBasicState(FragmentManager.java:1578)
06-17 11:37:56.235: E/AndroidRuntime(31080): at android.app.FragmentManagerImpl.saveFragmentInstanceState(FragmentManager.java:590)
06-17 11:37:56.235: E/AndroidRuntime(31080): at com.example.testfragment.List_View_Fragment.onSaveInstanceState(List_View_Fragment.java:51)
06-17 11:37:56.235: E/AndroidRuntime(31080): at android.app.Fragment.performSaveInstanceState(Fragment.java:1840)
06-17 11:37:56.235: E/AndroidRuntime(31080): at android.app.FragmentManagerImpl.saveFragmentBasicState(FragmentManager.java:1578)
06-17 11:37:56.235: E/AndroidRuntime(31080): at android.app.FragmentManagerImpl.saveFragmentInstanceState(FragmentManager.java:590)
06-17 11:37:56.235: E/AndroidRuntime(31080): at com.example.testfragment.List_View_Fragment.onSaveInstanceState(List_View_Fragment.java:51)
06-17 11:37:56.235: E/AndroidRuntime(31080): at android.app.Fragment.performSaveInstanceState(Fragment.java:1840)
06-17 11:37:56.235: E/AndroidRuntime(31080): at android.app.FragmentManagerImpl.saveFragmentBasicState(FragmentManager.java:1578)
06-17 11:37:56.235: E/AndroidRuntime(31080): at android.app.FragmentManagerImpl.saveFragmentInstanceState(FragmentManager.java:590)
06-17 11:37:56.235: E/AndroidRuntime(31080): at com.example.testfrag
答案 0 :(得分:0)
你的代码中有一个无限循环,我不知道为什么,但似乎是在调用
List_View_Fragment.SavedState myFragmentState = getFragmentManager().saveFragmentInstanceState(lf)
引发了这种行为。我建议您删除此行并保存视图状态(TextViews,EditText等)。在onActivityCreated
方法(片段内)中,恢复这些元素的值。
希望有所帮助
答案 1 :(得分:0)
你应该使用setRetainInstance(true)。