1

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();


            }
        }
    );
Emzor
  • 1,380
  • 17
  • 28

3 Answers3

2

From the docs:

duration int: How long to display the message. Either LENGTH_SHORT or LENGTH_LONG

So, either Toast.LENGTH_SHORT or Toast.LENGTH_LONG. Not '3'

archived
  • 647
  • 8
  • 24
  • 1
    You can set it with a numerical value, but short is 0 and long is 1, Its better to use the built in constants though. – Adam Forbis Feb 25 '16 at 01:50
0
Toast.makeText(this,"Log added"+password,Toast.LENGTH_SHORT).show();
Kiplening
  • 1
  • 2
0

Use the proper context MainActivity.this NOT this.

Toast.makeText(MainActivity.this, "Log added", Toast.LENGTH_SHORT).show();
Emzor
  • 1,380
  • 17
  • 28
  • I have changed it to MainActivity.this and it is now saying "Incompatible Types". Required: 'android.widget.Toast' Found: 'void'. – Tom Dunwoody Feb 25 '16 at 19:59