title: android widget textview dynamically update
body: <p>I am currently learning about widgets in Android.
  I want to be able to update my textview, but each time when I try to update it - my program crashes.
  Here is my class:</p>
  
  <pre><code>public class DayWidget extends AppWidgetProvider {
  
      public static CheckDate CheckDate;
      public static EventList EventList;
  
      public static String EVENT_CLICK = "EventClick";
      public String msg;
  
      public RemoteViews views;
      public AppWidgetManager manager;
      public Context cntxt;
      public Intent intnt;
      public int[] ids;
  
      public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds){
          super.onUpdate(context, appWidgetManager, appWidgetIds);
  
          CheckDate = new CheckDate();
          EventList = new EventList();
  
          manager = appWidgetManager;
          views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
          ids = appWidgetIds;
  
          Intent intent = new Intent(context, DayWidget.class);
          intent.setAction(EVENT_CLICK);
          intent.putExtra("msg", "change");
  
          PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
  
          views.setOnClickPendingIntent(R.id.Event, actionPendingIntent);
  
          CheckDate.Check();
  
          views.setTextViewText(R.id.Today, CheckDate.Today);
  
          manager.updateAppWidget(ids, views);
      }
  
      public void UpdateWidget(){
          Bundle extras = intnt.getExtras();
          if(extras!=null) {
              AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(cntxt);
              ComponentName AppWidget = new ComponentName(cntxt.getPackageName(), DayWidget.class.getName());
              int[] appWidgetIds = appWidgetManager.getAppWidgetIds(AppWidget);
              views.setTextViewText(R.id.Event, "New text");
  
              onUpdate(cntxt, appWidgetManager, appWidgetIds);
          }
      }
  
      public void onReceive(Context context, Intent intent){
          intnt = intent;
          cntxt = context;
  
          if(EVENT_CLICK.equals(intent.getAction())){
              try{
                  msg = intent.getStringExtra("msg");
              }catch(NullPointerException e){
                  e.printStackTrace();
              }
              UpdateWidget();
              //Toast toast = Toast.makeText(cntxt, "TEST", Toast.LENGTH_LONG);
              //toast.show();
          }
          super.onReceive(context, intent);
      }
  }`
  </code></pre>
  
  <p>Manifest</p>
  
  <pre><code>&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.sunmille.day"
  android:versionCode="1"
  android:versionName="1.0" &gt;
  
  &lt;uses-sdk
      android:minSdkVersion="9"
      android:targetSdkVersion="17" /&gt;
  
  &lt;application
      android:allowBackup="true"
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name" &gt;
  
      &lt;receiver
          android:name="DayWidget"&gt;
          &lt;intent-filter&gt;
              &lt;action
                  android:name="android.appwidget.action.APPWIDGET_UPDATE" /&gt;
              &lt;action
                  android:name="android.appwidget.action.ACTION_WIDGET_RECEIVER" /&gt; 
          &lt;/intent-filter&gt;
          &lt;meta-data
              android:name="android.appwidget.provider"
              android:resource="@xml/widget_data" /&gt;
      &lt;/receiver&gt;
  
  &lt;/application&gt;
  </code></pre>
  
  <p></p>
  
  <p>Widget data</p>
  
  <pre><code>&lt;appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" 
  android:minWidth="294dp"
  android:minHeight="72dp"
  android:minResizeWidth="294dp"
  android:minResizeHeight="72dp"
  android:updatePeriodMillis="0"
  android:resizeMode="horizontal|vertical"
  android:initialLayout="@layout/widget_layout"&gt;
  </code></pre>
  
  <p></p>
  
  <p>Widget layout</p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  android:background="@drawable/widget_background" &gt;
  
    &lt;LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:gravity="center" &gt;
  
      &lt;TextView
          android:id="@+id/TopText"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Cегодня "
          android:textIsSelectable="false"
          android:textColor="#000000"
          android:textSize="20sp"
          android:textAppearance="?android:attr/textAppearanceLarge" /&gt;
  
      &lt;TextView
          android:id="@+id/Today"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:textIsSelectable="false"
          android:textColor="#000000"
          android:textSize="20sp"
          android:textAppearance="?android:attr/textAppearanceLarge" /&gt;    
  
  &lt;/LinearLayout&gt;
  
    &lt;TextView
        android:id="@+id/Event"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
          android:textIsSelectable="false"
          android:textColor="#000000"
          android:textSize="20sp"
          android:text="Some event"
        android:textAppearance="?android:attr/textAppearanceLarge" /&gt;
  </code></pre>
  
  <p></p>
  
  <p>Log:</p>
  
  <pre><code>03-29 14:10:15.954: E/AndroidRuntime(4552): java.lang.RuntimeException: Unable to start receiver com.sunmille.day.DayWidget: java.lang.NullPointerException
  03-29 14:10:15.954: E/AndroidRuntime(4552):     at com.sunmille.day.DayWidget.UpdateWidget(DayWidget.java:61)
  03-29 14:10:15.954: E/AndroidRuntime(4552):     at com.sunmille.day.DayWidget.onReceive(DayWidget.java:77)
  </code></pre>
  
  <p>CORRECT CODE:</p>
  
  <pre><code>public class DayWidget extends AppWidgetProvider {
  
  public static CheckDate CheckDate;
  public static EventList EventList;
  
  public static String EVENT_CLICK = "EventClick";
  public String msg;
  
  public RemoteViews rviews;
  public AppWidgetManager manager;
  public Context cntxt;
  public Intent intnt;
  public int[] ids;
  
  public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds){
      super.onUpdate(context, appWidgetManager, appWidgetIds);
  
      CheckDate = new CheckDate();
      EventList = new EventList();
  
      manager = appWidgetManager;
      RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
      ids = appWidgetIds;
  
      Intent intent = new Intent(context, DayWidget.class);
      intent.setAction(EVENT_CLICK);
      intent.putExtra("msg", "change");
  
      PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
  
      views.setOnClickPendingIntent(R.id.Event, actionPendingIntent);
  
      CheckDate.Check();
  
      views.setTextViewText(R.id.Today, CheckDate.Today);
  
      manager.updateAppWidget(ids, views);
  }
  
  public void UpdateWidget(){
      Bundle extras = intnt.getExtras();
      if(extras!=null) {
          AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(cntxt);
          ComponentName AppWidget = new ComponentName(cntxt.getPackageName(), DayWidget.class.getName());
          int[] appWidgetIds = appWidgetManager.getAppWidgetIds(AppWidget);
  
          rviews = new RemoteViews(AppWidget.getPackageName(), R.layout.widget_layout);
          rviews.setTextViewText(R.id.Event, "New text");
  
          appWidgetManager.updateAppWidget(appWidgetIds, rviews);
      }
  }
  
  public void onReceive(Context context, Intent intent){
      intnt = intent;
      cntxt = context;
  
      if(EVENT_CLICK.equals(intent.getAction())){
          try{
              msg = intent.getStringExtra("msg");
          }catch(NullPointerException e){
              e.printStackTrace();
          }
          UpdateWidget();
          Toast toast = Toast.makeText(cntxt, "TEST", Toast.LENGTH_LONG);
          toast.show();
      }
      super.onReceive(context, intent);
  }
  </code></pre>
  
  <p>}</p>