0

I am making an app that stores username and password and automatically login into the wifi. I am storing these data in internal storage. So, to test that whether data has been permanently written, I made a method which will show username and password in toast message. If I am in the app and after saving data, I click on show button, it perfectly shows data. But when I exit app and after reopening the I click on show it force closes.

public class MyActivity extends Activity {

public Button save;
public Button show;
public EditText user;
public EditText pass;
String PRONTO = "PrivateData";
String un;
String pw;

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

    user = (EditText) findViewById(R.id.username);
    pass = (EditText) findViewById(R.id.password);
    save = (Button) findViewById(R.id.button);
    show = (Button) findViewById(R.id.test);

    save.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            un = (String) user.getText().toString();
            pw = (String) pass.getText().toString();
            //Creating file for saving username and passwords
            FileOutputStream fos;
            try {
                fos = openFileOutput(PRONTO, Context.MODE_PRIVATE);
                fos.write(un.getBytes());
                fos.write(pw.getBytes());
                fos.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }



        }
    });
    show.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            //for testing of storage of data
            FileInputStream fis;

            try {
                fis = openFileInput(PRONTO);
                fis.read(un.getBytes());
                fis.read(pw.getBytes());
                fis.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            //displaying the stored data to user
            Toast.makeText(getApplicationContext(), "Username and Password is" + un + " " + pw, Toast.LENGTH_SHORT).show();

        }
    });
}

}

SomeDevloperName
  • 622
  • 1
  • 6
  • 10

1 Answers1

1

After Re opening of your App

String PRONTO ,String un ,String pw; are null

declare it outside of your Activity mothod like

String PRONTO="PrivateData";
kalyan pvs
  • 14,486
  • 4
  • 41
  • 59