0

我有以下我知道在 Java 中工作的代码。我现在正在尝试在 Android 应用程序中做同样的事情。操作非常简单:我需要使用一个类中的方法(在同一个工作区中),我首先需要从我存储在/res/raw中的文本文件中获取行。

基本上我需要从 Android Activity 类调用驻留在 Java 类中的这个方法。这怎么可能?

活动类片段(CaptureActivity.java):

setContentView(R.layout.orientation);
                        try {
                            SentenceGenerator sentenceGenerator = new SentenceGenerator();
                            ArrayList<String> firstState = stateGenerator.getState(resultOutput, result, result);
                            String viewPoint = firstState.get(0);
                            String object = firstState.get(1);
                            String subject = firstState.get(2);
                            Thing subjectInfo = new Thing();
                            if(resultOutput.equals("Sliema")){
                                Sliema sliema = new Sliema();
                                subjectInfo = sliema.getInfo(viewPoint, object, subject);
                            }
                            try {
                                String s = sentenceGenerator.generateSentence("start.txt", "middle.txt", "end.txt", resultOutput, viewPoint, object, subject, subjectInfo.type, subjectInfo.name, subjectInfo.properties, false);
                                TextView instructionTextView = (TextView) findViewById(R.id.instructionTextView);
                                instructionTextView.setText(s);

                            } catch (Exception e) {
                            }
                        } catch (ClassNotFoundException e) {
                        }

Java 类片段 (SentenceGenerator.java):

public static ArrayList<String> getLines(String filename) throws Exception {
    ArrayList<String> lines = new ArrayList<String>();
    FileInputStream fstream = new FileInputStream(filename);

    BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
    String strLine;
    while ((strLine = br.readLine()) != null) {
        lines.add(strLine);
    }
    in.close();
    return lines;
}

感谢您提前提供任何帮助!

4

1 回答 1

2

更改以下代码:

     BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) {
    lines.add(strLine);
}

 Scanner br = new Scanner(new InputStreamReader(in));
while (br.hasNext()) {
    String strLine = br.nextLine();
    lines.add(strLine);
}

那应该行得通。希望这可以帮助!

于 2013-04-30T23:39:26.423 回答