有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

安卓错误:MainActivity上需要类、接口或枚举。JAVA

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

\安卓\app\src\main\java\com\profileid\MainActivity.java:2: error: class, interface, or enum expected
package com.projectname;

我刚刚升级到Expo SDK 43,并从^{迁移

我的主要活动。java看起来像这样:

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

import 安卓.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);
            }
        });
    }
}


共 (1) 个答案

  1. # 1 楼答案

    您的第一个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;