简体   繁体   English

活动中的午餐片段时崩溃

[英]crash when lunching fragment from activity

I'm building media player and i want to lunch fragment to control actions like play/pause when user play audio file but it didn't work and throw this exception:我正在构建媒体播放器,我想午餐片段来控制用户播放音频文件时的播放/暂停等操作,但它不起作用并抛出此异常:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mediaplayer, PID: 1092
java.lang.IllegalStateException: Activity has been destroyed
    at androidx.fragment.app.FragmentManagerImpl.enqueueAction(FragmentManagerImpl.java:1566)
    at androidx.fragment.app.BackStackRecord.commitInternal(BackStackRecord.java:317)
    at androidx.fragment.app.BackStackRecord.commit(BackStackRecord.java:282)
    at com.example.mediaplayer.MainActivity.doActionToFile(MainActivity.java:126)
    at com.example.mediaplayer.RecyclerViewAdapter$FileLayoutHolder.onClick(RecyclerViewAdapter.java:68)

this is code i use to lunch the fragment from static method in mainActivity (this method is called when use click on audio item):这是我用来午餐 mainActivity 中 static 方法的片段的代码(使用单击音频项时调用此方法):

AudioPlayerFragment player = AudioPlayerFragment.newInstance();
           FragmentActivity fragmentActivity = new FragmentActivity();
           FragmentManager manager = fragmentActivity.getSupportFragmentManager();
           FragmentTransaction transaction = manager.beginTransaction();
           transaction.add(R.id.fragment_container,player);
           transaction.addToBackStack(null);
           transaction.commit();

and this is AudioPlayerFragment class:这是 AudioPlayerFragment class:

public class AudioPlayerFragment extends Fragment {
    public AudioPlayerFragment(){}

    public static AudioPlayerFragment newInstance(){return new AudioPlayerFragment();}

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.audio_control_layout, container, false);
        return view;
    }
}

this is main activity layout witch i use Framelayout to be fragment's container:这是我使用 Framelayout 作为片段容器的主要活动布局:

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

<androidx.viewpager.widget.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/pager"
    android:layout_height="match_parent"
    android:layout_width="match_parent">

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <FrameLayout
        android:id="@+id/fragment_container"
        android:name="SimpleFragment"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:layout="@layout/audio_control_layout">
    </FrameLayout>

</androidx.viewpager.widget.ViewPager>

and this is audio_control_layout:这是audio_control_layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <ToggleButton
        android:id="@+id/tb_love"
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_margin="@dimen/space16"
        android:textOff=""
        android:textOn=""
        android:button="@drawable/toggle_button"
        android:background="@null"/>
    <LinearLayout
        android:id="@+id/ll_bottom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"
        android:padding="@dimen/space16"
        android:weightSum="5">
        <ImageButton
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@drawable/ic_repeat"
            android:background="@null"/>
        <ImageButton
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@drawable/ic_backward"
            android:background="@null"/>
        <ImageButton
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@drawable/ic_play"
            android:background="@null"/>

        <ImageButton
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@drawable/ic_forward"
            android:background="@null"/>

        <ImageButton
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@drawable/ic_shuffle"
            android:background="@null"/>
    </LinearLayout>
    <RelativeLayout
        android:id="@+id/rl_progress"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/ll_bottom"
        android:layout_margin="@dimen/space16">
        <TextView
            android:id="@+id/tv_current_time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:text="2:01"
            android:textSize="14sp"
            android:textColor="@color/colorTextGrey"/>
        <TextView
            android:id="@+id/tv_full_time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:text="3:47"
            android:textSize="14sp"
            android:textColor="@color/colorTextGrey" />
        <ProgressBar
            android:layout_width="match_parent"
            android:layout_height="@dimen/space8"
            android:layout_toLeftOf="@id/tv_full_time"
            android:layout_toRightOf="@id/tv_current_time"
            android:layout_marginLeft="@dimen/space16"
            android:layout_marginRight="@dimen/space16"
            android:layout_centerVertical="true"
            android:progressBackgroundTint="#E9E9E9"
            android:progress="50"
            android:indeterminate="false"
            style="@style/Widget.AppCompat.ProgressBar.Horizontal"
            android:progressDrawable="@drawable/progress_bar"/>
    </RelativeLayout>
    <TextView
        android:id="@+id/tv_artist"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/rl_progress"
        android:layout_centerHorizontal="true"
        android:text="by From First to Last "
        android:textSize="14sp"
        android:textColor="@color/colorTextGrey" />
    <TextView
        android:id="@+id/tv_song"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/tv_artist"
        android:layout_marginBottom="@dimen/space8"
        android:layout_centerHorizontal="true"
        android:text="Make War"
        android:textSize="18sp"
        android:textColor="@color/colorTextDark" />
  <!--  <androidx.viewpager.widget.ViewPager
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/pager_audio"
        android:layout_height="match_parent"
        android:layout_width="match_parent" >-->

    <ImageView
        android:layout_above="@id/tv_song"
        android:layout_below="@id/tb_love"
        android:layout_marginTop="@dimen/space16"
        android:layout_marginBottom="@dimen/space16"
        android:src="@drawable/album2"
        android:id="@+id/iv_cover"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_gravity="center"/>
    <!--</androidx.viewpager.widget.ViewPager>-->

</RelativeLayout>

it is right to use fragment or use another activity?使用片段或使用其他活动是否正确? what do normal music player use to control over this actions activity or fragment?普通音乐播放器用什么来控制这个动作活动或片段?

You can call this.getSupportFragmentManager();你可以调用this.getSupportFragmentManager(); in the doActionToFile method since the frameLayout is already a part of your mainActivity no need to instantiate a new activity.doActionToFile方法中,因为 frameLayout 已经是 mainActivity 的一部分,因此无需实例化新活动。 You need to use the contextualized manager this is probably the main cause of the crash.您需要使用上下文管理器,这可能是导致崩溃的主要原因。

If you want to create a new activity you need to use the normal lifecycle methods, I would suggest using first Intent and creating a custom Activity class that does the Fragment transaction in the onCreate or onResume method of the activity.如果你想创建一个需要使用正常生命周期方法的新活动,我建议使用第一个Intent并创建一个自定义 Activity class,它在活动的 onCreate 或 onResume 方法中执行 Fragment 事务。 Activity appears destroyed because it does not exist. Activity 似乎已被破坏,因为它不存在。 Also maybe you can try using a DialogFragment so you don't require creating a whole activity.另外,也许您可以尝试使用DialogFragment ,这样您就不需要创建整个活动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM