Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 方向改变时碎片崩溃_Android_Android Layout_Android Fragments - Fatal编程技术网

Android 方向改变时碎片崩溃

Android 方向改变时碎片崩溃,android,android-layout,android-fragments,Android,Android Layout,Android Fragments,每次方向更改时,碎片都会因错误而崩溃: 01-26 12:46:43.215 2895-2895/com.cbsystematic.mobile.itvdn E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.cbsystematic.mobile.itvdn, PID: 2895 java.lang.RuntimeException: Unable to destroy activity {com.cbsystem

每次方向更改时,碎片都会因错误而崩溃:

01-26 12:46:43.215    2895-2895/com.cbsystematic.mobile.itvdn 

E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.cbsystematic.mobile.itvdn, PID: 2895
    java.lang.RuntimeException: Unable to destroy activity {com.cbsystematic.mobile.itvdn/com.cbsystematic.mobile.itvdn.NavigationActivity}: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
            at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3671)
            at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3689)
            at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3889)
            at android.app.ActivityThread.access$900(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1284)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
            at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1365)
            at android.support.v4.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:516)
            at com.cbsystematic.mobile.itvdn.LessonsFragment.replaceFragment(LessonsFragment.java:126)
            at com.cbsystematic.mobile.itvdn.LessonsFragment.onDetach(LessonsFragment.java:119)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1080)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1108)
            at android.support.v4.app.FragmentManagerImpl.dispatchDestroy(FragmentManager.java:1954)
            at android.support.v4.app.FragmentActivity.onDestroy(FragmentActivity.java:313)
            at android.support.v7.app.ActionBarActivity.onDestroy(ActionBarActivity.java:169)
            at android.app.Activity.performDestroy(Activity.java:6112)
            at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1140)
            at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3658)
            at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3689)
            at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3889)
            at android.app.ActivityThread.access$900(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1284)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
我需要保存当前片段的状态

片段类代码:

public class LessonsFragment extends Fragment implements AbsListView.OnItemClickListener {


private OnFragmentInteractionListener mListener;

/**
 * The fragment's ListView.
 */
private AbsListView mListView;

/**
 * The Adapter which will be used to populate the ListView/GridView with
 * Views.
 */
private ListAdapter mAdapter;
private static String courseUrl;
private ParserJson parserJson = new ParserJson();
private TextView descriptionTextView;
private DescriptionData descriptionData;


// TODO: Rename and change types of parameters
public static LessonsFragment newInstance(String url) {
    LessonsFragment fragment = new LessonsFragment();
    courseUrl = url;
    return fragment;
}

/**
 * Mandatory empty constructor for the fragment manager to instantiate the
 * fragment (e.g. upon screen orientation changes).
 */
public LessonsFragment() {
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // TODO: Change Adapter to display your content
    mAdapter = new LessonsItemAdapter(getActivity(),
            R.layout.lessons_list_item, parserJson.getLessons(courseUrl).getLessonsArray());
    descriptionData = parserJson.getLessons(courseUrl).getDescriptionData();
}

//    @Override
//    public void onSaveInstanceState(Bundle outState) {
//        //No call for super(). Bug on API Level > 11.
//    }

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_lessons_list, container, false);

    descriptionTextView = (TextView) view.findViewById(R.id.lessons_description);
    descriptionTextView.setText("Описание курса:\n" +
            descriptionData.getShortDescription());

    // Set the adapter
    mListView = (AbsListView) view.findViewById(android.R.id.list);
    ((AdapterView<ListAdapter>) mListView).setAdapter(mAdapter);

    // Set OnItemClickListener so we can be notified on item clicks
    mListView.setOnItemClickListener(this);

    return view;
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {
        mListener = (OnFragmentInteractionListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString()
                + " must implement OnFragmentInteractionListener");
    }

}

@Override
public void onDetach() {
    super.onDetach();
    mListener = null;

    //this occurs when we move out of this Fragment
    replaceFragment(CatalogFragment.newInstance(1));

}

private void replaceFragment (Fragment fragment){
    String backStateName =  fragment.getClass().getName();
    String fragmentTag = backStateName;
    boolean fragmentPopped = getFragmentManager().popBackStackImmediate (backStateName, 0);

    if (!fragmentPopped && getFragmentManager().findFragmentByTag(fragmentTag) == null){ //fragment not in back stack, create it.
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.replace(R.id.container, fragment, fragmentTag);
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        ft.addToBackStack(backStateName);
        ft.commit();
    }
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    if (null != mListener) {
        mListener.onLessonFragmentInteraction(parserJson.getLessons(courseUrl).getLessonsArray().get(position).getId());
    }
}

/**
 * This interface must be implemented by activities that contain this
 * fragment to allow an interaction in this fragment to be communicated
 * to the activity and potentially other fragments contained in that
 * activity.
 * <p/>
 * See the Android Training lesson <a href=
 * "http://developer.android.com/training/basics/fragments/communicating.html"
 * >Communicating with Other Fragments</a> for more information.
 */
public interface OnFragmentInteractionListener {
    // TODO: Update argument type and name
    public void onLessonFragmentInteraction(String id);
}
}
公共类LessonsFragment扩展片段实现AblistView.OnItemClickListener{
私有OnFragmentInteractionListener mListener;
/**
*片段的列表视图。
*/
私有AbsListView-mListView;
/**
*用于填充ListView/GridView的适配器
*观点。
*/
私有列表适配器mAdapter;
私有静态字符串库;
private ParserJson ParserJson=new ParserJson();
私有文本视图descriptionTextView;
私有描述数据描述数据;
//TODO:重命名和更改参数类型
公共静态LessonsFragment newInstance(字符串url){
LessonsFragment=新的LessonsFragment();
courseUrl=url;
返回片段;
}
/**
*片段管理器实例化
*碎片(如屏幕方向改变时)。
*/
公共课片段{
}
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//TODO:更改适配器以显示您的内容
mAdapter=newlessonsitemadapter(getActivity(),
R.layout.lessons\u list\u项,parserJson.getLessons(courseUrl.getLessonsArray());
descriptionData=parserJson.getLessons(courseUrl.getDescriptionData();
}
//@覆盖
//SaveInstanceState上的公共无效(束超出状态){
////没有调用super()。API级别>11的Bug。
//    }
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图=充气机。充气(R.layout.fragment\u lessons\u列表,容器,false);
descriptionTextView=(TextView)view.findViewById(R.id.lessons\u description);
descriptionTextView.setText(“öПааааааааааааа+
descriptionData.getShortDescription());
//设置适配器
mListView=(AbsListView)view.findviewbyd(android.R.id.list);
((AdapterView)mListView).setAdapter(mAdapter);
//设置McClickListener,以便在单击项目时收到通知
mListView.setOnItemClickListener(此);
返回视图;
}
@凌驾
公共事务主任(活动){
超级转速计(活动);
试一试{
mListener=(OnFragmentInteractionListener)活动;
}catch(ClassCastException e){
抛出新的ClassCastException(activity.toString()
+“必须实现OnFragmentInteractionListener”);
}
}
@凌驾
公共无效连接(){
super.onDetach();
mListener=null;
//当我们离开这个片段时就会发生这种情况
replaceFragment(CatalogFragment.newInstance(1));
}
私有void replaceFragment(片段片段){
字符串backStateName=fragment.getClass().getName();
String fragmentTag=backStateName;
布尔fragmentPopped=getFragmentManager().popBackStackImmediate(backStateName,0);
如果(!fragmentPopped&&getFragmentManager().findFragmentByTag(fragmentTag)==null){//片段不在后堆栈中,请创建它。
FragmentTransaction ft=getFragmentManager().beginTransaction();
ft.replace(R.id.容器、碎片、碎片标签);
ft.setTransition(碎片事务处理、传输、碎片淡入淡出);
ft.addToBackStack(backStateName);
ft.commit();
}
}
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
if(null!=mListener){
mListener.onLessonFragmentInteraction(parserJson.getLessons(courseUrl.getLessonsArray().get(position.getId());
}
}
/**
*此接口必须由包含以下内容的活动实现
*片段,以允许通信此片段中的交互
*该活动以及其中可能包含的其他片段
*活动。
*

*有关更多信息,请参阅Android培训课程。 */ FragmentInteractionListener上的公共接口{ //TODO:更新参数类型和名称 public void onLessonFragmentInteraction(字符串id); } }

舱单:

<?xml version="1.0" encoding="utf-8"?>


>

片段的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment_lessons"
android:background="#FAF9F9"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.cbsystematic.mobile.itvdn.LessonsFragment">


<TextView
    android:id="@+id/lessons_description"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="#acacac" />

<ListView android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/lessons_description"/>

我知道这些问题就像脖子上的痛。但请给出你的假设


谢谢你的帮助

它崩溃是因为每次更改方向时都会重新创建片段

将此行添加到清单中的活动:

android:configChanges="keyboardHidden|orientation|screenSize"

由于日志行
java.lang.RuntimeException:无法销毁活动,您的错误导致
活动
,而不是
片段
。由于在onSaveInstanceState之后日志行
无法执行此操作,您在
onDestrony
(在
super
之前)或更早版本的
活动中的smth错误
android:configChanges="keyboardHidden|orientation|screenSize"