2011-12-21 35 views
0

我有關於android的教訓的書。當我有很多文本時,現在我意識到幫助屏幕。在關於文本的書籍類型中。 android sdk可以使用帶有許多文本的txt文件。並在書類型的代碼,但即時通訊使用他,它didesent工作(我的應用程序啓動,但我沒有任何文字在屏幕上)有什麼不對?幫助正確的變體。我的代碼:Android中的文本資源sdk

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 

    } 
} 

回答

1

嗯...一個,你是從inputStreamToString(...)返回null。您需要正確實施它,然後才能指望它返回任何內容。

+0

的評論,「TODO」意味着這是一段代碼,您將需要編寫。這是「要做」的縮寫,告訴您計算機無法爲您編寫該代碼。 – BRFennPocock 2011-12-21 16:30:40

0

變化

private String inputStreamToString(InputStream iFile) { 
    // TODO Auto-generated method stub 
    return null; 
} 

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; 
} 
+0

它沒有工作:| – user1108339 2011-12-21 16:35:49

+0

@ user1108339這是因爲你的'openRawResource()'函數有類似的問題。它什麼也沒做,我認爲這對下一行產生了一個問題,'InputStream iFile = getResources()。「openRawResource(R.raw.lineagehelp);' – Sheriff 2011-12-21 18:07:57

+0

和我需要做什麼? – user1108339 2011-12-21 18:46:04