Reputation: 1179
I'm trying to attach a simple service to download a file, but on run of the service method I'm getting an error. It seems that the watcher is not getting the watcher = binder.getService();
, but not not sure why as I'm following the example I was given. The purpose of this app is to download a file off of the web using a foreground service, check for updates, and save the new file if it updates. I'm just trying to get the service to download to begin with.
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();
}
}
Manifest
<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>
Upvotes: 0
Views: 135
Reputation: 1006539
bindService()
is asynchronous. watcher
will still be null
when bindService()
returns, as the service does not even exist yet, let alone you be given your binder. Instead, move your lookForUpdates()
call to onServiceConnected()
.
Upvotes: 3