2011-09-06 75 views
0

嘗試啓動啓動通知和下載的服務時,發生空異常。StartService中的NullPointerException

從主營:

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 [email protected] 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 
+0

您是否將服務添加到了Android清單? – Joru

+0

解決你的問題,它的一部分不在代碼 –

+0

是的,我喜歡這個: Omar

回答

0

嘗試改變這一行:

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

發送至:

Intent ServiceIntent = new Intent(); 
i.setClassName("omar.downloadservice","omar.downloadservice.DownloadService"); 
+0

我得到相同的logcat :( – Omar

+0

是應用程序的軟件包名稱:「omar.downloadservice」? – IncrediApp

+0

是的..我調試應用程序,我在服務的OnCreate中放置了一個斷點,並且它到達那裏..它讀取所有的OnCreate,但之後崩潰。 – Omar