简体   繁体   English

将原始.txt文件添加到布局

[英]Adding a raw .txt file to a layout

I'm trying to add a text file for a help page on my app but with the code I have it's just crashing the app when I select help from the options menu. 我正在尝试在应用程序上为帮助页面添加文本文件,但是使用代码,当我从选项菜单中选择帮助时,它只是使应用程序崩溃。 I know it's to do with this code as when I comment it out it opens the help page fine. 我知道这与这段代码有关,因为当我注释掉它时,它可以很好地打开帮助页面。

The code is in my HelpActivity class: 该代码在我的HelpActivity类中:

public class HelpActivity extends Activity {



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.help_page);
}

InputStream iFile = getResources().openRawResource(R.raw.gamehelp);

private String inputStreamToString(InputStream iFile) {
    TextView helpText = (TextView) findViewById(R.id.tvHelpText);
    String strFile = inputStreamToString(iFile);
    helpText.setText(strFile);
    return strFile;
    }

}

Can anyone see any problem with how I'm trying to do this? 谁能看到我尝试执行此操作时遇到的任何问题?

Thanks 谢谢

Since you didn't include any error logs, I'm judging just from your code: 由于您未包含任何错误日志,因此仅从您的代码中判断:

InputStream iFile = getResources().openRawResource(R.raw.gamehelp);

You are calling this in class body, but getResources() is not available here. 您在类主体中调用此方法,但是getResources()在此处不可用。 What you should do is: 您应该做的是:

InputStream iFile;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.help_page);
    iFile = getResources().openRawResource(R.raw.gamehelp);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM