dem*_*lov 6 classcastexception firebase flutter android-module
Flutter 新创建的应用程序在使用 fire base 时出错。
我应该如何解决这个问题,因为它要求 Flutter 应用程序主活动无法转换为 android 应用程序活动。
04-05 21:16:07.570 27391-27391/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.firestoreflutterchat, PID: 27391
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.firestoreflutterchat/com.example.firestoreflutterchat.MainActivity}: java.lang.ClassCastException: com.example.firestoreflutterchat.MainActivity cannot be cast to android.app.Activity
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2124)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
at android.app.ActivityThread.access$800(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5097)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassCastException: com.example.firestoreflutterchat.MainActivity cannot be cast to android.app.Activity
at android.app.Instrumentation.newActivity(Instrumentation.java:1084)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2115)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
at android.app.ActivityThread.access$800(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5097)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Run Code Online (Sandbox Code Playgroud)
这是主要的 dart 文件,这里没有做任何更改。
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
这是 android src 主要活动 kotlin :
package com.example.firestoreflutterchat
import android.content.Context
//import androidx.multidex.MultiDex
import io.flutter.app.FlutterApplication
import io.flutter.embedding.android.FlutterActivity
class MainActivity : FlutterApplication() {
}
Run Code Online (Sandbox Code Playgroud)
我只打开了一个新的 flutter 项目,并按照 fire base 步骤将依赖项和 google-services.json 文件添加到项目中。
后来它在其中给出了 firebase 错误,所以我添加了 multidex。
在 flutter 应用程序的主要 dart 文件和 android 模块 Main 活动中没有进行任何更改。
是不是我做错了什么,我是新手,所以我不明白。
Raj*_*j A 18
更改包名称时会出现此错误。它与 Kotlin 不同步
脚步:
小智 16
为了解决这个错误,我不得不更新我的 Flutter 应用程序 Kotlin 文件夹中 MainActivity 文件中的包名。从控制台的输出中,我知道我的应用程序中某处仍然存在默认名称,这导致了错误。更新 MainActivity 文件中的包名解决了这个问题。
因为你是不是MainActivity的孩子。修改如下FlutterApplicationFlutterActivityMainActivity
import io.flutter.embedding.android.FlutterActivity
class MainActivity: FlutterActivity() {
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7134 次 |
| 最近记录: |