Android获取资源/缓冲区读取器问题

时间:2012-04-05 03:35:33

标签: java android resources inputstream bufferedreader

我的代码似乎运行但是它没有在文本视图上显示任何结果我猜测是因为我设置代码的方式。代码在下面有人请帮助我。谢谢

     public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main3);

                Button button = (Button) findViewById(R.id.but);

                input = (EditText) findViewById(R.id.editTextj);


                display = (TextView) findViewById(R.id.textView8);


                button.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        CCActivity3 fs = new CCActivity3();
                        fs.fileReader();

                    }// button
                });// button end
            }

            public void fileReader() {

                try {
                InputStream is=this.getResources().openRawResource(R.raw.file);

                BufferedReader bc = new BufferedReader(new InputStreamReader(is));

                    String cLine;
                    String inputText = "";

                    List<String> test2 = new ArrayList<String>();   

                    // read file line by line



                    while ((cLine = bc.readLine()) != null) {


                         inputText = inputText + cLine + "\n";

                    }



                    s = input.getText().toString();

                    test = CCActivity3.getPermutation(s);//Permutation method
                          test2.retainAll(test);//intersection
                    String fg = "";

                    for (String s2 : test2) {
                        fg += s2 + "\n";
                    }


                    display.setText(fg);




                     bc.close();

                } catch (Exception e) {// catch any errors if necessary

                    display.setText(e.getMessage());
                }




        }

如果你检查资源行我非常肯定没有做到这一点,而且代码的格式化我相信它们只是分散了。提示res / raw路径上的file.txt有超过100,000个字符串/单词,这可能是原因。再次感谢

2 个答案:

答案 0 :(得分:0)

关于您的代码:

            button.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    CCActivity3 fs = new CCActivity3();
                    fs.fileReader();

                }// button
            });// button end

CCActivity3是否来自某个活动? 如果是这样,那么你不应该这样构造它。 您无法实例化自己的Activity类。只有Android框架可以做到这一点 这里似乎发生的是在fileReader()中,你在一个Activity上调用getResources(),它的Context还没有被onCreate()正确初始化。

在任何情况下,您可以做的一件事是直接调用fileReader()方法而不实例化CCActivity3,如下所示:

    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            fileReader();
        }// button
    });// button end

答案 1 :(得分:0)

test = CCActivity3.getPermutation(s); //置换方法 test2.retainAll(测试); //交叉点

以上代码可以获得正确的回报吗?