2011-05-16 46 views
1

這個問題的背景。我有一個appwidget與我的應用程序相關聯,定期更新設置的時間間隔,使用更新服務將http帖子發送到服務器,並使用從服務器接收的數據更新小部件。應用小部件更新服務部隊有時會關閉 - 需要幫助!

我從我的測試和用戶報告中已經注意到,這個特定的強制關閉實例週期性地(但很少)發生,當它更新小部件的時間以及網絡狀態從可用更改爲不可用時。因爲我在紐約地鐵的手機上注意到了這一點。

在調試過程中,我發現如果http post發生並且網絡狀態在收到響應之前發生變化,它基本上會收到IOException。所以我處理了這個異常,並用默認更新更新了這個特殊情況下的小部件。它運行良好。

但有趣的是,我已經注意到這個力量再次關閉,並且如何解決這個問題已經不多了。

有沒有人遇到過這個,知道我可以如何處理這個?

java.lang.NullPointerException 
    at android.widget.RemoteViews$ReflectionAction.writeToParcel(RemoteViews.java:399) 
    at android.widget.RemoteViews.writeToParcel(RemoteViews.java:1003) 
    at com.android.internal.appwidget.IAppWidgetService$Stub$Proxy.updateAppWidgetProvider(IAppWidgetService.java:402) 
    at android.appwidget.AppWidgetManager.updateAppWidget(AppWidgetManager.java:283) 
    at com.tyu.android.TyuWidget$UpdateService$1.run(TyuWidget.java:167) 

一些代碼片段,可以幫助你的專家,以便更好地理解這個問題,並幫助初學者。

@Override 
public void onReceive(Context context, Intent intent) { 
     check_intent = intent.getAction(); 
      if(check_intent.equals("android.appwidget.action.APPWIDGET_UPDATE")){ 
     this.onUpdate(context, intent); 
     } 
} 

這裏是OnUpdate方法代碼片段。

public void onUpdate(Context context, Intent intent){ 
      Intent widgetUpdate = new Intent(context, TyuWidget.class); 
      widgetUpdate.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE); 
      AlarmManager alarms = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); 
      PendingIntent newPending = PendingIntent.getBroadcast(context, 0, widgetUpdate,PendingIntent.FLAG_UPDATE_CURRENT); 
      alarms.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime()+ PERIOD, newPending); 
      context.startService(new Intent(context, UpdateService.class)); 

} 

線程內更新小部件的UpdateService類的OnStart方法。

widgetUpdateThread = new Thread(){ 
       @Override 
       public void run(){ 
        RemoteViews updateViews = buildUpdate(getApplicationContext()); 
        if(updateViews!=null){ 
         ComponentName thisWidget = new ComponentName(getApplicationContext(), TyuWidget.class); 
         AppWidgetManager manager = AppWidgetManager.getInstance(getApplicationContext()); 
         manager.updateAppWidget(thisWidget, updateViews); 
        } 
        else{ 
         updateViews = new RemoteViews(getApplicationContext().getPackageName(), R.layout.tuwidget); 
         updateViews.setImageViewResource(R.id.ad, R.drawable.tyu_null_game); 
         Intent defineIntent1 = new Intent(getApplicationContext(), Tyu3.class); 
         PendingIntent pendingIntent1 = PendingIntent.getActivity(getApplicationContext(), 
           0 /* no requestCode */, defineIntent1, 0 /* no flags */); 
         updateViews.setOnClickPendingIntent(R.id.tuwidget, pendingIntent1); 
         ComponentName thisWidget = new ComponentName(getApplicationContext(), TyuWidget.class); 
         AppWidgetManager manager = AppWidgetManager.getInstance(getApplicationContext()); 
         manager.updateAppWidget(thisWidget, updateViews); 

        } 
       } 
      };    
      widgetUpdateThread.start(); 

buildUpdate方法代碼片段。

public RemoteViews buildUpdate(Context context) { 

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
response = httpclient.execute(httppost); 
entity = response.getEntity(); 
is = entity.getContent(); 

//etc etc...and then I return the update view and it gets updated. 
} 

感謝您的幫助。

+0

NPE從哪一行開始? – 2011-05-16 19:02:29

+0

@Programmer布魯斯manager.updateAppWidget(thisWidget,updateViews);內部widgetUpdateThread運行()...這就是Android Market Force Close報告告訴我的。在com.tyu.android.TyuWidget $ UpdateService $ 1.run(TyuWidget.java:167) – Aakash 2011-05-16 19:05:22

+0

錯誤發生時該行代碼是否爲null? – 2011-05-16 20:09:23

回答

2

擴展Service類的UpdateService類的OnStart()方法中的線程正在創建該問題。

解決方案:

擺脫了widgetUpdateThread線程和代替擴展服務類,用於IntentService。這會自動產生一個線程,並像魅力一樣工作。而不是OnStart,當使用IntentService時,我把代碼放在@Override onHandleIntent和Voila中!即使在並行使用非常非常CPU和內存密集的應用程序(如FruitNinja)時,也不會強行關閉。

夥計! IntentService由開發人員社區和Android Framework工程師強烈推薦。所以在更新小部件時檢查一下。

0

我認爲thisWidget的值可能在null中的manager.updateAppWidget(thisWidget,updateViews)。

希望這會有所幫助。 CD。