Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MainActivityDelegate cannot be converted to Activity SplashScreen.show(this); #591

Closed
ireneeming opened this issue Oct 27, 2022 · 13 comments

Comments

@ireneeming
Copy link

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

  • Node : 16.17.0
  • Yarn : 1.22.19
  • react : 18.1.0
  • react-native : 0.70.0

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

  • "react-native-splash-screen": "^3.3.0",
    What platform does your issue occur on? (Android/iOS/Both)
  • just checked Android only.

Describe your issue as precisely as possible :

  1. Steps to reproduce the issue or to explain in which case you get the issue

  2. Interesting logs

error: incompatible types: MainActivityDelegate cannot be converted to Activity
      SplashScreen.show(this);

more details of error

BUILD FAILED in 12s

error Failed to install the app. Make sure you have the Android development environment set up: https://reactnative.dev/docs/environment-setup.
Error: Command failed: ./gradlew app:installDebug -PreactNativeDevServerPort=8081
/Users/minjiseo/buyaladdin/BRApp/BuyaladdinApp/android/app/src/main/java/com/aladdin/com/aladdin/MainActivity.java:59: error: incompatible types: MainActivityDelegate cannot be converted to Activity
      SplashScreen.show(this);
                        ^
Note: /Users/minjiseo/myFolder/MyAppName/AppName/android/app/src/debug/java/com/AppName/com/AppName/ReactNativeFlipper.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
1 error

Join a screenshot or video of the problem on the simulator or device?

Show us the code you are using?

followed the guide settings.gradle, build.gradle, App.tsx and set all done.

MainActivity.java

package com.aladdin.com.aladdin;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.facebook.react.ReactActivity;
import com.facebook.react.ReactActivityDelegate;
import com.facebook.react.ReactRootView;
import com.lockincomp.liappagent.LiappAgent;

import org.devio.rn.splashscreen.SplashScreen;
...
@Override
    protected void onCreate(Bundle savedInstanceState) {

      SplashScreen.show(this);
  
     ...my other codes

      super.onCreate(null);
// super.onCreate(savedInstanceState); <- this also tried but not working
    }

I don't know why I'm having this kind of error. because of React Native 0.7 version error? or what,,,,please help..

@Guilleanto
Copy link

I have the same issue i'm using RN 0.70, maybe the new arch in RN 0.68 >?

@CrisangerA
Copy link

I have the same mistake

  • react: 18.1.0
  • react-native: 0.70.4

@Guilleanto I think the new architecture is not enabled since the console does not see the message fabric:true. Only rootTag
image

@GauthamChinna
Copy link

I have the same issue i'm using RN 0.70.4, is there any solution available?

@codersandipdas
Copy link

Add: import android.os.Bundle; at the top of MainActivity.java

@bonusrk
Copy link

bonusrk commented Nov 6, 2022

Fresh RN application MainActivity.java looks like so now:

package com.myapp;

import android.os.Bundle;
import com.facebook.react.ReactActivity;
import org.devio.rn.splashscreen.SplashScreen;
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 "MyApp";
  }

  /**
   * 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(this);
      super.onCreate(null);
    }
  }
}

Still have the issue incompatible types: MainActivityDelegate cannot be converted to Activity SplashScreen.show(this);

method show goes from here

    /**
     * 打开启动屏
     */
    public static void show(final Activity activity) {
        show(activity, false);
    }

and it take Activity type, while this in MainActivity is MainActivityDelegate

If we move onCreate outside of MainActivityDelegate to the very top class application will be built successfully

@ireneeming
Copy link
Author

ireneeming commented Nov 7, 2022

@bonusrk I followed your guide, but mine doesn't work.

package com.myapp.com.myapp;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.facebook.react.ReactActivity;
import org.devio.rn.splashscreen.SplashScreen;
import com.facebook.react.ReactActivityDelegate;
import com.facebook.react.ReactRootView;
import com.lockincomp.liappagent.LiappAgent;

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 "MyApp";
  }

  /**
   * 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.    //<-------here is I added onCreate upper side but still doesn't work...is that right way I did?
  protected void onCreate(Bundle savedInstanceState) {
    SplashScreen.show(this);
    super.onCreate(null);
  }


  @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) {

      ...my other codes which supposed to in this area
      super.onCreate(null);
    }
  }
}

@bonusrk
Copy link

bonusrk commented Nov 7, 2022

@bonusrk I followed your guide, but mine doesn't work.

package com.myapp.com.myapp;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.facebook.react.ReactActivity;
import org.devio.rn.splashscreen.SplashScreen;
import com.facebook.react.ReactActivityDelegate;
import com.facebook.react.ReactRootView;
import com.lockincomp.liappagent.LiappAgent;

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 "MyApp";
  }

  /**
   * 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.    //<-------here is I added onCreate upper side but still doesn't work...is that right way I did?
  protected void onCreate(Bundle savedInstanceState) {
    SplashScreen.show(this);
    super.onCreate(null);
  }


  @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) {

      ...my other codes which supposed to in this area
      super.onCreate(null);
    }
  }
}

I think you should move onCreate totally. I mean delete it in nested class and keep only in top level.
I went event a little bit forward and kept only main class just to check and it works

package com.myapp;

import android.os.Bundle;
import com.facebook.react.ReactActivity;
import org.devio.rn.splashscreen.SplashScreen;
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 "MyApp";
  }

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

@ireneeming
Copy link
Author

@bonusrk

now it works!!!!! Tnx very much!

@JslinSir
Copy link

JslinSir commented Nov 19, 2022

image

Error still reported
package com.driverapp;
import android.os.Bundle;
import com.facebook.react.ReactActivity;
import org.devio.rn.splashscreen.SplashScreen;
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 "driverApp";
  }

  /**
   * 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(this);
      super.onCreate(null);
   }

  }
}

@shahanshah87
Copy link

shahanshah87 commented Nov 25, 2022

@bonusrk

now it works!!!!! Tnx very much!

can you please share your final MainActivity.java

I tried like this:-
Screenshot 2022-11-25 at 7 11 51 PM

Build is successful but app is crashing.

@pedro-ernesto
Copy link

pedro-ernesto commented Dec 13, 2022

@shahanshah87
can you please share your final MainActivity.java

Build is successful but app is crashing.

You should pass null to super.onCreate

image

@pedro-ernesto
Copy link

pedro-ernesto commented Dec 13, 2022

@JslinSir
Error still reported

onCreate should be outside MainActivityDelegate, check out my comment above

@dleavittpmc
Copy link

@bonusrk You're the man! That did it. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants