Java 我的应用程序答案转到数组0并将所有内容组合在一起

Java 我的应用程序答案转到数组0并将所有内容组合在一起,java,android,Java,Android,我们已经尝试了您的解决方案,它工作得很好,但是所有的答案都指向数组0,并将所有内容组合在一起,这样您可以解决该问题吗 我用它来收集用户的数据 public class profile extends AppCompatActivity { EditText name1 , surname1 , idnumber1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedIn

我们已经尝试了您的解决方案,它工作得很好,但是所有的答案都指向数组0,并将所有内容组合在一起,这样您可以解决该问题吗

我用它来收集用户的数据

public class profile extends AppCompatActivity {

EditText name1 , surname1 , idnumber1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profile);

    name1 = (EditText)findViewById(R.id.name);
    surname1 = (EditText)findViewById(R.id.surname);
    idnumber1 = (EditText)findViewById(R.id.Idnumber);

}
public void save(View view){


    String name2 = name1.getText().toString();
    String surname2 = surname1.getText().toString();
    String idnumber2 = idnumber1.getText().toString();

    File  file = null;

    name2 = name2+"";
    surname2 = surname2+"";


    FileOutputStream fileOutputStream = null;


    try {
        file = getFilesDir();
        fileOutputStream = openFileOutput("Rank It Up.txt" , Context.MODE_PRIVATE);
        fileOutputStream.write(name2.getBytes());
        fileOutputStream.write(surname2.getBytes());
        fileOutputStream.write(idnumber2.getBytes());


    } catch (IOException e) {
        Log.d("Rank It Up", e.toString());
    }
   finally {
        try {
            fileOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    Toast.makeText(this, "Save successfully"+file+"/Rank It Up.txt", Toast.LENGTH_LONG).show();

}

public void next(View view){

    Toast.makeText(this, "Database page",Toast.LENGTH_LONG).show();
    Intent intent = new Intent(this, DBActivity.class);
    startActivity(intent);



}
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[] data = m.split(" ");
         String name2 = (data.length > 0) ? data[0] : "";
     String surname2 = (data.length > 1) ? data[1] : "";
     String idnumber2 = (data.length > 2) ? 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);

}
}

然后显示从用户收集的数据

public class profile extends AppCompatActivity {

EditText name1 , surname1 , idnumber1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profile);

    name1 = (EditText)findViewById(R.id.name);
    surname1 = (EditText)findViewById(R.id.surname);
    idnumber1 = (EditText)findViewById(R.id.Idnumber);

}
public void save(View view){


    String name2 = name1.getText().toString();
    String surname2 = surname1.getText().toString();
    String idnumber2 = idnumber1.getText().toString();

    File  file = null;

    name2 = name2+"";
    surname2 = surname2+"";


    FileOutputStream fileOutputStream = null;


    try {
        file = getFilesDir();
        fileOutputStream = openFileOutput("Rank It Up.txt" , Context.MODE_PRIVATE);
        fileOutputStream.write(name2.getBytes());
        fileOutputStream.write(surname2.getBytes());
        fileOutputStream.write(idnumber2.getBytes());


    } catch (IOException e) {
        Log.d("Rank It Up", e.toString());
    }
   finally {
        try {
            fileOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    Toast.makeText(this, "Save successfully"+file+"/Rank It Up.txt", Toast.LENGTH_LONG).show();

}

public void next(View view){

    Toast.makeText(this, "Database page",Toast.LENGTH_LONG).show();
    Intent intent = new Intent(this, DBActivity.class);
    startActivity(intent);



}
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[] data = m.split(" ");
         String name2 = (data.length > 0) ? data[0] : "";
     String surname2 = (data.length > 1) ? data[1] : "";
     String idnumber2 = (data.length > 2) ? 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);

}
问题在于为什么所有数据都显示在一行上


显然,您的数据有问题。它可能不包含空格,所以当您将其与空格拆分时,只会得到1个值。选中此项,您将看到在剥离调用之后,数据数组的长度为1。但您无法发现此错误,因为您有以下代码:

String name2 = (data.length > 0) ? data[0] : "";
String surname2 = (data.length > 1) ? data[1] : "";
String idnumber2 = (data.length > 2) ? data[2] : "";
这段代码填补了这些空白点,虽然它可以防止应用程序崩溃,但也会导致您不了解问题所在