fixed nullpointer exception

This commit is contained in:
daniel oeh 2012-07-24 17:59:04 +02:00
parent 7d99031706
commit b51aaf2a92
3 changed files with 51 additions and 19 deletions

View File

@ -43,7 +43,8 @@ public class DownloadActivity extends SherlockListActivity implements
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
if (AppConfig.DEBUG) Log.d(TAG, "Creating Activity"); if (AppConfig.DEBUG)
Log.d(TAG, "Creating Activity");
requester = DownloadRequester.getInstance(); requester = DownloadRequester.getInstance();
getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true);
@ -61,14 +62,16 @@ public class DownloadActivity extends SherlockListActivity implements
@Override @Override
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
if (AppConfig.DEBUG) Log.d(TAG, "Trying to bind service"); if (AppConfig.DEBUG)
Log.d(TAG, "Trying to bind service");
bindService(new Intent(this, DownloadService.class), mConnection, 0); bindService(new Intent(this, DownloadService.class), mConnection, 0);
} }
@Override @Override
protected void onStop() { protected void onStop() {
super.onStop(); super.onStop();
if (AppConfig.DEBUG) Log.d(TAG, "Stopping Activity"); if (AppConfig.DEBUG)
Log.d(TAG, "Stopping Activity");
} }
@Override @Override
@ -162,7 +165,8 @@ public class DownloadActivity extends SherlockListActivity implements
public void onServiceConnected(ComponentName className, IBinder service) { public void onServiceConnected(ComponentName className, IBinder service) {
downloadService = ((DownloadService.LocalBinder) service) downloadService = ((DownloadService.LocalBinder) service)
.getService(); .getService();
if (AppConfig.DEBUG) Log.d(TAG, "Connection to service established"); if (AppConfig.DEBUG)
Log.d(TAG, "Connection to service established");
dla = new DownloadlistAdapter(DownloadActivity.this, 0, dla = new DownloadlistAdapter(DownloadActivity.this, 0,
downloadService.getDownloadObserver().getStatusList()); downloadService.getDownloadObserver().getStatusList());
setListAdapter(dla); setListAdapter(dla);
@ -179,13 +183,28 @@ public class DownloadActivity extends SherlockListActivity implements
@Override @Override
public void onProgressUpdate() { public void onProgressUpdate() {
dla.notifyDataSetChanged(); runOnUiThread(new Runnable() {
@Override
public void run() {
dla.notifyDataSetChanged();
}
});
} }
@Override @Override
public void onFinish() { public void onFinish() {
if (AppConfig.DEBUG) Log.d(TAG, "Observer has finished, clearing adapter"); if (AppConfig.DEBUG)
dla.clear(); Log.d(TAG, "Observer has finished, clearing adapter");
dla.notifyDataSetInvalidated(); runOnUiThread(new Runnable() {
@Override
public void run() {
dla.clear();
dla.notifyDataSetInvalidated();
}
});
} }
} }

View File

@ -57,16 +57,20 @@ public class DownloadRequester {// TODO handle externalstorage missing
private long download(Context context, FeedFile item, File dest) { private long download(Context context, FeedFile item, File dest) {
if (!isDownloadingFile(item)) { if (!isDownloadingFile(item)) {
if (dest.exists()) { if (dest.exists()) {
if (AppConfig.DEBUG) Log.d(TAG, "File already exists. Deleting !"); if (AppConfig.DEBUG)
Log.d(TAG, "File already exists. Deleting !");
dest.delete(); dest.delete();
} }
if (AppConfig.DEBUG) Log.d(TAG, "Requesting download of url " + item.getDownload_url()); if (AppConfig.DEBUG)
Log.d(TAG,
"Requesting download of url " + item.getDownload_url());
downloads.add(item); downloads.add(item);
item.setDownload_url(URLChecker.prepareURL(item.getDownload_url())); item.setDownload_url(URLChecker.prepareURL(item.getDownload_url()));
DownloadManager.Request request = new DownloadManager.Request( DownloadManager.Request request = new DownloadManager.Request(
Uri.parse(item.getDownload_url())).setDestinationUri(Uri Uri.parse(item.getDownload_url())).setDestinationUri(Uri
.fromFile(dest)); .fromFile(dest));
if (AppConfig.DEBUG) Log.d(TAG, "Version is " + currentApi); if (AppConfig.DEBUG)
Log.d(TAG, "Version is " + currentApi);
if (currentApi >= 11) { if (currentApi >= 11) {
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN); request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN);
} else { } else {
@ -85,7 +89,8 @@ public class DownloadRequester {// TODO handle externalstorage missing
context.sendBroadcast(new Intent(ACTION_DOWNLOAD_QUEUED)); context.sendBroadcast(new Intent(ACTION_DOWNLOAD_QUEUED));
return downloadId; return downloadId;
} else { } else {
Log.e(TAG, "URL " + item.getDownload_url() + " is already being downloaded"); Log.e(TAG, "URL " + item.getDownload_url()
+ " is already being downloaded");
return 0; return 0;
} }
} }
@ -115,7 +120,8 @@ public class DownloadRequester {// TODO handle externalstorage missing
* ID of the download to cancel * ID of the download to cancel
* */ * */
public void cancelDownload(final Context context, final long id) { public void cancelDownload(final Context context, final long id) {
if (AppConfig.DEBUG) Log.d(TAG, "Cancelling download with id " + id); if (AppConfig.DEBUG)
Log.d(TAG, "Cancelling download with id " + id);
DownloadManager dm = (DownloadManager) context DownloadManager dm = (DownloadManager) context
.getSystemService(Context.DOWNLOAD_SERVICE); .getSystemService(Context.DOWNLOAD_SERVICE);
int removed = dm.remove(id); int removed = dm.remove(id);
@ -132,7 +138,8 @@ public class DownloadRequester {// TODO handle externalstorage missing
/** Cancels all running downloads */ /** Cancels all running downloads */
public void cancelAllDownloads(Context context) { public void cancelAllDownloads(Context context) {
if (AppConfig.DEBUG) Log.d(TAG, "Cancelling all running downloads"); if (AppConfig.DEBUG)
Log.d(TAG, "Cancelling all running downloads");
DownloadManager dm = (DownloadManager) context DownloadManager dm = (DownloadManager) context
.getSystemService(Context.DOWNLOAD_SERVICE); .getSystemService(Context.DOWNLOAD_SERVICE);
for (FeedFile f : downloads) { for (FeedFile f : downloads) {
@ -173,11 +180,11 @@ public class DownloadRequester {// TODO handle externalstorage missing
} }
return false; return false;
} }
public boolean hasNoDownloads() { public boolean hasNoDownloads() {
return downloads.isEmpty(); return downloads.isEmpty();
} }
public FeedFile getDownloadAt(int index) { public FeedFile getDownloadAt(int index) {
return downloads.get(index); return downloads.get(index);
} }
@ -235,9 +242,12 @@ public class DownloadRequester {// TODO handle externalstorage missing
private ServiceConnection mConnection = new ServiceConnection() { private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) { public void onServiceConnected(ComponentName className, IBinder service) {
mService = ((DownloadService.LocalBinder) service).getService(); mService = ((DownloadService.LocalBinder) service).getService();
if (AppConfig.DEBUG) Log.d(TAG, "Connection to service established"); if (AppConfig.DEBUG)
Log.d(TAG, "Connection to service established");
mService.queryDownloads(); mService.queryDownloads();
mContext.unbindService(mConnection); if (mContext != null) {
mContext.unbindService(mConnection);
}
} }
public void onServiceDisconnected(ComponentName className) { public void onServiceDisconnected(ComponentName className) {
@ -250,9 +260,9 @@ public class DownloadRequester {// TODO handle externalstorage missing
/** Notifies the DownloadService to check if there are any Downloads left */ /** Notifies the DownloadService to check if there are any Downloads left */
public void notifyDownloadService(Context context) { public void notifyDownloadService(Context context) {
mContext = context;
context.bindService(new Intent(context, DownloadService.class), context.bindService(new Intent(context, DownloadService.class),
mConnection, Context.BIND_AUTO_CREATE); mConnection, Context.BIND_AUTO_CREATE);
mContext = context;
mIsBound = true; mIsBound = true;
} }
} }

View File

@ -71,6 +71,9 @@ public class TypeGetter {
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
e.printStackTrace(); e.printStackTrace();
return null; return null;
} catch (NullPointerException e) {
e.printStackTrace();
return null;
} }
return reader; return reader;
} }