Android widget service works only until API9 why?

Can some one please help me to analize why this code that is a widget service only work @ api 8,9 and 10? this donĀ“t work on android API 17 4.2.2, also ocurrs an error when install.

When i run on 2.3.3 terminal it runs perfetcly and the show the feed!

the logcat i got when running on 4.2.2 terminal is this:

12-13 22:00:27.907: D/widget(1438): onUpdate()
12-13 22:00:27.957: D/widget(1438): Beginning update cycle...
12-13 22:00:28.017: D/AndroidRuntime(1438): Shutting down VM
12-13 22:00:28.017: W/dalvikvm(1438): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
12-13 22:00:28.059: E/AndroidRuntime(1438): FATAL EXCEPTION: main
12-13 22:00:28.059: E/AndroidRuntime(1438): java.lang.RuntimeException: Unable to start service com.example.rss.widgetservice@40ce59f8 with Intent { cmp=com.example.rss/.widgetservice }: java.lang.NullPointerException: println needs a message
12-13 22:00:28.059: E/AndroidRuntime(1438):     at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2673)
12-13 22:00:28.059: E/AndroidRuntime(1438):     at android.app.ActivityThread.access$1900(ActivityThread.java:141)
12-13 22:00:28.059: E/AndroidRuntime(1438):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1331)
12-13 22:00:28.059: E/AndroidRuntime(1438):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-13 22:00:28.059: E/AndroidRuntime(1438):     at android.os.Looper.loop(Looper.java:137)
12-13 22:00:28.059: E/AndroidRuntime(1438):     at android.app.ActivityThread.main(ActivityThread.java:5041)
12-13 22:00:28.059: E/AndroidRuntime(1438):     at java.lang.reflect.Method.invokeNative(Native Method)
12-13 22:00:28.059: E/AndroidRuntime(1438):     at java.lang.reflect.Method.invoke(Method.java:511)
12-13 22:00:28.059: E/AndroidRuntime(1438):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-13 22:00:28.059: E/AndroidRuntime(1438):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-13 22:00:28.059: E/AndroidRuntime(1438):     at dalvik.system.NativeStart.main(Native Method)
12-13 22:00:28.059: E/AndroidRuntime(1438): Caused by: java.lang.NullPointerException: println needs a message
12-13 22:00:28.059: E/AndroidRuntime(1438):     at android.util.Log.println_native(Native Method)
12-13 22:00:28.059: E/AndroidRuntime(1438):     at android.util.Log.d(Log.java:138)
12-13 22:00:28.059: E/AndroidRuntime(1438):     at com.example.rss.widgetservice.onStart(widgetservice.java:44)
12-13 22:00:28.059: E/AndroidRuntime(1438):     at android.app.Service.onStartCommand(Service.java:450)
12-13 22:00:28.059: E/AndroidRuntime(1438):     at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2656)
12-13 22:00:28.059: E/AndroidRuntime(1438):     ... 10 more

Here is the code:

Please some one help me to understand this!

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.RemoteViews;

public class widgetservice extends Service {  
      @Override  
      public void onStart(Intent intent, int startId) {  
         RemoteViews rviews = new RemoteViews(this.getPackageName(), R.layout.widget_layout);  
         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();  
         ComponentName meuWidget = new ComponentName(this, widget.class);  
         AppWidgetManager manager = AppWidgetManager.getInstance(this);  
         try {  
             rviews.setTextViewText(R.id.TextView01, "Updating...");  
             manager.updateAppWidget(meuWidget, rviews);  
             Log.d("widget", "Beginning update cycle...");  
             DocumentBuilder builder = factory.newDocumentBuilder();  
             Document dom = builder.parse("http://blog.masterd.pt/index.php/feed/");  
             Element root = dom.getDocumentElement();  
             NodeList items = root.getElementsByTagName("item");  
             if (items.getLength() > 0) {  
                Element entrada = (Element) items.item(0);  
                NodeList titulos = entrada.getElementsByTagName("title");  
                Element titulo = (Element) titulos.item(0);  
                String texto = titulo.getFirstChild().getNodeValue();  
                rviews.setTextViewText(R.id.TextView01, texto);  
                Log.d("widget", "Update cycle completed!");  
                manager.updateAppWidget(meuWidget, rviews);  
             }  
        }  
        catch(Exception ex) {  
            Log.d("widget", ex.getMessage());  
        }  
      }  

      @Override  
         public IBinder onBind(Intent intent) {  
           return null;  
        }  
      }  

And here is my manifest

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.rss"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="7"
        android:targetSdkVersion="18" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <receiver android:name="com.example.rss.widget">
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>
            <meta-data android:name="android.appwidget.provider" 
                android:resource="@xml/widget_info">
                        </meta-data>
            </receiver>
        <activity
            android:name="com.example.rss.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service android:name="com.example.rss.widgetservice" />

    </application>

</manifest>

And My widget provider

import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class widget  extends AppWidgetProvider {


    @Override  
    public void onUpdate(Context context,   
        AppWidgetManager appWidgetManager,  
        int[] appWidgetIds) {  
      Log.d("widget", "onUpdate()");  
      context.startService(new Intent(context, widgetservice.class));  
    } 
}

Answers


12-13 22:00:28.059: E/AndroidRuntime(1438): Caused by: java.lang.NullPointerException: println needs a message
12-13 22:00:28.059: E/AndroidRuntime(1438):     at android.util.Log.println_native(Native Method)
12-13 22:00:28.059: E/AndroidRuntime(1438):     at android.util.Log.d(Log.java:138)
12-13 22:00:28.059: E/AndroidRuntime(1438):     at com.example.rss.widgetservice.onStart(widgetservice.java:44)  

java.lang.NullPointerException: println needs a message

Seems that ex.getMessage() returns null at this block

catch(Exception ex) {  
    Log.d("widget", ex.getMessage());  
}  

Change code to this:

catch(Exception ex) {  
    String error = (ex.getMessage()==null)?"Some message error":ex.getMessage();
    Log.d("widget", error);  
}  

Thank all for your helo! @Ramaral you made me see what is the problem... on honeycomb to jelly bean we have to use async task... so an easy way to get this work on jelly bean was to remove uses sdk min and max version Now all work perfect!!! thanks so much! case SOLVED


Need Your Help

Can I show user data to other users in Stormpath?

swift stormpath express-stormpath stormpath-express

I'm trying to integrate Stormpath to an iOS app (Swift), and would like to be able to show one user's data to another user.

How to write my following C code using setjmp/longjmp in Python?

ctypes longjmp setjmp

I was wondering if the implementation of setjmp/longjmp is possible in Python? If not, is there any equivalent alternative?