错误:不兼容的类型:MainActivity无法转换为FragmentActivity .addPackage(new ReactDatePackage(this))

时间:2015-11-26 07:40:46

标签: javascript java android react-native

我想将此模块https://github.com/nucleartux/react-native-date用于react-native。我试图安装这个模块,但是当我执行第4步(Register React Package)时,我遇到了构建错误。 错误讯息:

  

MainActivity.java:31:错误:不兼容的类型:MainActivity无法转换为FragmentActivity   .addPackage(new ReactDatePackage(this))                                                    ^   注意:某些消息已经简化;使用-Xdiags重新编译:详细以获得完整输出   1错误
  :app:compileDebugJavaWithJavac FAILED
  * 什么地方出了错:   任务':app:compileDebugJavaWithJavac'执行失败。   编译失败。

错误代码:

package com.myapp;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;

import com.facebook.react.LifecycleState;
import com.facebook.react.ReactInstanceManager;
import com.facebook.react.ReactRootView;
import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler;
import com.facebook.react.shell.MainReactPackage;
import com.facebook.soloader.SoLoader;

import me.nucleartux.date.ReactDatePackage;
import android.support.v4.app.FragmentActivity;

public class MainActivity extends Activity implements

DefaultHardwareBackBtnHandler {

private ReactInstanceManager mReactInstanceManager;
private ReactRootView mReactRootView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mReactRootView = new ReactRootView(this);

    mReactInstanceManager = ReactInstanceManager.builder()
            .setApplication(getApplication())
            .setBundleAssetName("index.android.bundle")
            .setJSMainModuleName("index.android")
            .addPackage(new MainReactPackage())
            .addPackage(new ReactDatePackage(this))
            .setUseDeveloperSupport(BuildConfig.DEBUG)
            .setInitialLifecycleState(LifecycleState.RESUMED)
            .build();

    mReactRootView.startReactApplication(mReactInstanceManager, "myapp", null);

    setContentView(mReactRootView);
}

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_MENU && mReactInstanceManager != null) {
        mReactInstanceManager.showDevOptionsDialog();
        return true;
    }
    return super.onKeyUp(keyCode, event);
}

@Override
public void onBackPressed() {
  if (mReactInstanceManager != null) {
    mReactInstanceManager.onBackPressed();
  } else {
    super.onBackPressed();
  }
}

@Override
public void invokeDefaultOnBackPressed() {
  super.onBackPressed();
}

@Override
protected void onPause() {
    super.onPause();

    if (mReactInstanceManager != null) {
        mReactInstanceManager.onPause();
    }
}

@Override
protected void onResume() {
    super.onResume();

    if (mReactInstanceManager != null) {
        mReactInstanceManager.onResume(this);
    }
}
}

你能帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

the doc at step 4中,您还必须MainActivity延长FragmentActivity

public class MainActivity extends FragmentActivity implements DefaultHardwareBackBtnHandler { // ! extends from FragmentActivity

(您的只展开Activity