title: Can't get android flash light widget to turn flash on
body: <p>I am trying to make a flash light widget for the galaxy nexus and have been stuck on this problem FOR DAYS. I have a widget that consists of a button that functions like a toggle. But I can't get the button to turn on the flash. The widget works as a toggle using toasts, but once I add the lines of code that communicate with the flash it crashes. So if you only left the toast code in each if statement, then the toasts would work. I know I am supposed to be using SurfaceHolder.Callback but I dont know how to implement that on a class that extends AppWidgetProvider. If someone could please look over my code and try to help me make this widget I would be very grateful. I just don't know how to implement this with the appwidgetprovider class. </p>
  
  <p>This error comes up only when I have: </p>
  
  <pre><code>//TURN FLASH OFF
  Parameters p = cam.getParameters();
  p.setFlashMode(Parameters.FLASH_MODE_OFF);
  cam.setParameters(p);
  cam.stopPreview();
  </code></pre>
  
  <p>and:</p>
  
  <pre><code>//TURN FLASH ON
  Parameters p = cam.getParameters();
  p.setFlashMode(Parameters.FLASH_MODE_TORCH);                    
  cam.setParameters(p);
  cam.startPreview();
  </code></pre>
  
  <p>If I dont put these lines of code in, the widget runs with no errors and only displays the toasts. Meaning it accesses the onReceive() method.</p>
  
  <p>I am using a galaxy nexus. </p>
  
  <pre><code>public class FlashWidget extends AppWidgetProvider {
  
  Camera cam;
  RemoteViews view;
  private static final String ACTION_WIDGET_RECEIVER = "Action";
  private static boolean isLightOn = false;
  
  
  
   @Override
      public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
  
          final int n = appWidgetIds.length;
          view = new RemoteViews(context.getPackageName(), R.layout.widget_lay);
  
          for (int i = 0; i &lt; n; i++) {  
                 int appWidgetId = appWidgetIds[i];  
  
                 Intent intent = new Intent(context, FlashWidget.class);
                 intent.setAction(ACTION_WIDGET_RECEIVER);
  
                 PendingIntent pend = PendingIntent.getBroadcast(context, 0, intent, 0);
  
                 view.setOnClickPendingIntent(R.id.button1, pend);
  
  
                 appWidgetManager.updateAppWidget(appWidgetId, view);
          }
  
  
      }
  
      @Override
      public void onReceive(Context context, Intent intent) {
  
          String action =intent.getAction();
  
          if(intent.getAction().equals(action)) {
  
  
              //YES ITS ON, so turn it OFF
              if(isLightOn) {
  
                  isLightOn = false;
                  //TURN FLASH OFF
                  Parameters p = cam.getParameters();
                  p.setFlashMode(Parameters.FLASH_MODE_OFF);
                  cam.setParameters(p);
                  cam.stopPreview();
                  Toast.makeText(context, "Flash OFF", Toast.LENGTH_SHORT).show();
  
              //NO ITS OFF, SO LETS TURN IT ON
              } else {
  
                  isLightOn = true;
                  //TURN FLASH ON
                  Parameters p = cam.getParameters();
                  p.setFlashMode(Parameters.FLASH_MODE_TORCH);
  
                  cam.setParameters(p);
                  cam.startPreview();
                  Toast.makeText(context, "Flash ON", Toast.LENGTH_SHORT).show();
  
              }
  
              super.onReceive(context, intent);
  
          }
  
  
      } 
  }
  </code></pre>
  
  <p>AndroidManifest.xml</p>
  
  <pre><code>&lt;uses-sdk
  android:minSdkVersion="14"
  android:targetSdkVersion="17" /&gt;
  
  &lt;uses-permission android:name="android.permission.CAMERA"/&gt;
  &lt;uses-feature android:name="android.hardware.camera"/&gt;
  
  
  
  &lt;application
      android:allowBackup="true"
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" &gt;
      &lt;activity
          android:name="com.example.flash.MainActivity"
          android:label="@string/app_name" &gt;
          &lt;intent-filter&gt;
              &lt;action android:name="android.intent.action.MAIN" /&gt;
              &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
          &lt;/intent-filter&gt;
      &lt;/activity&gt;
  
      &lt;receiver 
          android:name=".FlashWidget"
          android:label="Flash"&gt;
          &lt;intent-filter&gt;
              &lt;action android:name="android.appwidget.action.APPWIDGET_UPDATE"/&gt;
              &lt;action android:name="com.example.flash.ACTION_WIDGET_RECEIVER"/&gt;
              &lt;action android:name="android.appwidget.action.APPWIDGET_ENABLED"/&gt;
          &lt;/intent-filter&gt;
          &lt;meta-data 
              android:name="android.appwidget.provider"
              android:resource="@xml/widget_in"/&gt;
     &lt;/receiver&gt;
  &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>
  
  <p>And this is the error I get when I try to put the widget on my home screen</p>
  
  <pre><code>E/AndroidRuntime(10346): FATAL EXCEPTION: main
  E/AndroidRuntime(10346): java.lang.RuntimeException: Unable to start receiver com.example.flash.FlashWidget: java.lang.NullPointerException
  at android.app.ActivityThread.handleReceiver(ActivityThread.java:2236)
  E/AndroidRuntime(10346): at android.app.ActivityThread.access$1500(ActivityThread.java:130)
  E/AndroidRuntime(10346): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1271)
  </code></pre>