[英]how can i fix this this fatal exception in my android code?
我已经在MainActivity.java中使用textSplitter
和一些代码编写了一个类,但是却遇到了致命的运行时异常。
public class textSplitter {
public String myText(BufferedReader bufferedReader,StringBuilder stringBuilder,String line,String fin) throws IOException {
while ((line=bufferedReader.readLine()) != null) {
stringBuilder.append(line);
stringBuilder.append('\n');
}
fin = stringBuilder.toString();
return fin;
}
public String[] txtConverter(String[] text,String fin) {
text = fin.split("\\$");
return text;
}
}
主要活动:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
InputStream is = this.getResources().openRawResource(R.raw.text1);
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
StringBuilder stringBuilder = new StringBuilder();
String line = null;
String fin = null;
String[] text = null;
textSplitter myClass = new textSplitter();
try {
myClass.myText(br,stringBuilder,line,fin);
myClass.txtConverter(text,fin);
TextView myText = (TextView) findViewById(R.id.text);
myText.setText(text.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
}
你的问题在这里
myClass.myText(br,stringBuilder,line,fin);
因为您永远不会将fin
的值设置为结果。 尝试
fin = myClass.myText(br,stringBuilder,line,fin);
这样做时应避免NPE
text = fin.split("\\$");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.