Android Widget:显示RAM

时间:2013-01-12 02:49:41

标签: android android-widget

我正在尝试为我的应用创建一个小部件,显示设备的CPU使用率,电池电量和剩余RAM;不是存储空间。

CPU级别和电池充电代码工作正常;但RAM代码没有。我已经读过,为了在AppWidgetProvider中使用.getSystemService(),你必须将一个上下文从一个活动传递到这里。

问题是,当方法尝试使用ActivityManager设置systemService时,它会抛出:

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

我已将问题缩小到这段代码:

    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);

}
    ...
    }

有关如何解决此问题的任何想法?或者在窗口小部件中获取和显示RAM的替代方法?

编辑:整个错误日志是:

    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

仅当主应用未运行时才会发生这种情况。

1 个答案:

答案 0 :(得分:0)

根据错误日志,您的问题出现在AppWidget.java的第82行。基于预感,我认为是ActivityManager activityManager = ...。但是,如果那不是第82行,请改为查看。

除此之外,如果我是对的,你需要从其他地方获取背景。以下方法是AppWidget类的一部分:

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

当您致电getRam()时,您应该从您当前使用的任何方法传递Context对象;在这种情况下,那是onUpdate()。然后,您需要修改getRam()以使用它:

public long getRam(Context context) {
    // ...
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    // ...
}