0

I am making an app in which i have to check whether the device is in airplane mode or not and i am using follwing code and it is null pointer exception .My code is as follows:

 public static boolean isAirplaneModeOn(Context context)
            {
    System.out.println("test1");
                    return Settings.System.getInt(context.getContentResolver(),Settings.System.AIRPLANE_MODE_ON, 0) != 0;

            }

    /**
     *
     * @param status
     */
    public  void  setAirplaneMode(Context context,boolean status)
    {System.out.println("test111");
            boolean isAirplaneModeOn = isAirplaneModeOn(context);// **null pointer exception**
            if(isAirplaneModeOn && status)
            {
                    return;
            }
            if(!isAirplaneModeOn && !status)
            {
                    return;
            }
            if(isAirplaneModeOn && !status)
            {
             Settings.System.putInt(getApplicationContext().getContentResolver(),
                    Settings.System.AIRPLANE_MODE_ON, 0);
            Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
            intent.putExtra("state", 0);
            getApplicationContext().sendBroadcast(intent);
            return;
            }
            if(!isAirplaneModeOn && status)
            {
             Settings.System.putInt(getApplicationContext().getContentResolver(),
                    Settings.System.AIRPLANE_MODE_ON, 1);
            Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);

            intent.putExtra("state", 1);

            getApplicationContext().sendBroadcast(intent);

            return;
            }
    }

Can Any one tell me how to get mAirplaneEnabled flag value?

My logcat is as follows:

    03-13 14:57:04.507: I/System.out(9185): test111
    03-13 14:57:04.507: I/System.out(9185): test1
    03-13 14:57:04.515: I/System.out(9185): java.lang.NullPointerException
Aditya1510
  • 854
  • 1
  • 14
  • 31

1 Answers1

0

The npe is due to context because context is null here v.so the code snippet is likely below:

public  boolean isAirplaneModeOn(Context context) 
            {

            return Settings.System.getInt(ClassName.this.getContentResolver(),Settings.System.AIRPLANE_MODE_ON, 0)!=0;

            }
Aditya1510
  • 854
  • 1
  • 14
  • 31