android - Having trouble using load() and save() methods stored outside of main.java -
Here's what IO.java file looks like, in which the square is "io", which loads my methods ( ) And save ().
package com.myschedules; // FILE I / O import java.io. *; Import android.content.Context; Public class IO {reference asdf; // save file public void save (string filename, string data) {try {fileOutputStream fos = asdf.openFileOutput (filename, reference. MODE_PRIVATE); Fos.write (data.getBytes ()); Fos.close (); } Hold (Exception precedent) {}} // Load file Public string load (string filename) {try {fileInputStream fis = asdf.openFileInput (filename); BufferedReader Reader = new BufferedReader (new InputStreamReader (fis)); String line = zero, input = ""; While ((line = reader.readline ())! = Null input + = line; reader.close (); fis.close (); return input;} hold (ex before) {return "";}} }
When I had to call these methods in the main, it looks like:
io login_io = new when (); String login_preferences = login_io.load ("login_preferences.dat"); Unfortunately, this causes my app to crash on startup how can I store my load () And () saving the functions to a different file Repeat for my own organization is not very important for my main dislocation in the group. It is very easy with #included or imported calls in other languages, but I am lost here.
Edit :
Okay, now I have got another issue ... It is from within MainActivity.java, I want to file some I / O after the button is clicked. However, this "Iio Login_io = new io (this); "generates the following error:"
" Constructor IO (New View. Exclaimer () {}) is undefined "
cmd_login.setOnClickListener (New OnClickListener) {public void onClick (see v) {io login_io = new io (this) ; String login_preferences = login_io.load ("login_preferences.dat");
The accident probably referred to a NullPointerException on the reference variable Asdf . You need to set in the asdf constructor. Add constructor like this : // consortor public IO (last reference reference) {asdf = context;} Save public zeros. .. // Do not change .. public string load ... // does not change. You must also add this 'variable' variable to your immediate activity like this in the io class in your main activity class: Change : <> Deio> io login_io = new io (); // Bad: ( To do this : io login_io = new io (this); // good :) PS I think below your comments that the main activity. Java is an Android activity class (i.e. " class increases mainActivity activity ") so that ' It's the right kind of thing. It's good news.
Comments
Post a Comment