在启动android时无法启动我的服务

时间:2014-04-11 20:34:52

标签: java android android-intent broadcastreceiver logcat

在我的应用程序中我希望在启动完成时启动服务,并且我以编程方式控制此选项,服务在启动时启动但强制关闭并重新启动它我不知道为什么

这是我的logcat

04-11 22:26:04.999: E/AndroidRuntime(2256): FATAL EXCEPTION: main
04-11 22:26:04.999: E/AndroidRuntime(2256): java.lang.NullPointerException
04-11 22:26:04.999: E/AndroidRuntime(2256):     at technologysociety.socialsaddict.ServiceSocial$6.handleMessage(ServiceSocial.java:156)
04-11 22:26:04.999: E/AndroidRuntime(2256):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-11 22:26:04.999: E/AndroidRuntime(2256):     at android.os.Looper.loop(Looper.java:130)
04-11 22:26:04.999: E/AndroidRuntime(2256):     at android.app.ActivityThread.main(ActivityThread.java:3835)
04-11 22:26:04.999: E/AndroidRuntime(2256):     at java.lang.reflect.Method.invokeNative(Native Method)
04-11 22:26:04.999: E/AndroidRuntime(2256):     at java.lang.reflect.Method.invoke(Method.java:507)
04-11 22:26:04.999: E/AndroidRuntime(2256):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
04-11 22:26:04.999: E/AndroidRuntime(2256):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
04-11 22:26:04.999: E/AndroidRuntime(2256):     at dalvik.system.NativeStart.main(Native Method)

我正在使用复选框控制此选项

StartUP.setOnClickListener(new OnClickListener() {


        //Start of on boot
      @Override
      public void onClick(View v) {
                //is StartUP checked?
        if (StartUP.isChecked()) {

             PackageManager pm  = MainActivity.this.getPackageManager();
                ComponentName componentName = new ComponentName(MainActivity.this, StartUP.class);
                pm.setComponentEnabledSetting(componentName,PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                                PackageManager.DONT_KILL_APP);
                Toast.makeText(getApplicationContext(), "activated", Toast.LENGTH_SHORT).show();


        }else{

              PackageManager pm  = MainActivity.this.getPackageManager();
                ComponentName componentName = new ComponentName(MainActivity.this, StartUP.class);
                pm.setComponentEnabledSetting(componentName,PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                                PackageManager.DONT_KILL_APP);
                Toast.makeText(getApplicationContext(), "Deactivated", Toast.LENGTH_SHORT).show();
        }

      }
    });

和广播复兴课

  public class StartUP extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
           // This method is called when the BroadcastReceiver is receiving

           // Start Service On Boot Start Up
           Intent service = new Intent(context, ServiceSocial.class);
           context.startService(service);



    }
}

我的清单我也有:

<receiver android:name="StartUP" android:enabled="false">
        <intent-filter >                
            <action android:name="android.intent.action.BOOT_COMPLETED"/>                
        </intent-filter>
    </receiver>

1 个答案:

答案 0 :(得分:0)

你需要在ServiceSocial.java的第156行找出什么是null,并且要么找出它为什么它是null,如果它不应该是null,或者适应它为null的情况,如果null在那里是可接受的。

然后阅读:How to read and understand the java stack trace?