错误:MainActivity.java 上需要类、接口或枚举

Dan*_*ona 1 java android android-studio reactjs

我正在尝试编译一个在 Android Studio 上使用 Expo 的应用程序,但是,每次尝试时都会收到以下错误消息:

\android\app\src\main\java\com\profileid\MainActivity.java:2: error: class, interface, or enum expected
package com.projectname;
Run Code Online (Sandbox Code Playgroud)

我刚刚升级到 Expo SDK 43 并从react-native-unimodules.

我的 MainActivity.java 如下所示:

import expo.modules.ReactActivityDelegateWrapper;
package com.projectname;

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);
            }
        });
    }
}

Run Code Online (Sandbox Code Playgroud)

Ben*_* P. 7

你的第一个import陈述和package陈述不符合顺序。该package语句必须是文件的第一行:

package com.projectname;

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.ReactActivityDelegateWrapper;
import expo.modules.splashscreen.singletons.SplashScreen;
import expo.modules.splashscreen.SplashScreenImageResizeMode;
Run Code Online (Sandbox Code Playgroud)