Android get resource / Buffer reader problems

2019-09-05 18:37发布

问题:

My code seems to run but it does not display the result any result on text view am guessing is because of the way I set my code. The code is below Somebody please help me. Thanks

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




        }

If you check the resource line am very sure am not getting that right and also the formating of the code I believe they just scattered . Hint the file.txt on the res/raw path has more than 100,000 strings/words, could this be the cause.Thanks Again

回答1:

Regarding your code:

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

                }// button
            });// button end

Is CCActivity3 derived from an Activity? If so, then you shouldn't construct it this way. You can't instantiate your own Activity class. Only the Android framework can do that.
What seems to be happening here is that inside fileReader(), you are calling getResources() on an Activity whose Context has not yet been properly initialized by onCreate().

In any case, one thing you can do is call fileReader() method directly without instantiating CCActivity3, like so:

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


回答2:

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

Above codes can get the right return?