我有一个版本为 2.2.3 的 Flutter 应用程序,我最近使用平台通道方法将本机 SDK 集成到该应用程序中。出于某种原因,当我在模拟器或实际设备上运行时,我的 Flutter 应用程序开始显示黑屏。该应用程序卡在黑屏上,无法继续显示主屏幕。这是我的主文件代码:
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
InAppPurchaseAndroidPlatformAddition.enablePendingPurchases();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
final Future<FirebaseApp> _initialization = Firebase.initializeApp();
@override
Widget build(BuildContext context) {
SystemChrome.setEnabledSystemUIOverlays([]);
SystemChrome.setPreferredOrientations(
[DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]);
print('Inside main.dart build method');
return FutureBuilder(
future: _initialization,
builder: (context2, snapshot) {
if (snapshot.hasError) {
NetworkErr();
return Container();
} else if (snapshot.connectionState == ConnectionState.done) {
return MultiProvider(
providers: [
ChangeNotifierProvider(
create: (ctx) => ClueList(),
),
ChangeNotifierProvider(
create: (ctx) => Score(),
),
ChangeNotifierProvider(
create: (ctx) => Auth(),
),
ChangeNotifierProvider(
create: (ctx) => Badges(),
),
],
child: MaterialApp(
theme: ThemeData(
primarySwatch: Colors.teal,
// primaryColor: Color.fromARGB(150, 35, 152, 159,),
primaryColorLight: Color.fromARGB(
80,
35,
152,
159,
),
accentColor: Color.fromARGB(255, 255, 185, 36),
textTheme: ThemeData.light().textTheme.copyWith(
bodyText2: TextStyle(
fontFamily: 'Happy School',
fontSize: 20,
))),
home: HomeScreen(),
在 MainActivity.java 文件中,我集成了本机 sdk,我在其中调用以从 Yodo1 MAS 检索广告 下面是 MainActivity.java 文件中的代码。
package com.example.xxxxx;
import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.NonNull;
import com.yodo1.mas.Yodo1Mas;
import com.yodo1.mas.error.Yodo1MasError;
import com.yodo1.mas.event.Yodo1MasAdEvent;
import io.flutter.app.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugins.GeneratedPluginRegistrant;
public class MainActivity extends FlutterActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
System.out.println("before Calling MAS Get Instance");
Yodo1Mas.getInstance().init(this, "xxxxxxxxx", new Yodo1Mas.InitListener() {
@Override
public void onMasInitSuccessful() {
System.out.println("MAS Init Successful");
}
@Override
public void onMasInitFailed(@NonNull Yodo1MasError error) {
}
});
new MethodChannel(getFlutterView(), "com.examples.xxxxxx/yodo1").setMethodCallHandler(
new MethodCallHandler() {
@Override
public void onMethodCall(MethodCall call, Result result) {
if (call.method.equals("getBannerAd")) {
System.out.println("RADHA LOading Banner Ad");
getBannerAd();
}
}
}
);
}
private void getBannerAd() {
int align = Yodo1Mas.BannerTop | Yodo1Mas.BannerHorizontalCenter;
boolean isLoaded = Yodo1Mas.getInstance().isBannerAdLoaded();
if (isLoaded) {
Yodo1Mas.getInstance().showBannerAd(MainActivity.this, align);
Yodo1Mas.getInstance().setBannerListener(new Yodo1Mas.BannerListener() {
@Override
public void onAdOpened(@NonNull Yodo1MasAdEvent event) {
}
@Override
public void onAdError(@NonNull Yodo1MasAdEvent event, @NonNull Yodo1MasError error) {
}
@Override
public void onAdClosed(@NonNull Yodo1MasAdEvent event) {
}
}
);
}
}
}
这是我的 AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.xxxxx">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"/>
<uses-permission android:name="com.android.vending.BILLING" />
<application
android:name="io.flutter.app.FlutterApplication"
android:icon="@mipmap/ic_launcher"
android:usesCleartextTraffic="false"
android:networkSecurityConfig="@xml/network_security_config">
<activity
android:name=".MainActivity"
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"
/>
<meta-data
android:name="io.flutter.embedding.android.SplashScreenDrawable"
android:resource="@drawable/launch_background"
/>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
<action android:name="FLUTTER_NOTIFICATION_CLICK" />
<category android:name="android.intent.category.DEFAULT" />
</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" />
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-xxxxxxxxxxxxxxxxxxxxxxxxxx"/>
<!-- <meta-data
android:name="com.google.android.gms.games.APP_ID"
android:value="@string/app_id" /> -->
<!-- <meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" /> -->
</application>
</manifest>
我还尝试了以下方法:
- 我跑了
flutter pub cache repair
- 我最初的项目是 Flutter + Kotlin 项目。由于集成本机 sdk 的代码仅在 Java 中可用,因此我通过将文件夹结构从 src/main/kotlin 移动到 src/main/java 将项目转换为 Java 项目,如下所述: Convert an existing Flutter Kotlin project to颤振Java项目
我已经坚持了3天多了。我搜索了几乎所有在 SOF 上找到的黑屏解决方案,但无济于事。如果您有任何其他建议,请告诉我。谢谢。