2

您好,我尝试使用额外的字符串启动 IntentService,但它会引发空指针异常,我搜索但我没有找到任何东西,所以我问它。似乎该服务无法提取意图的额外内容,我不知道是不是因为我忘记了什么,是我第一次使用 IntentService。

好吧,主要是我抛出的意图:

    String url = MY_HOST+"/videos/video.mp4";
    Intent intent = new Intent(this, VideosDownloader.class);
    intent.putExtra("url", url);
    startService(intent);

在服务中,我管理额外的:

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);

 }
}

在清单中,我认为一切正常,我声明为服务:

<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>

错误抛出是:

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

谢谢回复。

已解决:对不起,我的错,David Wasser 给了我解决方案,询问我是否发布了所有代码,我重写 Oncreate() 什么都不做,我忘记它应该有 super 所以我删除了它(因为它什么也没做)。感谢您的回复!

4

2 回答 2

0

尝试删除该方法onBind

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

因为IntentService有它的实现。

于 2012-12-19T08:27:01.433 回答
0

已解决:对不起,我的错,David Wasser 给了我解决方案,询问我是否发布了所有代码,我重写 Oncreate() 什么都不做,我忘记它应该有 super 所以我删除了它(因为它什么也没做)。感谢您的回复!

于 2012-12-20T09:28:45.393 回答