You need to enable JavaScript to run this app. Expo SDK41 Bare工作流(ejected) Android构建MainActivity.java报错如何解决?
优惠活动
大模型
产品
解决方案
定价
更多

Expo SDK41 Bare工作流(ejected) Android构建MainActivity.java报错如何解决?

问题根因

你的MainActivity继承自com.reactnativenavigation.NavigationActivity(Wix react-native-navigation库的父类),该父类本身没有createReactActivityDelegate、getMainComponentName这两个方法,所以触发Override注解不匹配、方法找不到的报错。

解决方案(二选一,根据你的导航方案选择)

方案1:实际使用React Navigation(JS端导航库,你package.json已安装相关依赖)

该场景下你误用了NavigationActivity作为父类,按以下步骤修改:

  1. 修改MainActivity的父类继承,替换为ReactActivity
  2. 补充getMainComponentName方法,返回你的RN项目主组件名
  3. 保留原有splash screen、配置变更的相关逻辑

修改后的完整MainActivity.java代码如下:

package com.satyamdorville.discover;
import android.content.res.Configuration;
import android.content.Intent;
import android.os.Bundle;
import com.facebook.react.ReactActivity;
import com.facebook.react.ReactActivityDelegate;
import com.facebook.react.ReactRootView;
import com.swmansion.gesturehandler.react.RNGestureHandlerEnabledRootView;
import expo.modules.splashscreen.singletons.SplashScreen;
import expo.modules.splashscreen.SplashScreenImageResizeMode;


public class MainActivity extends ReactActivity {

    @Override
    protected String getMainComponentName() {
        return "discover"; // 和index.js中AppRegistry.registerComponent的第一个参数保持一致
    }

    // Added automatically by Expo Config
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        Intent intent = new Intent("onConfigurationChanged");
        intent.putExtra("newConfig", newConfig);
        sendBroadcast(intent);
    }

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(null);
    // SplashScreen.show(...) has to be called after super.onCreate(...)
    // Below line is handled by '@expo/configure-splash-screen' command and it's discouraged to modify it manually
    SplashScreen.show(this, SplashScreenImageResizeMode.CONTAIN, ReactRootView.class, false);
  }

     @Override
     protected ReactActivityDelegate createReactActivityDelegate() {
         return new ReactActivityDelegate(this, getMainComponentName()) {
             @Override
             protected ReactRootView createRootView() {
                 return new RNGestureHandlerEnabledRootView(MainActivity.this);
             }
         };
     }
}

方案2:实际使用Wix react-native-navigation(原生导航库)

该场景下不需要createReactActivityDelegate相关逻辑,直接删除整个方法即可,无需修改父类继承。修改后你需要自行检查react-native-navigation的初始化逻辑是否符合官方要求,确保在MainApplication中完成了屏幕注册、库初始化操作。

验证步骤

修改完成后先在本地执行cd android && ./gradlew assembleDebug验证安卓本地编译是否正常,编译通过后再提交EAS Build即可。


内容的提问来源于stack exchange,提问作者Satyam Dorville

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.19 01:06:03