Java androidsdk中的文本资源
我有一本关于安卓的书。现在,当我有很多文本时,我意识到帮助屏幕。在书中键入关于文本的内容。android sdk可以使用包含许多文本的txt文件。在书中输入代码,但当我使用他时,它不起作用(我的应用程序启动了,但我在屏幕上没有任何文本),怎么了?帮助正确的变体。我的代码:Java androidsdk中的文本资源,java,android,Java,Android,我有一本关于安卓的书。现在,当我有很多文本时,我意识到帮助屏幕。在书中键入关于文本的内容。android sdk可以使用包含许多文本的txt文件。在书中输入代码,但当我使用他时,它不起作用(我的应用程序启动了,但我在屏幕上没有任何文本),怎么了?帮助正确的变体。我的代码: package com.lineage.goddess; import java.io.InputStream; import android.app.Activity; import android.os.Bundle;
package com.lineage.goddess;
import java.io.InputStream;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class LineageHelpActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.help);
openRawResource();
InputStream iFile = getResources().openRawResource(R.raw.lineagehelp);
TextView helpText =(TextView) findViewById(R.id.TextView_HelpText);
String strFile = inputStreamToString(iFile);
helpText.setText(strFile);
}
private String inputStreamToString(InputStream iFile) {
// TODO Auto-generated method stub
return null;
}
private void openRawResource() {
// TODO Auto-generated method stub
}
}
嗯。。。首先,您将从
inputStreamToString(…)
返回null
。您需要正确地实现它,然后才能期望它返回任何内容。更改
private String inputStreamToString(InputStream iFile) {
// TODO Auto-generated method stub
return null;
}
与
注释“TODO”表示这是您需要编写的代码部分。这是“to do”的缩写,表示计算机无法为您编写代码。@user1108339这是因为您的
openrawsource()
函数有类似的问题。它什么也不做,我假设这会给下一行带来问题,InputStream iFile=getResources().openRawResource(R.raw.relegeHelp)代码>尝试从代码中删除“private void openrawsource(){}”。它可能会覆盖活动方法
private String inputStreamToString(InputStream iFile)
{
Writer writer = new StringWriter();
if(iFile!=null)
{
char[] buffer = new char[1024];
try{
Reader reader = new BufferedReader(
new InputStreamReader(iFile, "UTF-8"));
int n;
while ((n = reader.read(buffer)) != -1) {
writer.write(buffer, 0, n);
}
} catch (UnsupportedEncodingException e) {
return e.toString();
} catch (IOException e) {
return e.toString();
}finally
{
try {
iFile.close();
} catch (IOException e) {
return e.toString();
}
}
}
String result = writer.toString();
return result;
}