0

我正在尝试使用此链接在我的 react-native 项目中添加 facebook sdk - https://developers.facebook.com/docs/react-native/getting-started-android

但是,rnpm 并没有按照链接中的说明自动添加 SDK。因此,我使用此链接手动添加了 react-native-fbsdk - https://github.com/facebook/react-native-fbsdk/issues/205

然后,我构建我的项目并生成发布和调试 apk。但是,现在 android 应用程序在启动时崩溃了。而且,没有办法找出发生这种情况的原因。

这是代码-

MainActivity.java

package com.netizen_hybrid;


import android.content.Intent;

import com.facebook.react.ReactActivity;
import com.facebook.react.ReactPackage;
import com.facebook.react.shell.MainReactPackage;

import com.facebook.FacebookSdk;
import com.facebook.CallbackManager;
import com.facebook.reactnative.androidsdk.FBSDKPackage;

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

public class MainActivity extends ReactActivity {
    CallbackManager mCallbackManager;
    /**
     * Returns the name of the main component registered from JavaScript.
     * This is used to schedule rendering of the component.
     */
    
    @Override
    protected String getMainComponentName() {
        return "Netizen_Hybrid";
    }

    /**
     * Returns whether dev mode should be enabled.
     * This enables e.g. the dev menu.
     */
    @Override
    protected boolean getUseDeveloperSupport() {
        return BuildConfig.DEBUG;
    }

    /**
     * A list of packages used by the app. If the app uses additional views
     * or modules besides the default ones, add more packages here.
     */
    @Override
    protected List<ReactPackage> getPackages() {
        mCallbackManager = new CallbackManager.Factory().create();
        ReactPackage packages[] = new ReactPackage[]{
           new MainReactPackage(),
           new FBSDKPackage(mCallbackManager),
        };
        return Arrays.<ReactPackage>asList(packages);
    }
    
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
       super.onActivityResult(requestCode, resultCode, data);
       mCallbackManager.onActivityResult(requestCode, resultCode, data);
    }
    
}

build.gradle -

dependencies {
compile fileTree(dir: "libs", include: ["*.jar"])
compile "com.android.support:appcompat-v7:23.0.1"
compile "com.facebook.react:react-native:+"  // From node_modules
compile 'com.facebook.android:facebook-android-sdk:4.+'
compile project(":react-native-fbsdk")

}

settings.gradle -

rootProject.name = 'Netizen_Hybrid'

include ':app'

include ':react-native-fbsdk'
project(':react-native-fbsdk').projectDir = new File(settingsDir, '../node_modules/react-native-fbsdk/Android')

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.netizen_hybrid"
    android:versionCode="1"
    android:versionName="1.0">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="22" />

    <application
      android:allowBackup="true"
      android:label="@string/app_name"
      android:icon="@mipmap/ic_launcher"
      android:theme="@style/AppTheme"
      android:name="com.netizen_hybrid.MyApplication"
      >
      
      <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
      </activity>
      <activity android:name="com.facebook.FacebookActivity"
          android:configChanges=
                 "keyboard|keyboardHidden|screenLayout|screenSize|orientation"
          android:theme="@android:style/Theme.Translucent.NoTitleBar"
          android:label="@string/app_name" />
      <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>
      <provider android:authorities="com.facebook.app.FacebookContentProvider142933766117579"
          android:name="com.facebook.FacebookContentProvider"
          android:exported="true" />
    </application>

</manifest>

MyApplication.java -

import android.app.Application;
import com.facebook.FacebookSdk;
import com.facebook.appevents.AppEventsLogger;

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        // Initialize the SDK before executing any other operations,
        FacebookSdk.sdkInitialize(getApplicationContext());
        AppEventsLogger.activateApp(this);
    }
}

谁能告诉我哪里做错了?提前致谢。

4

2 回答 2

0

可能是因为你没有指定你的包裹MyApplication.java吗?尝试以与 中相同的方式设置它MainActivity.java

我的应用程序.java:

package com.netizen_hybrid;

... 

我犯了同样的错误。adb logcat我在尝试启动应用程序之前通过运行发现了问题。日志输出指向我的应用程序类。

于 2016-06-11T13:43:06.120 回答
0

配置对我来说看起来是正确的。但我可能会忽略某事。您可以尝试为 HelloFacebook 示例应用程序https://github.com/facebook/react-native-fbsdk/tree/master/Sample生成 apk吗?这可能有助于检查缺少的内容。此外,您还可以将我指向您在 GH 上的项目(仅包含必要的代码),以便我可以帮助调试。

于 2016-06-09T18:40:52.833 回答