重复的类:MainActivity

时间:2016-08-23 18:09:06

标签: java android react-native react-native-android

我有一个简单的问题,我确信有人能够更好地了解Java的基础知识可以帮助我。

我正在使用React-Native并且一切都在为iOS工作,但在尝试为android编译时,我得到一个名为“duplicate class:com.myApp.MainActivity”的错误。

我知道它的MainActivity可能在以下代码中被定义了2次,但出于某种原因,我尝试了我所知道的一切,而且我无法将这两个MainActivity合并为一个。

有人能指出我正确的方向吗?谢谢!

    package com.myApp;

    import com.facebook.react.ReactActivity;
    import com.oblador.vectoricons.VectorIconsPackage;
    import com.tuanpm.RCTSmartconfig.RCTSmartconfigPackage;
    import com.pusherman.networkinfo.RNNetworkInfoPackage;
    import com.tuanpm.RCTMqtt.*;

    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
        protected String getMainComponentName() {
            return "myApp";
        }
    }

    public class MainActivity extends Activity implements DefaultHardwareBackBtnHandler {
        @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(),
                new VectorIconsPackage(),
                new RCTSmartconfigPackage(),
                new RCTMqttPackage())
                    .addPackage(new RNNetworkInfoPackage())      // <- ADD HERE
                    .setUseDeveloperSupport(BuildConfig.DEBUG)
                    .setInitialLifecycleState(LifecycleState.RESUMED)
                    .build();

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

            setContentView(mReactRootView);
        }
    }

更新1

在关注@cybersam解决方案后,我得到了这个:

:app:compileDebugJavaWithJavac
/Users/nicolasferro/Desktop/myApp/android/app/src/main/java/com/myApp/MainActivity.java:9: error: cannot find symbol
public class MainActivity extends ReactActivity implements DefaultHardwareBackBtnHandler {
                                                           ^
  symbol: class DefaultHardwareBackBtnHandler
/Users/nicolasferro/Desktop/myApp/android/app/src/main/java/com/myApp/MainActivity.java:12: error: cannot find symbol
    protected void onCreate(Bundle savedInstanceState) {
                            ^
  symbol:   class Bundle
  location: class MainActivity
/Users/nicolasferro/Desktop/myApp/android/app/src/main/java/com/myApp/MainActivity.java:14: error: mReactRootView has private access in ReactActivity
        mReactRootView = new ReactRootView(this);
        ^
/Users/nicolasferro/Desktop/myApp/android/app/src/main/java/com/myApp/MainActivity.java:14: error: cannot find symbol
        mReactRootView = new ReactRootView(this);
                             ^
  symbol:   class ReactRootView
  location: class MainActivity
/Users/nicolasferro/Desktop/myApp/android/app/src/main/java/com/myApp/MainActivity.java:16: error: mReactInstanceManager has private access in ReactActivity
        mReactInstanceManager = ReactInstanceManager.builder()
        ^
/Users/nicolasferro/Desktop/myApp/android/app/src/main/java/com/myApp/MainActivity.java:26: error: cannot find symbol
                .setInitialLifecycleState(LifecycleState.RESUMED)
                                          ^
  symbol:   variable LifecycleState
  location: class MainActivity
/Users/nicolasferro/Desktop/myApp/android/app/src/main/java/com/myApp/MainActivity.java:20: error: cannot find symbol
                .addPackage(new MainReactPackage(),
                                ^
  symbol:   class MainReactPackage
  location: class MainActivity
/Users/nicolasferro/Desktop/myApp/android/app/src/main/java/com/myApp/MainActivity.java:16: error: cannot find symbol
        mReactInstanceManager = ReactInstanceManager.builder()
                                ^
  symbol:   variable ReactInstanceManager
  location: class MainActivity
/Users/nicolasferro/Desktop/myApp/android/app/src/main/java/com/myApp/MainActivity.java:29: error: mReactInstanceManager has private access in ReactActivity
        mReactRootView.startReactApplication(mReactInstanceManager, "myApp", null);
                                             ^
/Users/nicolasferro/Desktop/myApp/android/app/src/main/java/com/myApp/MainActivity.java:29: error: mReactRootView has private access in ReactActivity
        mReactRootView.startReactApplication(mReactInstanceManager, "myApp", null);
        ^
/Users/nicolasferro/Desktop/myApp/android/app/src/main/java/com/myApp/MainActivity.java:31: error: mReactRootView has private access in ReactActivity
        setContentView(mReactRootView);
                       ^
11 errors
:app:compileDebugJavaWithJavac FAILED

3 个答案:

答案 0 :(得分:1)

由于ReactActivity已经扩展Activity,您可以将具有相同全名(也不允许)的2个公共类(不允许)合并为一个:

package com.myApp;

import com.facebook.react.ReactActivity;
import com.oblador.vectoricons.VectorIconsPackage;
import com.tuanpm.RCTSmartconfig.RCTSmartconfigPackage;
import com.pusherman.networkinfo.RNNetworkInfoPackage;
import com.tuanpm.RCTMqtt.*;

public class MainActivity extends ReactActivity implements DefaultHardwareBackBtnHandler {

    @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(),
            new VectorIconsPackage(),
            new RCTSmartconfigPackage(),
            new RCTMqttPackage())
                .addPackage(new RNNetworkInfoPackage())      // <- ADD HERE
                .setUseDeveloperSupport(BuildConfig.DEBUG)
                .setInitialLifecycleState(LifecycleState.RESUMED)
                .build();

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

        setContentView(mReactRootView);
    }

    /**
     * Returns the name of the main component registered from JavaScript.
     * This is used to schedule rendering of the component.
     */
    @Override
    protected String getMainComponentName() {
        return "myApp";
    }
}

答案 1 :(得分:0)

您有两个类完全相同的类名function runQueries(queryObservable) { return queryObservable .debounceTime(500) .distinctUntilChanged() .switchMap(search); } function search(query) { //Kicks off the first query return runQuery(query, 0) //Uses the results of the first query to see if more queries should be made .expand((result, idx) => //Continues to execute more queries until `hasMorePages` is false hasMorePages(result) ? runQuery(query, idx + 1) : Observable.empty()); } 。如果要从您声明的两个类继承这两个行为,则可以将第一个类重命名为MainActivity,然后获取您声明为扩展BaseActivity的第二个MainActivity类。

答案 2 :(得分:0)

不仅有2个具有相同名称的类,而且在同一个文件中有2个不能用java编译的公共类。