Java StartService中的NullPointerException

Java StartService中的NullPointerException,java,android,service,nullpointerexception,Java,Android,Service,Nullpointerexception,我在尝试启动启动通知和下载的服务时遇到空异常 从Main: Intent ServiceIntent = new Intent(DownloadServiceActivity.this,DownloadService.class); ServiceIntent.putExtra("DownloadService_URL", "http://dl.dropbox.com/u/18331007/Quran1.apk"); ServiceIntent.putExtra("DownloadService

我在尝试启动启动通知和下载的服务时遇到空异常

从Main:

Intent ServiceIntent = new Intent(DownloadServiceActivity.this,DownloadService.class);
ServiceIntent.putExtra("DownloadService_URL", "http://dl.dropbox.com/u/18331007/Quran1.apk");
ServiceIntent.putExtra("DownloadService_FILENAME", "/sdcard/test/Test1.rar");
ServiceIntent.putExtra("DownloadService_PATH", "/sdcard/test/");
startService(ServiceIntent);
服务:

public class DownloadService extends IntentService{
    ProgressBar progressBar;
    private int progress = 10;
    NotificationManager notificationManager;
    PendingIntent pendingIntent;
    Notification notification;

    public DownloadService() {
        super("DownloadService");
    }

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub

        Intent intent = new Intent(this, DownloadServiceActivity.class);
        pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);

        notification = new Notification(R.drawable.icon, "simulating a download", System
                                        .currentTimeMillis());
        notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT;
        notification.contentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.downloadservice);
        notification.contentIntent = pendingIntent;
        notification.contentView.setImageViewResource(R.id.status_icon, R.drawable.icon);
        notification.contentView.setTextViewText(R.id.status_text, "simulation in progress");
        notification.contentView.setProgressBar(R.id.status_progress, 100, progress, false);

        notificationManager = (NotificationManager) getApplicationContext().getSystemService(
            getApplicationContext().NOTIFICATION_SERVICE);

        notificationManager.notify(42, notification);
    };

    @Override
    protected void onHandleIntent(Intent intent) {
        String URL=intent.getStringExtra("DownloadService_URL");
        String FileName=intent.getStringExtra("DownloadService_FILENAME");
        String Path=intent.getStringExtra("DownloadService_PATH");

        try{
            URL url = new URL(URL);
            URLConnection conexion = url.openConnection();
            conexion.connect();
            int lenghtOfFile = conexion.getContentLength();

            InputStream input = new BufferedInputStream(url.openStream());
            OutputStream output = new FileOutputStream(Path+FileName);

            byte data[] = new byte[1024];
            long total = 0;

            int count = 0;
            while ((count = input.read(data)) != -1) {
                total += count;
                notification.contentView.setProgressBar(R.id.status_progress, 100, (int)((total*100)/lenghtOfFile), false);
                notificationManager.notify(42, notification);
                output.write(data);
            }

            output.flush();
            output.close();
            input.close();
        }
        catch(Exception e){ }
    }
}
以下是logcat输出:

09-06 19:50:06.601: ERROR/AndroidRuntime(8787): FATAL EXCEPTION: main
09-06 19:50:06.601: ERROR/AndroidRuntime(8787): java.lang.RuntimeException: Unable to start service omar.downloadservice.DownloadService@4016a7c8 with Intent { cmp=omar.downloadservice/.DownloadService }: java.lang.NullPointerException
09-06 19:50:06.601: ERROR/AndroidRuntime(8787):     at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2056)
09-06 19:50:06.601: ERROR/AndroidRuntime(8787):     at android.app.ActivityThread.access$2800(ActivityThread.java:117)
09-06 19:50:06.601: ERROR/AndroidRuntime(8787):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:998)
09-06 19:50:06.601: ERROR/AndroidRuntime(8787):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-06 19:50:06.601: ERROR/AndroidRuntime(8787):     at android.os.Looper.loop(Looper.java:130)
09-06 19:50:06.601: ERROR/AndroidRuntime(8787):     at android.app.ActivityThread.main(ActivityThread.java:3687)
09-06 19:50:06.601: ERROR/AndroidRuntime(8787):     at java.lang.reflect.Method.invokeNative(Native Method)
09-06 19:50:06.601: ERROR/AndroidRuntime(8787):     at java.lang.reflect.Method.invoke(Method.java:507)
09-06 19:50:06.601: ERROR/AndroidRuntime(8787):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
09-06 19:50:06.601: ERROR/AndroidRuntime(8787):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
09-06 19:50:06.601: ERROR/AndroidRuntime(8787):     at dalvik.system.NativeStart.main(Native Method)
09-06 19:50:06.601: ERROR/AndroidRuntime(8787): Caused by: java.lang.NullPointerException
09-06 19:50:06.601: ERROR/AndroidRuntime(8787):     at android.app.IntentService.onStart(IntentService.java:110)
09-06 19:50:06.601: ERROR/AndroidRuntime(8787):     at android.app.IntentService.onStartCommand(IntentService.java:118)
09-06 19:50:06.601: ERROR/AndroidRuntime(8787):     at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2043)
09-06 19:50:06.601: ERROR/AndroidRuntime(8787):     ... 10 more

尝试更改此行:

Intent ServiceIntent = new Intent(DownloadServiceActivity.this,DownloadService.class);
致:


你是否将该服务添加到android清单中了?修复你的问题,部分内容不在代码中是的,我是这样做的:你的应用程序包名是:omar.downloadservice吗?是的。。我调试了应用程序,并在服务的OnCreate中设置了一个断点,然后它就到了。。它读取所有OnCreate,但随后崩溃..您有onStartCommand方法吗?public int-onStart命令intent-intent,int-flags,int-startId尝试将onCreate逻辑移到此处我将代码放入onStart中,它现在可以工作了!谢谢你的时间。如果你能解释一下这个问题以及你建议背后的原因,那就太好了。
Intent ServiceIntent = new Intent();
i.setClassName( "omar.downloadservice","omar.downloadservice.DownloadService" );