NullPointerException in StartService
I'm having a null exception when trying to start a service that starts a notification and a download.
From 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);
The service:
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){ }
}
}
Here's the logcat output:
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: ERR开发者_开发百科OR/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
Try changing this line:
Intent ServiceIntent = new Intent(DownloadServiceActivity.this,DownloadService.class);
to:
Intent ServiceIntent = new Intent();
i.setClassName( "omar.downloadservice","omar.downloadservice.DownloadService" );
精彩评论