本帖最后由 aree 于 2013-01-25 18:10:40 编辑

解决方案 »

  1.   

    JNIEXPORT jstring JNICALL
    Java_Prompt_getLine(JNIEnv *env, jobject obj, jstring prompt)
    {
    char buf[128];
    const jbyte *str;
    str = (*env)->GetStringUTFChars(env, prompt, NULL);
    if (str == NULL) {
    return NULL; /* OutOfMemoryError already thrown */
    }
    printf("%s", str);
    (*env)->ReleaseStringUTFChars(env, prompt, str);
    /* We assume here that the user does not type more than
    * 127 characters */
    scanf("%127s", buf);
    return (*env)->NewStringUTF(env, buf);
    }class Prompt {
    // native method that prints a prompt and reads a line
    private native String getLine(String prompt);
    public static void main(String args[]) {
    Prompt p = new Prompt();
    String input = p.getLine("Type a line: ");
    System.out.println("User typed: " + input);
    }
    static {
    System.loadLibrary("Prompt");
    }
    }
      

  2.   

    static JNINativeMethod gMethods[] = 
    {"JniHello","(Ljava/lang/String)Ljava/lang/String;",(void*)Hello_Native}, 这样改试一试
      

  3.   

    以NDK自带hellojni为例
    public class HelloJni extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); /*
     * Create a TextView and set its content. the text is retrieved by
     * calling a native function.
     */
    TextView tv = new TextView(this);
    tv.setText(stringFromJNI("jialiry"));
    setContentView(tv);
    } /*
     * A native method that is implemented by the 'hello-jni' native library,
     * which is packaged with this application.
     */
    public native String stringFromJNI(String str); /*
     * This is another native method declaration that is *not* implemented by
     * 'hello-jni'. This is simply to show that you can declare as many native
     * methods in your Java code as you want, their implementation is searched
     * in the currently loaded native libraries only the first time you call
     * them.
     * 
     * Trying to call this function will result in a
     * java.lang.UnsatisfiedLinkError exception !
     */
    public native String unimplementedStringFromJNI(); /*
     * this is used to load the 'hello-jni' library on application startup. The
     * library has already been unpacked into
     * /data/data/com.example.hellojni/lib/libhello-jni.so at installation time
     * by the package manager.
     */
    static {
    System.loadLibrary("hello-jni");
    }
    }jstring
    Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env,
    jobject thiz, jstring jstr)
    {
    char buf[128];
    const char* string=(char*)(*env)->GetStringUTFChars(env,jstr,NULL);
    strcpy(buf, string);
    strcat(buf, "同志,hello from jni");
    (*env)->ReleaseStringUTFChars(env,jstr,(jbyte*)string);
    return (*env)->NewStringUTF(env, buf);
    }
    结果:
    jialiry同志,hello from jni
      

  4.   

    看一下以下例如,希望对你有所帮助:private native void _setDataSource(
            String path, String[] keys, String[] values)
            throws IOException, IllegalArgumentException, SecurityException, IllegalStateException;{
            "_setDataSource",
            "(Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/String;)V",
            (void *)android_media_MediaPlayer_setDataSourceAndHeaders
    }
      

  5.   

    传一个字符串,到native中自己拆成字符串即可,比如strtok函数