1

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

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

}

4

1 回答 1

0

views类型变量RemoteViews为空。您可以在 Logcat 输出中看到它。

于 2013-03-29T12:23:16.287 回答