Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我的申请没有';t显示内存中的数据_Java_Android - Fatal编程技术网

Java 我的申请没有';t显示内存中的数据

Java 我的申请没有';t显示内存中的数据,java,android,Java,Android,我们正在开发一个应用程序,该应用程序存在无法在内存中显示数据的问题 我们实际上认为错误在方法show和数组中,但无法确定它在哪里。如果您能帮助我们解决这个问题,我们将不胜感激 这是航海日志 还有我们的代码 public class DBActivity extends AppCompatActivity { TextView name1 , surname1 , idnumber1; @Override protected void onCreate(Bundle

我们正在开发一个应用程序,该应用程序存在无法在内存中显示数据的问题

我们实际上认为错误在方法show和数组中,但无法确定它在哪里。如果您能帮助我们解决这个问题,我们将不胜感激

这是航海日志

还有我们的代码

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);

    }
}

错误很明显。您将在
show
方法中获得一个
ArrayIndexOutOfBounds
异常

这段代码可能就在这里:

        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] : "";

至于为什么不能以您期望的格式获取字符串,这是一个需要调试的单独问题。

谁调用
show
方法?show方法由用户调用。此外,即使捕获了FileNotFoundException或IOException,您仍然会显示“Data Found”toast。谢谢。我们将尝试您的问题解决方案。我们已经尝试了您的解决方案,它工作得很好,但所有的答案都指向数组0,并将所有内容组合在一起,以便您能够解决该问题