Android 字符串上的contains()和substring()导致NullPointerException
我正试图读取一个文本文件。我想每行扫描一行文本文件。之后,将检查每一行是否包含EditText中的字符串。我尝试过contains和substring,但都不起作用。该文本文件的大小约为17MBAndroid 字符串上的contains()和substring()导致NullPointerException,android,string,Android,String,我正试图读取一个文本文件。我想每行扫描一行文本文件。之后,将检查每一行是否包含EditText中的字符串。我尝试过contains和substring,但都不起作用。该文本文件的大小约为17MB public class MainActivity extends Activity { Button t; EditText tv; ArrayList<String> myStringArray1; ListView lv; ArrayAdapte
public class MainActivity extends Activity {
Button t;
EditText tv;
ArrayList<String> myStringArray1;
ListView lv;
ArrayAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (EditText) findViewById(R.id.editText1);
lv = (ListView) findViewById(R.id.listView1);
myStringArray1 = new ArrayList<String>();
adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1,
myStringArray1);
lv.setAdapter(adapter);
tv.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
myStringArray1.clear();
String a = search(tv.getText().toString());
// myStringArray1.add(tv.getText().toString());
adapter.notifyDataSetChanged();
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
});
tv.requestFocus();
}
String search(String text) {
String everything;
AssetManager am = this.getAssets();
InputStream is = null;
try {
is = am.open("test.txt");
} catch (IOException e) {
// TODO Auto-generated catch block
// tv.setText("eror");
e.printStackTrace();
}
BufferedReader br = new BufferedReader(new InputStreamReader(is));
try {
StringBuilder sb = new StringBuilder();
String line = null;
try {
line = br.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
while (line != null) {
// sb.append(line);
// sb.append(System.lineSeparator());
try {
line = br.readLine();
if (line.substring(0, text.length()).equals(text)) {
myStringArray1.add(line);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//everything = sb.toString();
} finally {
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return "";
}
这两种方法都会导致相同的异常。正如Mike和Peter告诉您的,您应该删除这两个readline行。您尝试子串的第一行始终是第二行,可能不是您所要查找的 您可以使用while line=br.readLine!=让你的生活更轻松 例如:
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = br.readLine()) != null)
// sb.append(line);
// sb.append(System.lineSeparator());
if (line.substring(0, text.length()).equals(text)) {
myStringArray1.add(line);
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
第二行的内容为line=br.readLine;应该在while循环中的if块之后。Mike M.是正确的,此时您正在读取第二行,该行很可能为null。在执行子字符串之前,您无法测试行是否为null。答案不是,但我认为您使用子字符串的逻辑不正确。目前,它与startsWith相同。如果这是您想要的,请改用startsWith。否则,只使用包含。
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = br.readLine()) != null)
// sb.append(line);
// sb.append(System.lineSeparator());
if (line.substring(0, text.length()).equals(text)) {
myStringArray1.add(line);
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}