-2

the red line below the code appear after writing Toast.LENGHT_LONG

package com.example.neomn.myapplication1;

    import android.content.Context;
    import android.os.Bundle;
    import android.support.v7.app.ActionBarActivity;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.Button;
    import android.widget.Toast;

public class MainActivity extends ActionBarActivity {

        Button start = (Button) findViewById( R.id.btnstartservice );
        Button stop = (Button) findViewById( R.id.btnstopservice );


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

            start.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {



 Toast.makeText(this , "My First Service Started ",Toast.LENGTH_LONG).show();

                }
            });
Neo Mn
  • 150
  • 1
  • 12

4 Answers4

3

Because, Toast.makeText() first argument required Application Context and in your code, this reefers to Button's onClickListener class not a context of Activity.

change it with

Toast.makeText(MainActivity.this , "My First Service Started ",Toast.LENGTH_LONG).show();
user370305
  • 108,599
  • 23
  • 164
  • 151
1

Your Context, "this" is wrong.

Change:

 Toast.makeText(this, "My First Service Started ",Toast.LENGTH_LONG).show();

To:

 Toast.makeText(MainActivity.this, "My First Service Started ",Toast.LENGTH_LONG).show();

Here is your code:

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {


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

        Button start = (Button) findViewById(R.id.btnstartservice);
        Button stop = (Button) findViewById(R.id.btnstopservice);


        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Toast.makeText(MainActivity.this, "My First Service Started ", Toast.LENGTH_LONG).show();

            }
        });
    }
}

Read more about Context here: http://developer.android.com/reference/android/content/Context.html.

Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
  • Please explain properly in your answer about problems not only giving solutions work. Instead of posting whole unnecessary code, just explain why OP facing problem and whats the solution. – user370305 Mar 19 '15 at 14:08
1

Write following lines

Button start = (Button) findViewById( R.id.btnstartservice );
Button stop = (Button) findViewById( R.id.btnstopservice );

in your OnCreate method instead of writing them at class level because start and stop causes nullpointer as you define those outside of activity lifecycle methods.And also change

 Toast.makeText(this , "My First Service Started ",Toast.LENGTH_LONG).show();

to

 Toast.makeText(MainActivity.this , "My First Service Started ",Toast.LENGTH_LONG).show();

i.e. Rewrite your code as

   import android.content.Context;
   import android.os.Bundle;
   import android.support.v7.app.ActionBarActivity;
   import android.view.Menu;
   import android.view.MenuItem;
   import android.view.View;
   import android.widget.Button;
   import android.widget.Toast;
   public class MainActivity extends ActionBarActivity {



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

        Button start = (Button) findViewById(R.id.btnstartservice);
        Button stop = (Button) findViewById(R.id.btnstopservice);


        start.setOnClickListener(new View.OnClickListener() {@Override
            public void onClick(View v) {

                Toast.makeText(MainActivity.this, "My First Service Started ", Toast.LENGTH_LONG).show();

            }
        });
Giru Bhai
  • 14,370
  • 5
  • 46
  • 74
1

get the context from the view being clicked (use the v argument)

@Override
public void onClick(View v) {
   Toast.makeText(v.getContext() , "My First Service Started ",Toast.LENGTH_LONG).show();
}
petey
  • 16,914
  • 6
  • 65
  • 97