gpt4 book ai didi

android - 从 .txt 文件加载数组? (安卓)

转载 作者:行者123 更新时间:2023-11-29 18:18:22 24 4
gpt4 key购买 nike

我已经四处搜索,但没能想出解决这个问题的办法。我的(第一个)问题是,我在 getLengthOfText(); 方法中的 FileInputStream instream = context.getApplicationContext().openFileInput(textname); 上遇到了 NPE。我已经调试并且正确的文件名似乎传递给了这个方法。我已经坚持了几个星期,真的希望它能工作(新手)。

这个方法正在被另一个类调用。此外,文件在数据/数据中,其他一切都应该如此。请帮忙!!-技巧

/***** this class takes a text file and loads it into an array **/

public class loadArray {

Context context;
textWriter textwriter;
public loadArray (Context cxt) {
this.context = cxt;
textwriter = new textWriter (cxt);
context = cxt;
}

FileInputStream instream;
textWriter tw = new textWriter(context);
String[] choiceAry, dummyAry;
P p = new P(); // shared prefs helper
String Choice;
String comp = "notnull";

int addChoiceCount, length;

// Context context = this.getApplicationContext();

public void buildDummies(int length) {
// addChoiceCount = p.gisI("addChoiceCount");
choiceAry = new String[length];
p.pisI("length", length, context);

// dummyAry = new String[100];

}

public Integer getLengthOfText(String textname)
throws FileNotFoundException {// counts how many lines of text
// DataInputStream dis = new DataInputStream(openFileInput("file.dat"));
int length = 0;
instream = context.getApplicationContext().openFileInput(textname);
InputStreamReader inputreader = new InputStreamReader(instream);
BufferedReader buffreader = new BufferedReader(inputreader);

try {
// while (!comp.equals(null)){
while (buffreader.ready()) {
String temp = buffreader.readLine();
length++;
}
buffreader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return length;
}

public String[] laft(String textname) throws FileNotFoundException {
// loads a text file to an array...load array from text........
length = getLengthOfText(textname);
buildDummies(length);
try {
instream = context.getApplicationContext().openFileInput(textname);
InputStreamReader inputreader = new InputStreamReader(instream);
BufferedReader buffreader = new BufferedReader(inputreader);
// load array from text
for (int i = 0; i < (length); i++) {
try {
Choice = buffreader.readLine();
choiceAry[i] = Choice;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
buffreader.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return choiceAry;

}

public String getOrderName() {
// TODO Auto-generated method stub
return null;
}

}

感谢来自#android-dev 的 Leeds 和 billiard,我找到了答案

我正在做的也是从另一个没有扩展 Activity 的类调用 loadArry.. 所以它是 Activity >calling> 不扩展 Activity 的类 >calling> 不扩展 Activity 的类。不知何故,上下文丢失了。我基本上在顶部应用了类似的行

Context context;
textWriter textwriter;
public loadArray (Context cxt) {
this.context = cxt;
textwriter = new textWriter (cxt);
context = cxt;

希望这能在将来为某人节省很多时间。

最佳答案

您在代码中把事情弄得太复杂了。最后,复杂的代码 = 更难调试。

我一直将文本文件加载到数组中。

考虑使用 java.util.StringTokenizer类或类 java.lang.String 中的 String split() 方法。

TenFour4 提出了另一个好点。

代码应该是这样的...

Context context;
textWriter textwriter;
public loadArray (Context cxt){
this.context = cxt;
textwriter = new textWriter (cxt);
}

--更新--

一个常见的错误是您尝试读取的文件没有正确关闭,因此每当您尝试访问仍然打开的文件时都会收到 NullPointerException

例如

PrintWriter pw = new PrintWriter (new FileWriter (fileName));
pw.println ("This is output data: " + data);
//new loadArray ().getLengthOfText (fileName); //ERROR Throws NPE
pw.close(); //Make Sure you Close Before trying to read the file again!
new loadArray ().getLengthOfText (fileName); //Works like a charm :)

关于android - 从 .txt 文件加载数组? (安卓),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6905214/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com