小编itf*_*che的帖子

如何在Android上的C中加载我自己的Java类?

我试图调用我使用Android NDK从C编写的一些Java代码.该应用程序是NativeActivity应用程序.我必须访问一些仅在Java中可用的功能,并且该功能要求您子类化另一个类,所以我不能直接从C执行调用.因此,我有这样的Java代码:

// src/com/example/my/package/SubClass.java
package com.example.my.package;

import android.foo.TheSuperClass;

public class SubClass extends TheSuperClass {
  public SubClass() {
    setImportantProperty(true);
  }
}
Run Code Online (Sandbox Code Playgroud)

我也有这样的C代码:

// Some file.c
void setThatImportantJavaProperty() {
  JavaVM *vm = AndroidGetJavaVM(); // This returns a valid JavaVM object
  JNIEnv* env;
  (*vm)->AttachCurrentThread(vm, &env, 0);

  jclass theSubClass = (*env)->FindClass(env, "com/example/my/package/SubClass");
  jthrowable exception = (*env)->ExceptionOccurred(env);
  if (exception) {
    (*env)->ExceptionDescribe(env);
    // This gives me: "java.lang.NoClassDefFoundError: [generic]".
    // Also, theSubClass is null, so the next line causes a segfault.
  }
  jmethodID theSubClassConstructor = (*env)->GetMethodID(env, theSubClass, …
Run Code Online (Sandbox Code Playgroud)

c java java-native-interface android android-ndk

10
推荐指数
2
解决办法
1万
查看次数

'String'不能转换为'DictionaryIndex <String,[(String)]?>'编译错误,具有非常基本的字典用法

我正在尝试创建一个非常简单的字典,将字符串映射到Swift中的字符串数组.代码如下所示:

class FirstViewController: UIViewController {

    var characters:[String] = []
    var adjacency = [String : [String]?]()

    override func viewDidLoad() {
        super.viewDidLoad()

        characters = loadCharacters()
        adjacency = loadAdjacency()

        var character:String = characters[0]
        var adj:[String] = adjacency[character] // This line gives the first compiler error
        adj = adjacency["a"] // This line gives the second compiler error

        println(adj)
    }

    func loadCharacters() -> [String] {
        return ["a", "b", "c"]
    }

    func loadAdjacency() -> [String : [String]?] {
        return ["a": ["a", "b", "c"], "b": ["b", "c", …
Run Code Online (Sandbox Code Playgroud)

swift

3
推荐指数
1
解决办法
753
查看次数

标签 统计

android ×1

android-ndk ×1

c ×1

java ×1

java-native-interface ×1

swift ×1