2017-04-06 24 views
-1

我試圖在我的應用程序中添加一個幫助頁面的文本文件,但使用代碼時我只是在從選項菜單中選擇幫助時崩潰了應用程序。我知道這是對這個代碼的處理,因爲當我將它註釋掉時,它會打開幫助頁面。將原始.txt文件添加到佈局

的代碼是我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; 
    } 

} 

任何人都可以看到我是如何試圖做到這一點任何問題嗎?

感謝

回答

2

既然你不包含任何錯誤日誌,我只是從你的代碼來看:

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

您在類主體調用此,但getResources()不可用這裏。你應該做的是:

InputStream iFile; 

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

謝謝你。它已經停止了它的崩潰,但它實際上並沒有顯示文本文件 –

+0

@PhilAdams我不知道這件事,從代碼中不清楚。但我可以建議你嘗試將你的txt文件的內容放到string.xml資源文件中。你可能需要它首先逃脫它(谷歌「Java轉義字符串在線」),但我想這可能是一個解決方案。 – MatusMak

+0

謝謝,我會試試看 –