gpt4 book ai didi

java - 重复类 : MainActivity

转载 作者:行者123 更新时间:2023-12-01 09:34:35 27 4
gpt4 key购买 nike

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

我正在使用 React-Native,并且一切都适用于 iOS,但是当尝试编译 Android 时,我收到一个名为“duplicate class: com.myApp.MainActivity”的错误。

我知道它可能在下面的代码中定义了两次 MainActivity,但由于某种原因,我尝试了我所知道的一切,但我无法将这两个 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

最佳答案

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

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

关于java - 重复类 : MainActivity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39108019/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com