0

在我的 android 应用程序中检查服务是否运行,如果运行我想更改 UI 颜色。我使用以下代码

    if(true==util.isMyServiceRunning(getApplicationContext())){

    makeUIChangesforTurnOn(true);
    }

但它显示错误..

09-09 00:53:26.810: E/AndroidRuntime(1106): FATAL EXCEPTION: main 09-09 00:53:26.810: 
E/AndroidRuntime(1106): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.param.calltracker/com.param.calltracker.MainActivity}:java.lang.NullPointerException
09-09 00:53:26.810: E/AndroidRuntime(1106):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
09-09 00:53:26.810: E/AndroidRuntime(1106):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
09-09 00:53:26.810: E/AndroidRuntime(1106):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
09-09 00:53:26.810: E/AndroidRuntime(1106):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
09-09 00:53:26.810: E/AndroidRuntime(1106):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-09 00:53:26.810: E/AndroidRuntime(1106):     at android.os.Looper.loop(Looper.java:130)
09-09 00:53:26.810: E/AndroidRuntime(1106):     at android.app.ActivityThread.main(ActivityThread.java:3687)
09-09 00:53:26.810: E/AndroidRuntime(1106):     at java.lang.reflect.Method.invokeNative(Native Method)
09-09 00:53:26.810: E/AndroidRuntime(1106):     at java.lang.reflect.Method.invoke(Method.java:507)
09-09 00:53:26.810: E/AndroidRuntime(1106):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
09-09 00:53:26.810: E/AndroidRuntime(1106):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
09-09 00:53:26.810: E/AndroidRuntime(1106):     at dalvik.system.NativeStart.main(Native Method)
09-09 00:53:26.810: E/AndroidRuntime(1106): Caused by: java.lang.NullPointerException
09-09 00:53:26.810: E/AndroidRuntime(1106):     at com.param.calltracker.MainActivity.makeUIChangesforTurnOn(MainActivity.java:214)
09-09 00:53:26.810: E/AndroidRuntime(1106):     at com.param.calltracker.MainActivity.onCreate(MainActivity.java:79)
09-09 00:53:26.810: E/AndroidRuntime(1106):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-09 00:53:26.810: E/AndroidRuntime(1106):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)

我的 UI 更改代码>

   public void makeUIChangesforTurnOn(Boolean on){
    if(on){
        trun_on_off_img.setImageResource(R.drawable.green_signal);
        buttonToggleDetect.setBackgroundResource(R.drawable.orenge_button);
        buttonToggleDetect.setText("Turn off");
        textViewDetectState.setText("Monitoring Calls");
                textViewDetectState.setTextColor(getResources().getColor(R.color.green));


    }else{
        trun_on_off_img.setImageResource(R.drawable.yellow_signal);
        buttonToggleDetect.setBackgroundResource(R.drawable.green_button);
        buttonToggleDetect.setText("Turn on");
        textViewDetectState.setText("Not Monitoring Calls");
        textViewDetectState.setTextColor(getResources().getColor(R.color.orenge));

    }
}

任何人帮助我...

4

2 回答 2

1

遵循此代码:

private boolean isMyServiceRunning() {
        ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        for (RunningServiceInfo service : manager
                .getRunningServices(Integer.MAX_VALUE)) {
            if (YourService.class.getName().equals(
                    service.service.getClassName())) {
                return true;
            }
        }
        return false;
    }

在您的活动中调用此方法,如下所示:

      makeChangesInUI(isMyServiceRunning())
于 2013-09-08T19:28:34.070 回答
0

您在上面显示的代码可能工作正常,但在您的 makeUIChangesforTurnOn() 方法中有一个 NullPointerException。从堆栈跟踪中我可以看到它位于 MainActivity.java 第 214 行,因此您必须查看那里。

于 2013-09-08T19:59:55.880 回答