簡體   English   中英

Android SDK中的文本資源

[英]Text Resources in Android sdk

我有關於android的課程書。 現在,當我有很多文字時,我便會看到幫助屏幕。 在書中輸入有關文字的信息。 android sdk可以使用包含許多文本的txt文件。 並用書本輸入的代碼,但是當我使用他時,它確實起作用(我的應用啟動,但屏幕上沒有任何文本),這是怎么回事? 幫助正確的變體。 我的代碼:

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM