我正在尝试附加一个简单的服务来下载文件,但是在运行服务方法时我遇到了错误。似乎观察者没有得到watcher = binder.getService();
,但不知道为什么我跟随我给出的例子。此应用程序的目的是使用前台服务从Web下载文件,检查更新,并在更新时保存新文件。我只是想开始下载服务。
04-12 13:19:07.023 2801-2801/com.peterchappy.filewatcher E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.peterchappy.filewatcher, PID: 2801
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.peterchappy.filewatcher/com.peterchappy.filewatcher.activities.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.peterchappy.filewatcher.services.WatcherService.lookForUpdates(java.lang.String)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.peterchappy.filewatcher.services.WatcherService.lookForUpdates(java.lang.String)' on a null object reference
at com.peterchappy.filewatcher.activities.MainActivity.onCreate(MainActivity.java:33)
at android.app.Activity.performCreate(Activity.java:5937)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
MainActivity.java
package com.peterchappy.filewatcher.activities;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.Bundle;
import com.peterchappy.filewatcher.R;
import com.peterchappy.filewatcher.services.WatcherService;
public class MainActivity extends Activity {
String dataURL;
boolean connected;
WatcherService watcher;
@Override
public void onStart(){
super.onStart();
Intent serviceIntent = new Intent(this, WatcherService.class);
bindService(serviceIntent, myConnection, Context.BIND_AUTO_CREATE);
watcher.lookForUpdates("http://www.reddit.com");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void onStop(){
super.onStop();
unbindService(myConnection);
}
ServiceConnection myConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
WatcherService.TestBinder binder = (WatcherService.TestBinder) service;
watcher = binder.getService();
connected = true;
}
@Override
public void onServiceDisconnected(ComponentName name) {
connected = false;
}
};
}
WatcherService.java
package com.peterchappy.filewatcher.services;
import android.content.Intent;
import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import com.peterchappy.filewatcher.activities.MainActivity;
public class WatcherService extends Service {
IBinder myBinder = new TestBinder();
public WatcherService() {
}
@Override
public IBinder onBind(Intent intent) {
Notification.Builder n;
Intent i = new Intent(this, MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.setAction("SOME_ACTION");
PendingIntent pIntent = PendingIntent.getActivity(this, 0, i, 0);
n = new Notification.Builder(this)
.setContentTitle("FileWatcher")
.setContentText("Your FileWatcher is running")
.setContentIntent(pIntent)
.setAutoCancel(false);
startForeground(1, n.build());
return myBinder;
}
public class TestBinder extends Binder {
public WatcherService getService(){
return WatcherService.this;
}
}
public void lookForUpdates(final String urlToDownload){
Thread t = new Thread () {
@Override
public void run (){
try {
URL url = new URL(urlToDownload);
URLConnection connection = url.openConnection();
connection.connect();
InputStream input = new BufferedInputStream(connection.getInputStream());
OutputStream output = new FileOutputStream("/" + urlToDownload + "_temp");
byte data[] = new byte[1024];
int count;
while ((count = input.read(data)) != -1) {
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
Log.d("Download", "Complete");
} catch (IOException e) {
Log.d("Download", e.getMessage().toString());
}
}
};
t.start();
}
}
清单
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".activities.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<service android:name=".WatcherService"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>
答案 0 :(得分:3)
bindService()
是异步的。当watcher
返回时,null
仍为bindService()
,因为该服务尚未存在,更不用说给你的活页夹了。相反,请将lookForUpdates()
来电转移至onServiceConnected()
。