Linkfish
Linkfish

Reputation: 55

Android Studio: error: incompatible types: MainActivity cannot be converted to Application

I have an issue with building my app that I am unable to solve, and I need some help.

I have a simple class, MainActivity.java It's a very basic app to test receiving push messages with through FCB. I am now also trying to implement an SDK for a third party, to send and receive Push through, which is where I get stuck.

Here's the code of my class:

package com.emarsys.tcspushtestapp;

import android.app.Application;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.emarsys.mobileengage.MobileEngage;
import com.emarsys.mobileengage.config.MobileEngageConfig;
import com.google.firebase.iid.FirebaseInstanceId;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btnShowToken = (Button)findViewById(R.id.button_show_token);
        btnShowToken.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Get the token
                String token = FirebaseInstanceId.getInstance().getToken();
                Log.d(TAG, "Token: + " + token);
                Toast.makeText(MainActivity.this, token, Toast.LENGTH_SHORT).show();
            }
        });

        MobileEngageConfig config = new MobileEngageConfig.Builder()
                .application(this)
                .credentials(String, String)
                .enableDefaultChannel("MainChannel", "Default Channel")
                //.disableDefaultChannel()
                .build();
        MobileEngage.setup(config);
    }
}

This issue sits with the:

.application(this) , in which (this) has a red error line under it stating:

application(android.app.Application) in builder cannot be applied to (com.emarsys.tcspushtestapp.MainActivity)

Compilation error: error: incompatible types: MainActivity cannot be converted to Application

When I change my class to extend to Application, instead of AppCompatActivity, the error disappears but new errors appear on the .onCreate, setContentView and findViewById.

Other things I have tried is: public class MainActivity extends AppCompatActivity implements Application

It then complains it is expecting an interface for Application though.

Any help is very much appreciated!

Thank you. Kind regards, MD

Upvotes: 2

Views: 11000

Answers (5)

KevinSom98
KevinSom98

Reputation: 1

try using this

class MainActivity: FlutterActivity() {

override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
    super.configureFlutterEngine(flutterEngine)
    GeneratedPluginRegistrant.registerWith(flutterEngine)
}

}

Upvotes: 0

Nadeem Abbas
Nadeem Abbas

Reputation: 15

Remove the below code in manifest file

meta-data android:name="flutterEmbedding" 
android:value="2"

run your project again, it will work fine

Upvotes: 0

Saleh Abdullabəyli
Saleh Abdullabəyli

Reputation: 47

you must replace MainActivity.this to new FlutterEngine(this);

Upvotes: 2

Linkfish
Linkfish

Reputation: 55

Hitesh Sarsava's post fixed my problem.

.application(getApplication()) solved the issue, so I will use his answer as the solution.

Everyone else's posts were also great and helped me understand the issue on a more in depth scale, so thanks everyone!

Upvotes: 0

Zun
Zun

Reputation: 1681

this refers to MainActivity. Activity is of type Context. The MobileEngageConfig builder wants an instance of your Application class. Use getApplication(); instead

edit: the tutorial you followed most likely used the builder method in a custom Application class, hence why they used this.

Upvotes: 4

Related Questions