集成flutter_stripe与just_audio_background冲突的异常解决求助
我正在开发一款使用just_audio和just_audio_background插件的音乐App,原本运行正常,但集成flutter_stripe插件后,点击结账按钮时抛出异常:主Activity类com.ryanheise.audioservice.AudioServiceActivity不是FlutterFragmentActivity的子类。我已自定义MainActivity继承FlutterFragmentActivity,但AndroidManifest中配置的启动Activity为AudioServiceActivity,尝试直接在Manifest中定义自定义Activity也无效,求帮助。
异常信息
Your Main Activity class com.ryanheise.audioservice.AudioServiceActivity is not a subclass FlutterFragmentActivity.
MainActivity.kt
package com.example.spotify import io.flutter.embedding.android.FlutterActivity import io.flutter.embedding.android.FlutterFragmentActivity class MainActivity: FlutterFragmentActivity() { }
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.spotify" xmlns:tools="http://schemas.android.com/tools"> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.WAKE_LOCK"/> <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/> <application android:label="spotify" android:name="${applicationName}" android:icon="@mipmap/ic_launcher" android:usesCleartextTraffic="true"> <activity android:name="com.ryanheise.audioservice.AudioServiceActivity" android:exported="true" android:launchMode="singleTop" android:theme="@style/LaunchTheme" android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode" android:hardwareAccelerated="true" android:windowSoftInputMode="adjustResize"> <meta-data android:name="io.flutter.embedding.android.NormalTheme" android:resource="@style/NormalTheme" /> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> <!-- Don't delete the meta-data below. This is used by the Flutter tool to generate GeneratedPluginRegistrant.java --> <meta-data android:name="flutterEmbedding" android:value="2" /> <service android:name="com.ryanheise.audioservice.AudioService" android:foregroundServiceType="mediaPlayback" android:exported="true" tools:ignore="Instantiatable"> <intent-filter> <action android:name="android.media.browse.MediaBrowserService" /> </intent-filter> </service> <!-- ADD THIS "RECEIVER" element --> <receiver android:name="com.ryanheise.audioservice.MediaButtonReceiver" android:exported="true" tools:ignore="Instantiatable"> <intent-filter> <action android:name="android.intent.action.MEDIA_BUTTON" /> </intent-filter> </receiver> </application> </manifest>
解决方案
方法1:调整自定义MainActivity的继承关系并设为启动页
just_audio的AudioServiceActivity本身基于FlutterActivity,让自定义MainActivity继承它,既能满足音乐后台播放需求,又符合flutter_stripe对FlutterFragmentActivity的要求:
- 修改
MainActivity.kt的继承类:
package com.example.spotify import com.ryanheise.audioservice.AudioServiceActivity class MainActivity : AudioServiceActivity() { }
- 修改
AndroidManifest.xml中的启动Activity为自定义的MainActivity:
<activity android:name=".MainActivity" <!-- 替换原有的com.ryanheise.audioservice.AudioServiceActivity --> android:exported="true" android:launchMode="singleTop" android:theme="@style/LaunchTheme" android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode" android:hardwareAccelerated="true" android:windowSoftInputMode="adjustResize"> <meta-data android:name="io.flutter.embedding.android.NormalTheme" android:resource="@style/NormalTheme" /> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity>
注意:保留原有的AudioService和MediaButtonReceiver配置不变,这是just_audio后台播放的必需项。
方法2:强制覆盖Activity配置(方法1无效时用)
如果直接替换Activity名称出现配置冲突,在自定义MainActivity的manifest节点中添加tools:replace="android:name",确保系统优先使用你的自定义Activity:
<activity android:name=".MainActivity" android:exported="true" android:launchMode="singleTop" android:theme="@style/LaunchTheme" android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode" android:hardwareAccelerated="true" android:windowSoftInputMode="adjustResize" tools:replace="android:name"> <!-- 新增这一行 --> <!-- 其他meta-data和intent-filter保持不变 --> </activity>
验证步骤
- 执行
flutter clean清理项目缓存 - 重新运行项目
flutter run - 测试两个核心功能:音乐后台播放是否正常,点击结账按钮是否还会抛出异常
内容的提问来源于stack exchange,提问作者Arsal Malik

