2011-05-16 28 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。