0

I wrote an Android widget with alarmmanager as updating process. Sometimes i've got a nullpointer exception. But all objects are initialized...

public void UpdateWidget(Context con)
{
    //Log.d("berlinClock", "UpdateWidget="+i);
    RemoteViews views=new RemoteViews(con.getPackageName(),R.layout.widgetlayout);
    Calendar c = Calendar.getInstance(); 
    int h = c.get(Calendar.HOUR_OF_DAY);
    int m = c.get(Calendar.MINUTE);

    ComponentName thisWidget = new ComponentName(con,widget.class);

    //update views on remoteView
    // GOT NULLPOINTER EXEPTION IN FOLLOWING LINE
    views.setImageViewResource(R.id.wha1, R.drawable.cgrau);
    views.setImageViewResource(R.id.wha2, R.drawable.cgrau);
    ...
    }

Here is the Stacktrace:

java.lang.RuntimeException: Unable to start receiver ms.jung.android.berlinclock.widget: java.lang.NullPointerException
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2821)
at android.app.ActivityThread.access$3200(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2083)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at ms.jung.android.berlinclock.widget.UpdateWidget(widget.java:96)
at ms.jung.android.berlinclock.widget.onReceive(widget.java:34)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2810)

2 Answers 2

1
if(views!=null)
{
    ...
}

With this if condition it will not crash anymore, i saw that solution now in many examples, and i think that is the best way to do that.

Sign up to request clarification or add additional context in comments.

Comments

0

I think it might be because your RemoteViews object has not yet been inflated.

You can try putting your view inside of a layout, and inflating that layout.

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
RemoteViews view = inflater.inflate(R.layout.widgetlayout,null);

5 Comments

should i do something like: while(views==null){views=...} ?
this inflate method does not exist :(
Oh, my bad, the second parameter should be a ViewGroup object but is optional, should pass null if you don't want to pass an object. I made the correction.
Thats not possible: "cannot convert view to remoteview"
Have you tried running the UpdateWidget method on the activity's onStart() method? onStart() is called when the activity becomes visible so your views must have been inflated by the time they are called.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.