2
我想通过Textview显示日志文件,Textview日志文件内容由jni调用。
但Textview什么也没有显示(黑屏空白),当给出“你好/ n有多低”时,Textview显示正确。
return(* env) - > NewStringUTF(env,“hello/n How low”);被显示。
return(* env) - > NewStringUTF(env,str);没有显示。Textview什么都不显示jni
--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),它的工作原理。
--just显示日志file--
#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的表现呢?
在此先感谢。
谢谢,也许会返回null。但我不知道如何和难以调试它... – tknv 2011-01-07 06:06:46