0
我試圖附加一個簡單的服務來下載文件,但運行的服務方法出現錯誤。看起來,這個觀察者沒有得到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>
感謝您解決問題! –