升级Expo SDK43后Android Studio编译MainActivity.java出现class, interface, or enum expected错误
错误原因
Java 语法强制要求 .java 类文件的 package 包声明必须放在文件的最顶部,仅可位于注释之后,所有 import 导入语句之前。该问题大概率是你升级Expo SDK 43新增导入语句时,误将import写在了package声明之前导致的:你当前的 MainActivity.java 文件第一行是 import 语句,第二行才是 package com.projectname; 声明,不符合语法规则,因此编译器直接抛出语法错误。
修复方案
调整文件内代码顺序,将 package com.projectname; 移到所有 import 语句的最前面即可,修正后的完整代码如下:
package com.projectname; import expo.modules.ReactActivityDelegateWrapper; 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 void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 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); } /** * Returns the name of the main component registered from JavaScript. * This is used to schedule rendering of the component. */ @Override protected String getMainComponentName() { return "main"; } @Override protected ReactActivityDelegate createReactActivityDelegate() { return new ReactActivityDelegateWrapper(this, new ReactActivityDelegate(this, getMainComponentName()) { @Override protected ReactRootView createRootView() { return new RNGestureHandlerEnabledRootView(MainActivity.this); } }); } }
修改完成后重新编译即可解决该报错。
内容的提问来源于stack exchange,提问作者Daniel Corona
相关产品推荐
相关产品推荐

