繁体   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