-1

I have a GPSLoc class with the following method

public  class GPSLoc   { 
    public static void HAHA ()
      {
          {Toast.makeText( context,"SD Card not available , savin to local phone", Toast.LENGTH_LONG).show();};
      }

      private static  Context context;

then I am creating a button in the main activity with the simple task of calling that method and display the toast

Button three=(Button)findViewById(R.id.three);
three.setOnClickListener(new Button.OnClickListener() {

    public void onClick(View v) {

        GPSLoc Test1=new GPSLoc();  
        Test1.HAHA();

    }

and then I got that in the LogCat

 07-13 20:41:39.492: E/AndroidRuntime(6778): FATAL EXCEPTION: main
  07-13 20:41:39.492: E/AndroidRuntime(6778): java.lang.NullPointerException
  07-13 20:41:39.492: E/AndroidRuntime(6778):   at android.widget.Toast.<init>(Toast.java:90)
  07-13 20:41:39.492: E/AndroidRuntime(6778):   at android.widget.Toast.makeText(Toast.java:232)
  07-13 20:41:39.492: E/AndroidRuntime(6778):   at com.example.testnewbutton.GPSLoc.HAHA(GPSLoc.java:100)
  07-13 20:41:39.492: E/AndroidRuntime(6778):   at com.example.testnewbutton.MainActivity$1.onClick(MainActivity.java:67)
Cœur
  • 37,241
  • 25
  • 195
  • 267
user2557930
  • 319
  • 2
  • 11

2 Answers2

0

context inside GPSLoc is null. You never initialize it, since is a private field

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
0

Set context

private static Context context = getApplicationContext();
Srikanth Roopa
  • 1,782
  • 2
  • 13
  • 19
  • after playing a little bit, i managed to make it work with getApplicationContext, thanks a million.Do you have the link for som tutorial , which explains the matter of contexts and none activity classes in depth ? Thanks – user2557930 Jul 14 '13 at 16:56
  • check this CommonsWare has explained perfectly :) [context](http://stackoverflow.com/questions/7298731/when-to-call-activity-context-or-application-context) – Srikanth Roopa Jul 14 '13 at 17:49