0

我正在使用 Home 小部件做一个应用程序。

主页小部件第一次工作正常,而删除主页小部件正常并再次添加主页小部件强制关闭。

我的代码:

 @Override
        public void onUpdate(Context context, AppWidgetManager appWidgetManager1,int[] appWidgetIds1) {
            appWidgetIds=appWidgetIds1;
            appWidgetManager=appWidgetManager1;
            context.startService(new Intent(context, UpdateService.class));     
        }

public static class UpdateService extends Service {

        @Override
        public void onStart(Intent intent, int startId) {

            remoteViews = new RemoteViews(getPackageName(),R.layout.widget_layout);
            Date month=new Date(System.currentTimeMillis());
            todayDate=month.getDate();
            todayMonth=currnetMonth=(month.getMonth())+1;
            todayYear=currentYear=(month.getYear())+1900;

           // Build the widget update for today
            RemoteViews updateViews = buildUpdate(this);
            // Push update for this widget to the home screen
            ComponentName thisWidget = new ComponentName(this, MyWidgetProvider.class);
            AppWidgetManager manager = AppWidgetManager.getInstance(this);
            manager.updateAppWidget(thisWidget, updateViews);           
        }

        public RemoteViews buildUpdate(Context context) {

            list = UpdateWidgetDatas();
            try{
                int fr_size=list.length;
                if(fr_size>0){
                    remoteViews.setViewVisibility(R.id.loading, View.GONE);     
                    remoteViews.setViewVisibility(R.id.top_rel, View.VISIBLE);      
                    remoteViews.setTextViewText(R.id.title, "title");   
                    remoteViews.setTextViewText(R.id.des,"description");    
                    remoteViews.setTextViewText(R.id.date, "todayDate");    
                    remoteViews.setImageViewBitmap(R.id.friend_image, downloadBitMapImage("imagelink"));    

                }else{
                    remoteViews.setViewVisibility(R.id.top_rel, View.GONE);     
                    remoteViews.setViewVisibility(R.id.loading, View.VISIBLE);  
                    remoteViews.setTextViewText(R.id.loading, "No data Available.");            
                }           
                Intent BRHomePage = new Intent(context, HomePage.class);
                PendingIntent HomePageIntent=PendingIntent.getActivity(context, 0 , BRHomePage,PendingIntent.FLAG_UPDATE_CURRENT);
                remoteViews.setOnClickPendingIntent(R.id.top_rel, HomePageIntent); 
            }catch (Exception e) {
                // TODO: handle exception
                System.out.println("store error----"+e);
            }
            appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
            return remoteViews;         
        }


        public void onReceive(Context context, Intent intent) 
        {           
            final String action = intent.getAction();

            if(action.equalsIgnoreCase("android.appwidget.action.APPWIDGET_DELETED")){
                if(remoteViews!=null)
                    remoteViews=null;
            }
        }


        @Override
        public void onDestroy() {
            System.gc();            
            Toast.makeText(this, "removed widger", Toast.LENGTH_SHORT).show();
        }

        public static List<String> UpdateWidgetDatas() {

            try {
                //return list value
            }catch (Exception e) {
                Log.e("", "FacebookDetails--->"+e);
            }

            return list;            
        }               

        Bitmap downloadBitMapImage(String imageUrl){
            Bitmap imgBitmap=null;
            try
            {
                URL url= new URL(imageUrl);
                HttpURLConnection c = ( HttpURLConnection ) url.openConnection();
                c.setDoInput(true);
                c.connect();
                InputStream is = c.getInputStream();
                imgBitmap = BitmapFactory.decodeStream(is);
            }
            catch ( MalformedURLException e )
            {
                Log.d( "RemoteImageHandler", "fetchImage passed invalid URL: " + imageUrl );
            }
            catch ( IOException e )
            {
                Log.d( "RemoteImageHandler", "fetchImage IO exception: " + e );
            }
            return imgBitmap;
        }

} 

logcat 中出现错误:

10-29 17:25:51.849: ERROR/(160): Dumpstate > /data/log/dumpstate_app_error
10-29 17:25:51.849: ERROR/AndroidRuntime(7270): FATAL EXCEPTION: main
10-29 17:25:51.849: ERROR/AndroidRuntime(7270): java.lang.RuntimeException: Unable to start service cmyapp.MyWidgetProvider$UpdateService@4051e0c8 with Intent { cmp=cmyapp/.MyWidgetProvider$UpdateService }: java.lang.NullPointerException
10-29 17:25:51.849: ERROR/AndroidRuntime(7270):     at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2056)
10-29 17:25:51.849: ERROR/AndroidRuntime(7270):     at android.app.ActivityThread.access$2800(ActivityThread.java:117)
10-29 17:25:51.849: ERROR/AndroidRuntime(7270):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:998)
10-29 17:25:51.849: ERROR/AndroidRuntime(7270):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-29 17:25:51.849: ERROR/AndroidRuntime(7270):     at android.os.Looper.loop(Looper.java:130)
10-29 17:25:51.849: ERROR/AndroidRuntime(7270):     at android.app.ActivityThread.main(ActivityThread.java:3687)
10-29 17:25:51.849: ERROR/AndroidRuntime(7270):     at java.lang.reflect.Method.invokeNative(Native Method)
10-29 17:25:51.849: ERROR/AndroidRuntime(7270):     at java.lang.reflect.Method.invoke(Method.java:507)
10-29 17:25:51.849: ERROR/AndroidRuntime(7270):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
10-29 17:25:51.849: ERROR/AndroidRuntime(7270):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
10-29 17:25:51.849: ERROR/AndroidRuntime(7270):     at dalvik.system.NativeStart.main(Native Method)
10-29 17:25:51.849: ERROR/AndroidRuntime(7270): Caused by: java.lang.NullPointerException
10-29 17:25:51.849: ERROR/AndroidRuntime(7270):     at android.widget.RemoteViews$ReflectionAction.writeToParcel(RemoteViews.java:730)
10-29 17:25:51.849: ERROR/AndroidRuntime(7270):     at android.widget.RemoteViews.writeToParcel(RemoteViews.java:1376)
10-29 17:25:51.849: ERROR/AndroidRuntime(7270):     at com.android.internal.appwidget.IAppWidgetService$Stub$Proxy.updateAppWidgetIds(IAppWidgetService.java:374)
10-29 17:25:51.849: ERROR/AndroidRuntime(7270):     at android.appwidget.AppWidgetManager.updateAppWidget(AppWidgetManager.java:246)
10-29 17:25:51.849: ERROR/AndroidRuntime(7270):     at cmyapp.MyWidgetProvider$UpdateService.buildUpdate(MyWidgetProvider.java:168)
10-29 17:25:51.849: ERROR/AndroidRuntime(7270):     at cmyapp.MyWidgetProvider$UpdateService.onStart(MyWidgetProvider.java:87)
10-29 17:25:51.849: ERROR/AndroidRuntime(7270):     at android.app.Service.onStartCommand(Service.java:428)
10-29 17:25:51.849: ERROR/AndroidRuntime(7270):     at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2043)

请推荐正确的方法,谢谢,

4

0 回答 0