更改方向崩溃应用程序导致android.support.v4.app.ListFragment

时间:2017-12-14 18:42:46

标签: java android android-support-library android-listfragment

当我将android.support.v4.app.ListFragment替换为android.app.ListFragment时,我不知道为什么在使用ListFragment(支持版)时应用程序会因方向更改而崩溃。每件事都很好。我不想使用这种方法"android:configChanges="orientation"。 作为我的问题的解决方案。

  • 当我在启动应用程序后第一次旋转设备时(从纵向到陆地)每次 事情很好
  • 但又一次(陆地到肖像) 崩溃崩溃

我认为问题在于支持lib而不是我的代码。

MainFragment类代码:

package com.workout;


import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;


public class MainFragment extends ListFragment {

    public MainFragment() {}


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setListAdapter(new ArrayAdapter<>(getActivity(),

   android.R.layout.simple_list_item_1,WorkoutDetail.workoutDetails));

        if ( getListView() != null )
            getListView().setOnItemClickListener((MainActivity)getActivity());

    }
}

MainActivity Code:

package com.workout;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;


public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener{


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

       // Intent intent = new Intent(this,DetailActivity.class);
       // intent.putExtra(Intent.EXTRA_TEXT,id);
       // startActivity(intent);
    }
}

MainActivity XML:

<?xml version="1.0" encoding="utf-8"?>
<fragment
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:name="com.workout.MainFragment"/>

Logcat详细信息:

12-14 22:17:58.383 7159-7159/com.workout E/AndroidRuntime: FATAL EXCEPTION: main
                                                           Process: com.workout, PID: 7159
                                                           java.lang.IllegalStateException: Fragment has not been attached yet.
                                                               at android.support.v4.app.Fragment.instantiateChildFragmentManager(Fragment.java:2308)
                                                               at android.support.v4.app.Fragment.getChildFragmentManager(Fragment.java:773)
                                                               at android.support.v4.app.FragmentActivity.markState(FragmentActivity.java:967)
                                                               at android.support.v4.app.FragmentActivity.onSaveInstanceState(FragmentActivity.java:527)
                                                               at android.support.v7.app.AppCompatActivity.onSaveInstanceState(AppCompatActivity.java:509)
                                                               at android.app.Activity.performSaveInstanceState(Activity.java:1419)
                                                               at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1301)
                                                               at android.app.ActivityThread.callCallActivityOnSaveInstanceState(ActivityThread.java:4637)
                                                               at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4588)
                                                               at android.app.ActivityThread.-wrap19(ActivityThread.java)
                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1514)
                                                               at android.os.Handler.dispatchMessage(Handler.java:102)
                                                               at android.os.Looper.loop(Looper.java:241)
                                                               at android.app.ActivityThread.main(ActivityThread.java:6274)
                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

我已经访问了很多stackoverflow链接,但未能找到问题的解决方案。

1 个答案:

答案 0 :(得分:1)

尝试从活动中删除侦听器实现和onItemClick,并使用与listfragment一起存在的onListItemClick,您不需要在listfragment上实现侦听器,只需使用下面的方法,并希望这可以解决您的问题。

public class MainFragment extends ListFragment {

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);

    // put your code here
}
}

在上面做了以下(我已经测试了这个并且它正在工作) 按如下方式构建新的XLM文件(复制和粘贴)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

 <FrameLayout
    android:id="@+id/FragmentSlot"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</LinearLayout>

然后将您的片段动态添加到活动中,如下所示:

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // Begin the transaction
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    // Replace the contents of the container with the new fragment
    ft.replace(R.id.FragmentSlot, new MainFragment());
    // or ft.add(R.id.your_placeholder, new FooFragment());
    // Complete the changes added above
    ft.commit();
}
}

这确实可以解决你的问题。

是的,你是对的,它是从XLM膨胀片段时系统相关的东西。

为了避免将来你需要学习如何使用片段管理器和事务来添加,替换和删除片段。

选中此课程,包含三节课:https://developer.android.com/training/basics/fragments/index.html