NDK:将文本字段值传递给c ++字符串

时间:2017-04-09 17:21:00

标签: java android c++ android-ndk java-native-interface

我进入了android而不是应用程序本身。我试图用NDK重新创建我的学校作为Android应用程序 - > MyczkowskiAdam/LuckyNumbersCppCMD 从C ++打印输出到文本字段不是问题,但为了让我的程序工作,我必须传递两个字符串(FirstName& LastName),输入从textfield到C ++并存储它们,以便主程序可以解释它,那就是什么我真的很挣扎。

在我的C ++文件中,我有一个主程序进程,我必须传递给FrNm和LstNm:

int LuckyNumbersProcess(std::string FrNm, std::string LstNm) {
    //stuff
    //return LuckyNumber (int)
}

然后我有运行LuckyNumbersProcess的本机函数并将LuckyNumber写入文本字段:

extern "C"
JNIEXPORT jstring JNICALL 
Java_com_android_mycax_luckynumbers_MainActivity_Calculate (JNIEnv *env, 
jobject ) {
    LuckyNumbersProcess(FrNmFromJava, LstNmFromJava);
    std::string sLuckyNumber = NumberToString(LuckyNumber);
    return env->NewStringUTF(sLuckyNumber.c_str());
}

来自文本字段的输入显示存储在FrNmFromJava和LstNmFromJava中,但我该如何处理呢?

我尝试过这种方法:

extern "C"
JNIEXPORT void JNICALL 
Java_com_android_mycax_luckynumbers_MainActivity_GetFirstName (JNIEnv * env, 
jobject jobj) {

    const char *str;

    jclass myclass_class =(jclass) env->NewGlobalRef
            (env->FindClass ("GetFirstNameFromField"));

    // we need the MyClass constructor
    jmethodID constructorID = env->GetMethodID
            (myclass_class, "", "()V");

    // and the sayHello() method
    jmethodID methodID = env->GetMethodID
            (myclass_class, "iFirstName", "()Ljava/lang/String;");

    // instanciate a MyClass object
    jobject myclass_object =  env->NewObject
            (myclass_class, constructorID);

    // call the sayHello() method
    jstring s = (jstring)  env->CallObjectMethod
            (myclass_object, methodID);

    // convert the Java String to use it in C
    str = env->GetStringUTFChars(s, 0);
    //printf("%s" , str);
    FrNmFromJava = str;
    env->ReleaseStringUTFChars(s, str);

}

但它返回JNI NewGlobalRef调用挂起异常java.lang.ClassNotFoundException:未找到类" GetFirstNameFromField"在路上:

这是我的Java文件:

public class MainActivity extends AppCompatActivity {


// Used to load the 'native-lib' library on application startup.
static {
    System.loadLibrary("luckynumbers");
}

EditText mFirstName;
EditText mLastName;
Button mButton;
GetFirstNameFromField gfn = new GetFirstNameFromField();
GetLastNameFromField gln = new GetLastNameFromField();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mFirstName = (EditText)findViewById(R.id.firstName);
    mLastName = (EditText)findViewById(R.id.lastName);
    mButton = (Button)findViewById(R.id.button);
    mButton.setOnClickListener(
            new View.OnClickListener()
            {
                public void onClick(View view)
                {
                    gfn.iFirstName();
                    gln.iLastName();
                    GetFirstName();
                    GetLastName();
                    TextView tv = (TextView) findViewById(R.id.sample_text);
                    tv.setText(Calculate());
                }
            });
}

public class GetFirstNameFromField {
    public String iFirstName() {
        return mFirstName.getText().toString();
    }
}
public class GetLastNameFromField {
    public String iLastName() {
        return mLastName.getText().toString();
    }
}
/**
 * A native method that is implemented by the 'native-lib' native library,
 * which is packaged with this application.
 */
public native String Calculate();

public native String GetFirstName();

public native String GetLastName();
}

任何帮助将不胜感激:)

1 个答案:

答案 0 :(得分:1)

好的我明白了,现在一切正常。

在java中,我只将两个字符串传递给本机方法

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mFirstName = (EditText)findViewById(R.id.firstName);
        mLastName = (EditText)findViewById(R.id.lastName);
        mButton = (Button)findViewById(R.id.button);
        mButton.setOnClickListener(
                new View.OnClickListener()
                {
                    public void onClick(View view)
                    {
                        TextView tv = (TextView) findViewById(R.id.sample_text);
                    tv.setText(Calculate(mFirstName.getText().toString(),mLastName.getText().toString()));
                }
            });
    }

    public native String Calculate(String jFirstName, String jLastName);

在c ++中

extern "C"
JNIEXPORT jstring JNICALL 
Java_com_android_mycax_luckynumbers_MainActivity_Calculate (JNIEnv *env, jobject, jstring cFirstName, jstring cLastName) {
    const char *path = env->GetStringUTFChars(cFirstName, NULL);
    const char *path2 = env->GetStringUTFChars(cLastName, NULL);
    FrNmFromJava = ToString(path);
    LstNmFromJava = ToString(path2);
    LuckyNumbersProcess(FrNmFromJava, LstNmFromJava);
    std::string sLuckyNumber = ToString(LuckyNumber);
    ClearAll();
    return env->NewStringUTF(sLuckyNumber.c_str());
}