簡體   English   中英

活動中的午餐片段時崩潰

[英]crash when lunching fragment from activity

我正在構建媒體播放器,我想午餐片段來控制用戶播放音頻文件時的播放/暫停等操作,但它不起作用並拋出此異常:

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)

這是我用來午餐 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();

這是 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;
    }
}

這是我使用 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>

這是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>

使用片段或使用其他活動是否正確? 普通音樂播放器用什么來控制這個動作活動或片段?

你可以調用this.getSupportFragmentManager(); doActionToFile方法中,因為 frameLayout 已經是 mainActivity 的一部分,因此無需實例化新活動。 您需要使用上下文管理器,這可能是導致崩潰的主要原因。

如果你想創建一個需要使用正常生命周期方法的新活動,我建議使用第一個Intent並創建一個自定義 Activity class,它在活動的 onCreate 或 onResume 方法中執行 Fragment 事務。 Activity 似乎已被破壞,因為它不存在。 另外,也許您可以嘗試使用DialogFragment ,這樣您就不需要創建整個活動。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM