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

                }
            });
0

4 Answers 4

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

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

1
  • 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, 2015 at 14:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.