0
I’m new to programming.
I saved an array of strings through FileOutputStream.write()
in my MainActivity
.
I can read the array perfectly through the FileInputStream
in Activity itself.
To read this array in another Activity I used the following code:
// READ
FileInputStream fin;
fin = openFileInput("my_file");
ObjectInputStream ois = new ObjectInputStream(fin);
String[] temp = (String[]) ois.readObject();
ois.close();
String texto = temp[5].replace("-", " ");
TextView tv = (TextView) findViewById(R.id.a2);
tv.setText(texto);
The Eclipse pointed out the following error:
The method openFileInput(String) is Undefined for the type Adapterpages
Then I made the following modification in the code (maybe there is the error):
MainActivity main = new MainActivity();
Context contexto = main.getApplicationContext();
FileInputStream fin;
fin = contexto.openFileInput("my_file");
It works "without errors", but the text of TextView tv
doesn’t change.
UPDATE 1
Save the array:
// WRITE NEW FILE
FileOutputStream fout = openFileOutput("my_file",
Context.MODE_APPEND);
ObjectOutputStream oos = new ObjectOutputStream(fout);
oos.writeObject(mVal);
oos.close();
UPDATE 2
Call the other Activity:
// RU call button
public void btTela1(View v) {
startActivity(new Intent(this, Pager.class).putExtra("dados", texto));
}
how are you doing to use the
FileOutputStream
? You could post this snippet of code?– mateusalxd
posted... but I believe that the problem is not in it because as I said in Mainactivity I read the data, but in the other
– Felipe
Felipe, my suggestion would be to close Fileoutputstream/Fileinputstream as well. Looking at the documentation, these functions give read/write access to the application, so all Activities should fill the file... Came to debug the return reading?
– Wakim
The input and output are close according to the code I posted... I checked in the emulator right, I didn’t even know what the debug is for, I just researched it seems that it also points out the variables and values of it, isn’t that it ? I’ll give a studied debug until tomorrow and put the result of it here... But I’ll leave this question open, can be ? thanks
– Felipe
@Wakim I don’t think I know how to debug what you asked... debug shows variable values, as String is not variable, I cannot see the value of the text I get. What should I do?
– Felipe
With breakpoint da to see the value of the array, just change the perspective to debug. From what I read there’s no need to close all the streams, only the last one that was created in cascade. Another suggestion would be to delete the file and create from scratch whenever you write an array. Because you are opening the file in mode
APPEND
, that is, always writing at the end and the first array written may be empty or old value. This will depend on your logic.– Wakim
Another thing that came to mind, is using this file just to pass data from a
Activity
to another?– Wakim
@Wakim in
MainActivity
I can see the content of array with the debug, already in the other Activity no. I closed the stream only at the end as you spoke. Before you start writing the array I always delete the file because it isAPPEND
. And yes, I just want to pass data on Activity to another.– Felipe
I’ll do a test here, but since deletes the file I see no error in the code. But if it is only to pass data from one Activity to another, it is recommended to use as extra Intent than file. Here at the SO pt have some questions about it, it’s quite simple.
– Wakim
@Wakim yes, it would be much simpler, but I’m using a Viewpager, then I can’t imagine how I use it Intent in that case
– Felipe
How are you starting the other
Activity
?– Wakim
@Wakim added the code
– Felipe
Felipe, you are already passing extras, just add the array as well.
– Wakim
@Wakim I think I understand, but the problem is time to receive. I call the class Pager.class, but the layouts are not inflated on her, she just does the
setAdapter(adapter)
. That is, very complicated (for me). I am trying here in various ways, when I can put the answer. Thank you very much for the help.– Felipe