java.lang.RuntimeException: Unable to instantiate activity in Flutter
Asked Answered
D

10

25

Flutter newly created app gives error when using fire base.

How should i fix this problem as it is asking flutter app main activity cannot be cast to android app activity.

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) 

This is main dart file , no changes are made here.

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),
      ), 
    );
  }
}

This is android src Main activity 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() {


    }

I only opened a new flutter project and followed the fire base steps to add dependencies and google-services.json file to project.

It later was giving firebase in it error so i added multidex.

In the main dart file of flutter application and android module Main activity no changes were made.

Is it some thing i did wrong , i am new to flutter so i am not understanding.

Defamatory answered 5/4, 2020 at 16:58 Comment(4)
Could you post your code?Rosenzweig
which files to post , app is new with no code change just added firebase depencies and google_services.json.Defamatory
any help or some thing i am missingDefamatory
Added the splash screen, then it is working for me.Toccaratoccata
B
52

In order to solve this error, I had to update the package name in the MainActivity file in the Kotlin folder of my flutter app. From the output of the console I knew that somewhere the default name still existed somewhere in my app, and that this was causing an error. Updating the package name in the MainActivity file solved this.

Boomerang answered 29/10, 2020 at 14:25 Comment(4)
This is what the problem was for me as well, and flutter clean did not help in this case, but changing the package name in the MainActivity.kt file did.Ylangylang
This also solved the issue for me. I however went a few steps further. I erased the files for the android build in the build folder. I saw my project name, then updated the name in: android>app>src>main>AndroidManifest.xml, then also in android>app>src>debug>AndroidManifest.xml. And just to be safe in android>app>src>main>kotlin>com>example>my_app>MainActivity Doing this solved the issue.Odericus
To handle this issue you can use this package: pub.dev/packages/change_app_package_nameDeterminate
For future viewers, the package @Abbas mentions works really well, but for me it didn't catch absolutely everything, so do a manual cmd + f to ensure you get everything!Vanillic
R
22

The error occurs when you change the package name. It doesn't sync with Kotlin

Steps:

  • Go to android>app>src>main>kotlin>MainActivity
  • Change the package name to the respective package name.
Rapparee answered 8/9, 2021 at 13:6 Comment(3)
this solved the issue, which was caused by manually renaming the manifest files from the default com.example.appname to the custom oneSinewy
Sometimes when also your package name doesn't match with the AndroidManifest.xmlSophiasophie
Thank you very much. This solved the issue as I had changed my application identifier!Necrose
F
5

Because your MainActivity is child of FlutterApplication not FlutterActivity. Modify the MainActivity as following

import io.flutter.embedding.android.FlutterActivity

class MainActivity: FlutterActivity() {
}

Forthcoming answered 17/5, 2020 at 9:49 Comment(1)
This worked for me, thank you! During my last flutter update for some reason a bunch of other code was inserted in my MainActivity.kt file, so I just had to remove it and replace it with your code.Andizhan
A
3

In my case command flutter clean and flutter pub get works for me

Try it out.

Annalist answered 5/6, 2022 at 9:33 Comment(0)
D
2

One of the problems is bad V2 embedding. No need to change your package name. Try this in the manifest.xml in <activity> attribute

android:name="io.flutter.embedding.android.FlutterActivity"

and in the <application> attribute

android:name="${applicationName}"
Deviationism answered 5/10, 2022 at 11:33 Comment(0)
B
1

Update the folders containing your MainActivity.kt with the package name in the MainActivity.kt enter image description here

Birdie answered 11/2, 2022 at 5:17 Comment(0)
C
1
<application android:allowBackup="false" android:name="${applicationName}">

In the AndroidManisfest.xml update the tag application with the property android:name with "${applicationName}" like the code above. For me this issue was solved with this.

Conjunction answered 15/3, 2022 at 23:33 Comment(0)
P
1

2023:

I had the same issue. For me, it was because I had the Kotlin file (main activity) who had the wrong package name.The name was com.example.myapp instead of com.company.myapp. I have changed the name later, because I have to upload app to Play Store.

Solution:

Change the package name using this package - here (change_app_package_name)

This will change the package name of all places. It worked for me.

Pliner answered 7/7, 2023 at 15:31 Comment(0)
V
1

Correcting the default package name with the actual one in the file android/app/src/main/kotlin/com/example/options_app/MainActivity.kt solved the problem for me.

Voluptuous answered 11/10, 2023 at 13:19 Comment(0)
P
0

Thank you to everyone who has contributed an answer. I came across this error after overwriting files within cloud functions, later to discover that I had two of a certain folder and that I needed to delete the extra since it was there where Flutter was trying to run (incorrectly) ... Paying attention to Aadn's response, I also wanted to fix details within the three AndroidManifest files (there's one within android/app/src/debug, another within android/app/src/main, and another within android/app/src/profile) ... Whilst doing this I noticed that there's a folder that the MainActivity.kt file must refer to -- the package mentioned in MainActivity.kt must match this folder, like it is where things are going on ... To see the folder notice project_name/android/app/src/main/kotlin ... The folder there is the package that the MainActivity.kt file must mention, and it's the same package that the AndroidManifest.xml files should mention too ... For me, deleting the extra folder and then ensuring the correct package name would be called, solved this issue ... My apology for the lengthiness, but I hope it helps you to understand and to move forwards : )

Peters answered 12/12, 2022 at 14:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.