我们正在开发的应用程序存在无法在内部存储器中显示数据的问题。
实际上,我们认为该错误位于show和array方法内部,但无法确定错误的位置。如果您能帮助我们解决此问题,我们将不胜感激。
这是logcat。
和我们的代码
public class DBActivity extends AppCompatActivity {
TextView name1 , surname1 , idnumber1;
@Override
protected void onCreate(Bundle saveInstanceState){
super.onCreate(saveInstanceState);
setContentView(R.layout.activity_db);
name1 = (TextView)findViewById(R.id.name);
surname1 = (TextView)findViewById(R.id.surname);
idnumber1 = (TextView)findViewById(R.id.idnumber);
}
public void show(View view){
try {
FileInputStream fileInputStream = openFileInput("Rank It Up.txt");
int read = -1;
StringBuffer buffer = new StringBuffer();
while ((read=fileInputStream.read()) != -1)
{
buffer.append((char) read);
}
Log.d("Rank It Up",buffer.toString());
String m = buffer.toString();
String[] data = m.split(" ");
String name2 = data[0];
String surname2 = data[1];
String idnumber2 = data[2];
name1.setText(name2);
surname1.setText(surname2);
idnumber1.setText(idnumber2);
} catch (FileNotFoundException e) {
Toast.makeText(this, "File Not Found", Toast.LENGTH_LONG).show();
} catch (IOException e){
e.printStackTrace();
}
Toast.makeText(this, "Data found", Toast.LENGTH_LONG).show();
}
public void back(View view){
Toast.makeText(this, "Main page",Toast.LENGTH_LONG).show();
Intent intent = new Intent(this, profile.class);
startActivity(intent);
}
}
错误非常明显。您在方法中遇到ArrayIndexOutOfBounds
异常show
。
此代码块可能就在这里:
String[] data = m.split(" ");
String name2 = data[0];
String surname2 = data[1];
String idnumber2 = data[2];
代码在访问之前没有检查“数据”的长度。这是这样的:
String[] data = m.split(" ");
String name2 = (data.length > 0) ? data[0] : "";
String surname2 = (data.length > 1) ? data[1] : "";
String idnumber2 = (data.length > 2) ? data[2] : "";
因此,为什么没有得到您期望的格式的字符串,这是要调试的另一个问题。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句