Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9140101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:28:20+00:00 2026-06-17T09:28:20+00:00

I am trying to create a widget for my app that displays the device’s

  • 0

I am trying to create a widget for my app that displays the device’s CPU usage, Battery charge and remaining RAM; not the storage space.

The CPU level and battery charge code works fine; but the RAM code does not. I have read that in order to use .getSystemService() in an AppWidgetProvider you must pass a context from an activity to here.

The issue is, when the method tries to set up a systemService with an ActivityManager, it throws :

01-11 21:29:21.468: E/AndroidRuntime(30759): java.lang.RuntimeException: Unable to instantiate receiver com.example.myfirstappex.AppWidget: java.lang.NullPointerException

I have narrowed the problem to this block of code :

    import com.example.myfirstappex.MainActivity;

    public class AppWidget extends AppWidgetProvider {
    ...
    public long getRam()
{

    MemoryInfo mi = new MemoryInfo();

    Context mainCon = MainActivity.getAppContext();
    ActivityManager activityManager = (ActivityManager)mainCon.getSystemService(Context.ACTIVITY_SERVICE);

    activityManager.getMemoryInfo(mi);


    long availableMegs = mi.availMem / 1048576L;

    //Returning the value
    return(availableMegs);

}
    ...
    }

Any Ideas on how to fix this issue? Or an alternative to getting and displaying the RAM in a widget?

EDIT : The entire error log is :

    01-11 22:21:32.426: E/AndroidRuntime(32059): FATAL EXCEPTION: main
    01-11 22:21:32.426: E/AndroidRuntime(32059): java.lang.RuntimeException: Unable to start receiver com.example.myfirstappex.AppWidget: java.lang.NullPointerException

    01-11 22:21:32.426: E/AndroidRuntime(32059):    at android.app.ActivityThread.handleReceiver(ActivityThread.java:2383)

    01-11 22:21:32.426: E/AndroidRuntime(32059):    at android.app.ActivityThread.access$1500(ActivityThread.java:141)

    01-11 22:21:32.426: E/AndroidRuntime(32059):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1310)

    01-11 22:21:32.426: E/AndroidRuntime(32059):    at android.os.Handler.dispatchMessage(Handler.java:99)

     01-11 22:21:32.426: E/AndroidRuntime(32059):   at android.os.Looper.loop(Looper.java:137)

     01-11 22:21:32.426: E/AndroidRuntime(32059):   at android.app.ActivityThread.main(ActivityThread.java:5039)

     01-11 22:21:32.426: E/AndroidRuntime(32059):   at java.lang.reflect.Method.invokeNative(Native Method)

     01-11 22:21:32.426: E/AndroidRuntime(32059):   at java.lang.reflect.Method.invoke(Method.java:511)

     01-11 22:21:32.426: E/AndroidRuntime(32059):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)

     01-11 22:21:32.426: E/AndroidRuntime(32059):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)

     01-11 22:21:32.426: E/AndroidRuntime(32059):   at dalvik.system.NativeStart.main(Native Method)

    01-11 22:21:32.426: E/AndroidRuntime(32059): Caused by: java.lang.NullPointerException

    01-11 22:21:32.426: E/AndroidRuntime(32059):    at com.example.myfirstappex.AppWidget.getRam(AppWidget.java:82)

    01-11 22:21:32.426: E/AndroidRuntime(32059):    at com.example.myfirstappex.AppWidget.onUpdate(AppWidget.java:35)

    01-11 22:21:32.426: E/AndroidRuntime(32059):    at android.appwidget.AppWidgetProvider.onReceive(AppWidgetProvider.java:66)

    01-11 22:21:32.426: E/AndroidRuntime(32059):    at android.app.ActivityThread.handleReceiver(ActivityThread.java:2376)

    01-11 22:21:32.426: E/AndroidRuntime(32059):    ... 10 more

This only happens when the main app is not running.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T09:28:20+00:00Added an answer on June 17, 2026 at 9:28 am

    According to the error logs, your issue is on line 82 of AppWidget.java. Based on a hunch, I think that’s ActivityManager activityManager = .... However, if that’s not line 82, have a look there instead.

    That aside, if I’m right, you’ll need to get the context from elsewhere. The following methods are those which are part of your AppWidget class:

    onDeleted(Context context, int[] appWidgetIds)
    onDisabled(Context context)
    onEnabled(Context context)
    onReceive(Context context, Intent intent)
    onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
    

    When you call getRam(), you should pass it a Context object from whichever method you’re currently using; in this case, that’s onUpdate(). Then, you need to modify getRam() to use that:

    public long getRam(Context context) {
        // ...
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        // ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a contact widget that displays a GridView with the contacts'
i'm trying to code an app that displays the current device position on a
I'm trying to create a widget within the module and then load that widget
I'm trying to create an icon/widget (1 cell x 1 cell) that can be
I am trying to create a simple android widget that shows The time but
I'm trying to create a very simple stand-alone app that converts CATIA .dat files
I am trying to create a custom dialog box that displays an image in
I am trying to create an app that makes use of the camera on
recently I been trying to create an android app that uses JSON Objects to
I am trying to create a simple test app that basically extends the Android

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.