简体   繁体   中英

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:

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. 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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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