java - 活动中的午餐片段时崩溃
问题描述
我正在构建媒体播放器,我想午餐片段来控制用户播放音频文件时的播放/暂停等操作,但它不起作用并抛出此异常:
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 中的静态方法中午餐片段的代码(使用单击音频项时调用此方法):
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 类:
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 类,该类在活动的 onCreate 或 onResume 方法中执行 Fragment 事务。Activity 似乎已被破坏,因为它不存在。另外,也许您可以尝试使用DialogFragment,这样您就不需要创建整个活动。
推荐阅读
- azure - Cosmos Db CORS 设置不尊重允许来源中的 *
- xml - 在看似非空的对象上编辑 Powershell 中的 XML 属性 - “您不能在空值表达式上调用方法”
- zend-framework - 如何在zend框架3中的实体管理器配置原则中添加功能
- android-studio - Dart 2 中的 AndroidStudio 代码折叠和可选的新关键字
- .net - 我们应该使用 Reactive Extension 的 Observable 模式来运行多个依赖的异步任务吗?
- javascript - mySQL 8 UUID_TO_BIN 和 BIN_TO_UUID 与sailsjs
- python - 如何使用 xlrd/openpyxl 或任何其他包检查 Excel 工作表中是否存在页眉/页脚
- c - 复制一个wav文件
- android - 滚动视图和 layout_constraintHeight_max 使视图消失
- java - 通过 XML 进行第二次循环的空指针?