Hello I try to launch a IntentService with string as extra but it throws a null pointer exception, I search but I didnt found anything, so I ask it. Seems that the service can’t extract the extras of the intent, and I don’t know if is because I forget something, is the first time that I work with IntentService.
Well, in the main I throw the intent:
String url = MY_HOST+"/videos/video.mp4";
Intent intent = new Intent(this, VideosDownloader.class);
intent.putExtra("url", url);
startService(intent);
And in the service I manage the extra:
public class VideosDownloader extends IntentService{
public VideosDownloader() {
super("VideosDownloader");
// TODO Auto-generated constructor stub
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
protected void onHandleIntent(Intent intent) {
String url = intent.getStringExtra("url");
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setDescription("Some descrition");
request.setTitle("Some title");
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "video.mp4");
request.setVisibleInDownloadsUi(true);
// get download service and enqueue file
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
}
}
and in the Manifest I think is everything ok, I declare as service:
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:debuggable="true" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".VideosDownloader"/>
</application>
The error throw is:
12-19 08:45:50.482: E/AndroidRuntime(15470): FATAL EXCEPTION: main
12-19 08:45:50.482: E/AndroidRuntime(15470): java.lang.RuntimeException: Unable to start service com.example.downloadtest.VideosDownloader@40525e50 with Intent { cmp=com.example.downloadtest/.VideosDownloader (has extras) }: java.lang.NullPointerException
12-19 08:45:50.482: E/AndroidRuntime(15470): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2073)
12-19 08:45:50.482: E/AndroidRuntime(15470): at android.app.ActivityThread.access$2800(ActivityThread.java:121)
12-19 08:45:50.482: E/AndroidRuntime(15470): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1006)
12-19 08:45:50.482: E/AndroidRuntime(15470): at android.os.Handler.dispatchMessage(Handler.java:99)
12-19 08:45:50.482: E/AndroidRuntime(15470): at android.os.Looper.loop(Looper.java:138)
12-19 08:45:50.482: E/AndroidRuntime(15470): at android.app.ActivityThread.main(ActivityThread.java:3701)
12-19 08:45:50.482: E/AndroidRuntime(15470): at java.lang.reflect.Method.invokeNative(Native Method)
12-19 08:45:50.482: E/AndroidRuntime(15470): at java.lang.reflect.Method.invoke(Method.java:507)
12-19 08:45:50.482: E/AndroidRuntime(15470): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
12-19 08:45:50.482: E/AndroidRuntime(15470): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
12-19 08:45:50.482: E/AndroidRuntime(15470): at dalvik.system.NativeStart.main(Native Method)
12-19 08:45:50.482: E/AndroidRuntime(15470): Caused by: java.lang.NullPointerException
12-19 08:45:50.482: E/AndroidRuntime(15470): at android.app.IntentService.onStart(IntentService.java:110)
12-19 08:45:50.482: E/AndroidRuntime(15470): at android.app.IntentService.onStartCommand(IntentService.java:118)
12-19 08:45:50.482: E/AndroidRuntime(15470): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2060)
12-19 08:45:50.482: E/AndroidRuntime(15470): ... 10 more
Thanks for the reply.
SOLVED: Sorry my fault, David Wasser gave me the solution asking if I posted all code, I override Oncreate() doing nothing, and I forget that it should have super so I deleted it (because it did nothing). Thnaks for the replys!
SOLVED: Sorry my fault, David Wasser gave me the solution asking if I posted all code, I override Oncreate() doing nothing, and I forget that it should have super so I deleted it (because it did nothing). Thnaks for the replys!