我如何在Ionic Projects中获得MainActivity中的应用程序

时间:2018-10-30 13:21:13

标签: java android ionic-framework capacitor

我正在尝试将Swrve插件集成到我的Android项目中。 以下是我们在MainActivity中获得的代码

public class MainActivity extends BridgeActivity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Initializes the Bridge
    this.init(savedInstanceState, new ArrayList<Class<? extends Plugin>>() {{
      // Additional plugins you've installed go here
      // Ex: add(TotallyAwesomePlugin.class);
      add(MyPlugin.class);
    }});

    try {
      SwrveConfig config = new SwrveConfig();
      // To use the EU stack, include this in your config.
      // config.setSelectedStack(SwrveStack.EU);
      SwrveSDK.createInstance(this, <app_id>, "<api_key>", config);
    } catch (IllegalArgumentException exp) {
      Log.e("SwrveDemo", "Could not initialize the Swrve SDK", exp);
    }
  }
}

运行代码时出现以下错误

error: incompatible types: MainActivity cannot be converted to Application

该网站上的参考代码如下

public class YourApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        try {
            SwrveConfig config = new SwrveConfig();
            // To use the EU stack, include this in your config.
            // config.setSelectedStack(SwrveStack.EU);
            SwrveSDK.createInstance(this, <app_id>, "<api_key>", config);
        } catch (IllegalArgumentException exp) {
            Log.e("SwrveDemo", "Could not initialize the Swrve SDK", exp);
        }
    }
}

在离子项目中,我具有MainActivity而不是

public class MainActivity extends BridgeActivity {

在文档中我有这个

public class YourApplication extends Application {

并在初始化行中此关键字试图引用Application而不是MainActivity

SwrveSDK.createInstance(this, <app_id>, "<api_key>", config);

我对本机代码一无所知,因此,如果有人可以指导我并向我解释解决办法,我将不胜感激。

1 个答案:

答案 0 :(得分:-1)

您需要为该代码创建一个Application类。

制作一个新的Java类并将其设置为扩展Application:

public class SomeApp extends Application {}

然后将其添加到application标签中的AndroidManifest中:

<application
    ...
    android:name=".SomeApp">

然后按照设置指南,将应该包含的内容放在刚刚创建的Application类中。