本文介绍了无法解析符号ReactApplication/ReactNativeHost的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可在iOS上完美运行的react native,但由于MainApplication和MainActivity中的导入问题,无法在Android Studio中进行编译.我遵循了React Native FBSDK准则.

I have a react native running perfectly on iOS but does not compile in Android studio due to import issues in MainApplication and MainActivity. I followed the React Native FBSDK guidelines.

在MainApplication中,我在导入语句中得到无法解析符号":

In MainApplication I get "Cannot resolve symbol" on import statements:

导入com.facebook.react.ReactApplication;导入com.facebook.react.ReactNativeHost;

import com.facebook.react.ReactApplication;import com.facebook.react.ReactNativeHost;

以及以下错误:

错误:(7,8)错误:MainActivity不是抽象的,并且没有覆盖ReactActivity中的抽象方法getPackages()错误:任务':app:compileDebugJavaWithJavac'的执行失败.

Error:(7, 8) error: MainActivity is not abstract and does not override abstract method getPackages() in ReactActivityError:Execution failed for task ':app:compileDebugJavaWithJavac'.

我的MainApplication如下:

My MainApplication looks like:

package com.ExampleApp;

import android.app.Application;

import com.facebook.react.ReactApplication;
import com.tradle.react.UdpSocketsModule;
import com.peel.react.TcpSocketsModule;
import com.peel.react.rnos.RNOSModule;
import com.masteratul.exceptionhandler.ReactNativeExceptionHandlerPackage;
import com.oblador.vectoricons.VectorIconsPackage;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.react.shell.MainReactPackage;
import com.facebook.soloader.SoLoader;
import com.facebook.CallbackManager;
import com.facebook.FacebookSdk;
import com.facebook.reactnative.androidsdk.FBSDKPackage;
import com.facebook.appevents.AppEventsLogger;

import java.util.Arrays;
import java.util.List;

public class MainApplication extends Application implements ReactApplication {

  private static CallbackManager mCallbackManager = CallbackManager.Factory.create();

  protected static CallbackManager getCallbackManager() {
    return mCallbackManager;
  }

  @Override
  public void onCreate() {
    super.onCreate();
    // Use AppEventsLogger to log custom events.
    AppEventsLogger.activateApp(this);
  }

  private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {

    @Override
    public boolean getUseDeveloperSupport() {
        return BuildConfig.DEBUG;
    }

    @Override
    protected List<ReactPackage> getPackages() {
      return Arrays.<ReactPackage>asList(
          new MainReactPackage(),
            new UdpSocketsModule(),
            new TcpSocketsModule(),
            new RNOSModule(),
            new ReactNativeExceptionHandlerPackage(),
            new VectorIconsPackage(),
            new FBSDKPackage(mCallbackManager)

      );
    }
  };

  @Override
  public ReactNativeHost getReactNativeHost() {
    return mReactNativeHost;
  }

}

还有我的MainActivity:

And my MainActivity:

import android.content.Intent;
import com.facebook.react.ReactActivity;


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
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        MainApplication.getCallbackManager().onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected String getMainComponentName() {
        return "ExampleApp";
    }
}

推荐答案

在将我的本机项目与Expo分离后,我也发生了这种情况.

It happened to me too after detach my react-native project from Expo.

在MainApplication中,这些导入出现错误无法解析符号":

In MainApplication I had "error Cannot resolve symbol" for these imports:

import com.facebook.react.ReactApplication;
import com.facebook.react.ReactNativeHost;

在我使用的package.json文件中:

in my package.json file I used:

"react-native": "https://github.com/expo/react-native/archive/sdk-29.0.0.tar.gz",

问题是android找不到像这样的node_modules的目录.

the problem was that the android can't find the directory for node_modules like that.

为了解决此问题,我必须:

in order to fix the issue I had to:

  1. 将其更改为:

  1. change it to :

"react-native":"^ 0.57.8",

"react-native": "^0.57.8",

删除node_modules文件

delete node_modules file

这就是为我解决问题的原因.希望我能帮上忙.

That's what solved the problem for me.Hope I helped.

这篇关于无法解析符号ReactApplication/ReactNativeHost的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 12:56