android widget textview动态更新

时间:2013-03-29 11:59:17

标签: java android widget textview

我目前正在学习Android中的小部件。 我希望能够更新我的textview,但每次当我尝试更新它时 - 我的程序崩溃了。 这是我的班级:

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);
    }
}`

清单

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sunmille.day"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="9"
    android:targetSdkVersion="17" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >

    <receiver
        android:name="DayWidget">
        <intent-filter>
            <action
                android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            <action
                android:name="android.appwidget.action.ACTION_WIDGET_RECEIVER" /> 
        </intent-filter>
        <meta-data
            android:name="android.appwidget.provider"
            android:resource="@xml/widget_data" />
    </receiver>

</application>

小工具数据

<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">

小部件布局

<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" >

  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center" >

    <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" />

    <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" />    

</LinearLayout>

  <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" />

日志:

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)

正确代码:

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

}

1 个答案:

答案 0 :(得分:0)

views类型的变量RemoteViews为空。你可以在Logcat输出上看到它。