[英]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.