【发布时间】:2011-01-06 09:35:23
【问题描述】:
我想通过 Textview 显示日志文件,而 Textview 日志文件内容由 jni 调用。
但是Textview什么都没有显示(黑屏),当只给出“你好/n有多低”时,Textview显示正确。
return (*env)->NewStringUTF(env, "hello /n 有多低");
return (*env)->NewStringUTF(env, str);没有显示。
--application.java--
package com.showlog;
import android.app.Activity;
import android.widget.TextView;
import android.os.Bundle;
public class showlog extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText( stringFromJNI() );
setContentView(tv);
}
public native String stringFromJNI();
public native String unimplementedStringFromJNI();
static {
System.loadLibrary("showlog");
}
}
--showlog.c--
#include <string.h>
#include <stdio.h>
#include <jni.h>
#define MAX 119 // MAX of one line length of log file
#define Log_file "./Log_file.log" // log file path
jstring
Java_com_showlog_stringFromJNI(JNIEnv* env, jobject thiz)
{
char line[120]; // one line length of Log_file
char str[2000]; // Log_file length
FILE *fin;
fin=fopen(Log_file, "r");
while( ! feof(fin)){
fgets(line, MAX, fin);
strcat(str, line);
}
fclose(fin);
return (*env)->NewStringUTF(env, str);
}
然后我只尝试 c 代码(不是 jni lib),它可以工作。
--只显示日志文件--
#include <string.h>
#include <stdio.h>
#define MAX 119 // MAX of one line length of log file
#define Log_file "./Log_file.log" // log file path
main()
{
char line[120]; // one line length of Log_file
char str[2000]; // Log_file length
FILE *fin;
fin=fopen(Log_file, "r");
while( ! feof(fin)){
fgets(line, MAX, fin);
strcat(str, line);
}
fclose(fin);
printf("%s", str);
return 0;
}
Textview 是如何显示的?
提前致谢。
【问题讨论】:
标签: java android c java-native-interface android-ndk