Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.RuntimeException: Unable to instantiate activity in Flutter

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.

like image 895
demitri kozlov Avatar asked Apr 05 '20 16:04

demitri kozlov


5 Answers

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.

like image 173
Jon Avatar answered Nov 02 '22 18:11

Jon


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.
like image 20
Raj A Avatar answered Nov 02 '22 17:11

Raj A


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

import io.flutter.embedding.android.FlutterActivity

class MainActivity: FlutterActivity() {
}

like image 29
Sanjay Sharma Avatar answered Nov 02 '22 18:11

Sanjay Sharma


<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.

like image 35
Bruno Alves Avatar answered Nov 02 '22 17:11

Bruno Alves


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

Try it out.

like image 43
Amazed Avatar answered Nov 02 '22 17:11

Amazed