我是android新手。在我的项目中我有三个java类。 第三个java类是Notification.java(这不是主java),我在这里尝试 使用文本视图显示文本文件的内容,但不显示任何内容。但是当我把它作为Notificaion.java是主java文件的示例项目时,它能够显示文本文件内容。我知道我错过了一些小小的逻辑。
Notification.java
package com.example.alarmnotification;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Calendar;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import android.app.Activity;
import android.graphics.drawable.Drawable;
public class NotificationView extends Activity {
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
/*int[] quoteArray_june = {
R.raw.june,
R.raw.june
};*/
setContentView(R.layout.notification);
TextView QuoteTxt = (TextView)findViewById(R.id.quoteTxt);
QuoteTxt.setText(readTxt());
}
private String readTxt(){
InputStream inputStream = getResources().openRawResource(R.raw.june);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int i;
try {
i = inputStream.read();
while (i != -1) {
byteArrayOutputStream.write(i);
i = inputStream.read();
i--;
}
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return byteArrayOutputStream.toString();
}
}
和notification.xml是
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/prabhupada_Quote"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<TextView
android:id="@+id/quoteTxt"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/hello_world"
android:textSize="22px"
/>
</LinearLayout>
答案 0 :(得分:0)
您的图片视图可能隐藏了文字视图
如果资源文件包含&#34;可读文本&#34;使用以下方法读取字符串
public String readStringFromResource() {
StringBuilder contents = new StringBuilder();
String sep = System.getProperty("line.separator");
try {
InputStream is = getResources().openRawResource(R.raw.june);
BufferedReader input = new BufferedReader(
new InputStreamReader(is), 1024 * 8);
try {
String line = null;
while ((line = input.readLine()) != null) {
contents.append(line);
contents.append(sep);
}
} finally {
input.close();
}
} catch (FileNotFoundException ex) {
} catch (IOException ex) {
}
return contents.toString();
}