react-native-splash-screen icon indicating copy to clipboard operation
react-native-splash-screen copied to clipboard

incompatible types: MainApplication cannot be converted to Activity

Open matheusbaumgart opened this issue 5 years ago • 7 comments

Run react-native info in your project and share the content.

System:
    OS: macOS 10.15.3
    CPU: (4) x64 Intel(R) Core(TM) i5-7600K CPU @ 3.80GHz
    Memory: 310.18 MB / 24.00 GB
    Shell: 5.7.1 - /bin/zsh
  Binaries:
    Node: 13.3.0 - /usr/local/bin/node
    Yarn: 1.21.1 - /usr/local/bin/yarn
    npm: 6.13.4 - /usr/local/bin/npm
    Watchman: 4.9.0 - /usr/local/bin/watchman
  SDKs:
    iOS SDK:
      Platforms: iOS 13.2, DriverKit 19.0, macOS 10.15, tvOS 13.2, watchOS 6.1
    Android SDK:
      API Levels: 22, 23, 24, 25, 26, 27, 28, 29
      Build Tools: 23.0.1, 26.0.1, 26.0.2, 27.0.3, 28.0.3, 29.0.1, 29.0.2, 29.0.3
      System Images: android-26 | Google Play Intel x86 Atom, android-28 | Intel x86 Atom_64, android-28 | Google APIs Intel x86 Atom, android-28 | Google Play Intel x86 Atom, android-29 | Google APIs Intel x86 Atom, android-29 | Google Play Intel x86 Atom
      Android NDK: 20.0.5594570
  IDEs:
    Android Studio: 3.5 AI-191.8026.42.35.6010548
    Xcode: 11.3.1/11C504 - /usr/bin/xcodebuild
  npmPackages:
    @react-native-community/cli: ^2.9.0 => 2.10.0 
    react: 16.9.0 => 16.9.0 
    react-native: 0.61.1 => 0.61.1 
  npmGlobalPackages:
    create-react-native-app: 1.0.0
    react-native-cli: 2.0.1
    react-native-git-upgrade: 0.2.7
    react-native-macos-cli: 2.0.1
    react-native-scripts: 1.8.1

What react-native-splash-screen version are you using? ^3.2.0

What platform does your issue occur on? (Android/iOS/Both) Android

Describe your issue as precisely as possible :

  1. Followed README and ran react-native run-android. The build brakes.
  2. Error message: incompatible types: MainApplication cannot be converted to Activity

Show us the code you are using? My MainApplication.java:

import android.app.Application
import com.facebook.react.ReactApplication;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.react.shell.MainReactPackage;
import com.facebook.soloader.SoLoader;
import com.facebook.react.PackageList;
import com.calendarevents.CalendarEventsPackage;
import org.devio.rn.splashscreen.SplashScreen;

import java.util.List;

public class MainApplication extends Application implements ReactApplication {

  private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
    @Override
    public boolean getUseDeveloperSupport() {
      return BuildConfig.DEBUG;
    }

    @Override
    protected List<ReactPackage> getPackages() {
      @SuppressWarnings("UnnecessaryLocalVariable")
      List<ReactPackage> packages = new PackageList(this).getPackages();
      // additional non auto detected packages can still be added here:
      new CalendarEventsPackage();
      // packages.add(new SomeReactNativePackage());
      return packages;
    }

    @Override
    protected String getJSMainModuleName() {
      return "index";
    }
  };

  @Override
  public ReactNativeHost getReactNativeHost() {
    return mReactNativeHost;
  }

  @Override
  public void onCreate() {
    SplashScreen.show(this);
    super.onCreate();
    SoLoader.init(this, /* native exopackage */ false);
  }
}

matheusbaumgart avatar Feb 19 '20 22:02 matheusbaumgart

Hi, you might want to add the SplashScreen.show(this); in MainActivity.java instead of MainApplication.java. Just like this: Screen Shot 2020-02-19 at 19 01 21

Hope this helps!

ohhyunjin avatar Feb 20 '20 03:02 ohhyunjin

Hi, everyone!

I fix this issue today using a different approach since the fix above didn't worked for me:

Just replace the this inside of the show method for SplashScreen.show((Activity) getContext());

clucasalcantara avatar Nov 06 '22 21:11 clucasalcantara

(Activity) getContext()

Thanks that helped a lot. However I add to change the (Activity) to (MainActivity).

tJouet avatar Nov 18 '22 15:11 tJouet

Hi, everyone!

I fix this issue today using a different approach since the fix above didn't worked for me:

Just replace the this inside of the show method for SplashScreen.show((Activity) getContext());

Didn't work for me. Can you show me that piece of code how did you solve this issue?

imuhammadnadeem avatar Dec 30 '22 13:12 imuhammadnadeem

(Activity) getContext())

You might want to add import android.app.Activity; to the imports at the top of your MainActivity.java

awalmubarak avatar Jan 20 '23 01:01 awalmubarak

Sorry the late reply @Red-system! Here follows my MainActivity.java


import com.facebook.react.ReactActivity;
import org.devio.rn.splashscreen.SplashScreen;

import android.app.Activity;
import android.os.Bundle;
import com.facebook.react.ReactActivityDelegate;
import com.facebook.react.ReactRootView;

public class MainActivity extends ReactActivity {

  /**
   * Returns the name of the main component registered from JavaScript. This is used to schedule
   * rendering of the component.
   */
  @Override
  protected String getMainComponentName() {
    return "_";
  }

  /**
   * Returns the instance of the {@link ReactActivityDelegate}. There the RootView is created and
   * you can specify the renderer you wish to use - the new renderer (Fabric) or the old renderer
   * (Paper).
   */
  @Override
  protected ReactActivityDelegate createReactActivityDelegate() {
    return new MainActivityDelegate(this, getMainComponentName());
  }

  public static class MainActivityDelegate extends ReactActivityDelegate {
    public MainActivityDelegate(ReactActivity activity, String mainComponentName) {
      super(activity, mainComponentName);
    }

    @Override
    protected ReactRootView createRootView() {
      ReactRootView reactRootView = new ReactRootView(getContext());
      // If you opted-in for the New Architecture, we enable the Fabric Renderer.
      reactRootView.setIsFabric(BuildConfig.IS_NEW_ARCHITECTURE_ENABLED);
      return reactRootView;
    }

    @Override
    protected boolean isConcurrentRootEnabled() {
      // If you opted-in for the New Architecture, we enable Concurrent Root (i.e. React 18).
      // More on this on https://reactjs.org/blog/2022/03/29/react-v18.html
      return BuildConfig.IS_NEW_ARCHITECTURE_ENABLED;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        SplashScreen.show((Activity) getContext());
        super.onCreate(savedInstanceState);
    }
  }
}

clucasalcantara avatar Jan 20 '23 04:01 clucasalcantara

+1 same here

Bayramito avatar Oct 20 '23 15:10 Bayramito