Reputation: 9
I have defined a simple Toast
that is going to display "log added" when a file has been written to. The rest of the code is just the rest of the method that writes EditText content to a file called log.txt.
I have used getActivityContext()
, this
and MainActivity
as the context
arguments in an attempt to get this to work. However for some reason it still doesn't. I have imported the correct classes, I have checked the manifest
, but MainActivity
is correctly declared. I have searched for the answer to this for quite sometime as I knew it would be regarded as a novice issue. But never the less, it would be nice to get an answer :)
//SEND ARRIVAL TIME
public void sendArrTime(View view) {
btnArr = (Button) findViewById(R.id.btnArr);
btnArr.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Opens log.txt
OutputStreamWriter out = new OutputStreamWriter(openFileOutput("arrlog.txt", MODE_APPEND));
//Writes contents to file
etArr = (EditText) findViewById(R.id.etArr);
String text = etArr.getText().toString();
out.write(text);
out.write('\n');
//close file
out.close();
//Confirmation Toast
Toast toast = Toast.makeText(this, "Log added", 3).show();
}
}
);
Upvotes: 1
Views: 579
Reputation: 1368
Use the proper context MainActivity.this
NOT this
.
Toast.makeText(MainActivity.this, "Log added", Toast.LENGTH_SHORT).show();
Upvotes: 0
Reputation: 1
Toast.makeText(this,"Log added"+password,Toast.LENGTH_SHORT).show();
Upvotes: 0