2016-08-23 62 views
0

我有一個簡單的問題,我確信有人理解一點點Java的基礎知識可以幫助我。重複類:MainActivity

我正在使用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 

回答

0

你有兩個類具有完全相同的類名MainActivity。如果您繼承了您聲明的兩個類中的兩個行爲,則可以將第一個類重命名爲類似BaseActivity的類,然後獲取您聲明的第二個MainActivity類以擴展BaseActivity

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"; 
    } 
} 
+0

這使得很多感覺,實際上我正在嘗試做什麼,但我得到了很多新的錯誤,我失去了,如果它仍然是我缺少的東西,或只是一切需要調整。將使用新日誌更新帖子 –

+0

要修復「無法找到符號」錯誤,您需要導入那些缺少的符號。 「有私人訪問」錯誤意味着你正試圖訪問具有「私人」訪問權限的變量(在超類中)。如果您需要後一個問題的幫助,您應該創建一個新的問題(修復符號錯誤之後),並清楚說明您正在嘗試執行的操作。 – cybersam

0

您不僅有2類具有相同名稱,但是,您在同一文件中有兩個公用類,它們不能用java編譯。