From b51aaf2a921bbb6446f55067d706d72087d466d2 Mon Sep 17 00:00:00 2001 From: daniel oeh Date: Tue, 24 Jul 2012 17:59:04 +0200 Subject: [PATCH] fixed nullpointer exception --- .../antennapod/activity/DownloadActivity.java | 35 ++++++++++++++----- .../antennapod/storage/DownloadRequester.java | 32 +++++++++++------ .../syndication/handler/TypeGetter.java | 3 ++ 3 files changed, 51 insertions(+), 19 deletions(-) diff --git a/src/de/danoeh/antennapod/activity/DownloadActivity.java b/src/de/danoeh/antennapod/activity/DownloadActivity.java index 8dc029e75..5ece16a63 100644 --- a/src/de/danoeh/antennapod/activity/DownloadActivity.java +++ b/src/de/danoeh/antennapod/activity/DownloadActivity.java @@ -43,7 +43,8 @@ public class DownloadActivity extends SherlockListActivity implements @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - if (AppConfig.DEBUG) Log.d(TAG, "Creating Activity"); + if (AppConfig.DEBUG) + Log.d(TAG, "Creating Activity"); requester = DownloadRequester.getInstance(); getSupportActionBar().setDisplayHomeAsUpEnabled(true); @@ -61,14 +62,16 @@ public class DownloadActivity extends SherlockListActivity implements @Override protected void 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); } @Override protected void onStop() { super.onStop(); - if (AppConfig.DEBUG) Log.d(TAG, "Stopping Activity"); + if (AppConfig.DEBUG) + Log.d(TAG, "Stopping Activity"); } @Override @@ -162,7 +165,8 @@ public class DownloadActivity extends SherlockListActivity implements public void onServiceConnected(ComponentName className, IBinder service) { downloadService = ((DownloadService.LocalBinder) service) .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, downloadService.getDownloadObserver().getStatusList()); setListAdapter(dla); @@ -179,13 +183,28 @@ public class DownloadActivity extends SherlockListActivity implements @Override public void onProgressUpdate() { - dla.notifyDataSetChanged(); + runOnUiThread(new Runnable() { + + @Override + public void run() { + dla.notifyDataSetChanged(); + + } + }); } @Override public void onFinish() { - if (AppConfig.DEBUG) Log.d(TAG, "Observer has finished, clearing adapter"); - dla.clear(); - dla.notifyDataSetInvalidated(); + if (AppConfig.DEBUG) + Log.d(TAG, "Observer has finished, clearing adapter"); + runOnUiThread(new Runnable() { + + @Override + public void run() { + dla.clear(); + dla.notifyDataSetInvalidated(); + } + }); + } } diff --git a/src/de/danoeh/antennapod/storage/DownloadRequester.java b/src/de/danoeh/antennapod/storage/DownloadRequester.java index 356201a2d..90c481ebb 100644 --- a/src/de/danoeh/antennapod/storage/DownloadRequester.java +++ b/src/de/danoeh/antennapod/storage/DownloadRequester.java @@ -57,16 +57,20 @@ public class DownloadRequester {// TODO handle externalstorage missing private long download(Context context, FeedFile item, File dest) { if (!isDownloadingFile(item)) { 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(); } - 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); item.setDownload_url(URLChecker.prepareURL(item.getDownload_url())); DownloadManager.Request request = new DownloadManager.Request( Uri.parse(item.getDownload_url())).setDestinationUri(Uri .fromFile(dest)); - if (AppConfig.DEBUG) Log.d(TAG, "Version is " + currentApi); + if (AppConfig.DEBUG) + Log.d(TAG, "Version is " + currentApi); if (currentApi >= 11) { request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN); } else { @@ -85,7 +89,8 @@ public class DownloadRequester {// TODO handle externalstorage missing context.sendBroadcast(new Intent(ACTION_DOWNLOAD_QUEUED)); return downloadId; } 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; } } @@ -115,7 +120,8 @@ public class DownloadRequester {// TODO handle externalstorage missing * ID of the download to cancel * */ 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 .getSystemService(Context.DOWNLOAD_SERVICE); int removed = dm.remove(id); @@ -132,7 +138,8 @@ public class DownloadRequester {// TODO handle externalstorage missing /** Cancels all running downloads */ 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 .getSystemService(Context.DOWNLOAD_SERVICE); for (FeedFile f : downloads) { @@ -173,11 +180,11 @@ public class DownloadRequester {// TODO handle externalstorage missing } return false; } - + public boolean hasNoDownloads() { return downloads.isEmpty(); } - + public FeedFile getDownloadAt(int index) { return downloads.get(index); } @@ -235,9 +242,12 @@ public class DownloadRequester {// TODO handle externalstorage missing private ServiceConnection mConnection = new ServiceConnection() { public void onServiceConnected(ComponentName className, IBinder service) { 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(); - mContext.unbindService(mConnection); + if (mContext != null) { + mContext.unbindService(mConnection); + } } 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 */ public void notifyDownloadService(Context context) { + mContext = context; context.bindService(new Intent(context, DownloadService.class), mConnection, Context.BIND_AUTO_CREATE); - mContext = context; mIsBound = true; } } diff --git a/src/de/danoeh/antennapod/syndication/handler/TypeGetter.java b/src/de/danoeh/antennapod/syndication/handler/TypeGetter.java index 15adb5f19..ffa6a9034 100644 --- a/src/de/danoeh/antennapod/syndication/handler/TypeGetter.java +++ b/src/de/danoeh/antennapod/syndication/handler/TypeGetter.java @@ -71,6 +71,9 @@ public class TypeGetter { } catch (FileNotFoundException e) { e.printStackTrace(); return null; + } catch (NullPointerException e) { + e.printStackTrace(); + return null; } return reader; }