Search code examples
javaandroidbroadcastreceiverandroid-launcherandroid-download-manager

Android BroadcastReceiver nullpointerexception on registering


I'm Trying to receive a download completed action but it says the broadcast receiver is null on registering.
But I checked it and it clearly isn't.

The LogCat Error

public class UpdateHandler extends Activity{

    private DownloadManager manager;

    /**
     * Class Constructor will only be used for non-static Referencing in a static condition.
     */
    public UpdateHandler(DownloadManager dm){
        this.manager = dm;  
    }


    public void updateApp(final String appname) {

        String apkurl = apk_url + appname;
        DownloadManager.Request request = new DownloadManager.Request(
                Uri.parse(apkurl));
        request.setDescription("Even geduld aub...");
        request.setTitle("Bimii-app wordt gedownload");
        request.setDestinationInExternalPublicDir(
                Environment.DIRECTORY_DOWNLOADS, appname);

        File folder1 = android.os.Environment
                .getExternalStoragePublicDirectory(android.os.Environment.DIRECTORY_DOWNLOADS
                        + "/" + appname);
        if (folder1.exists()) {
            Log.d("!--->", Environment.DIRECTORY_DOWNLOADS + "/" + appname
                    + " pre-existant, delete!");
            folder1.delete();
        } else {
            Log.d("!--->", Environment.DIRECTORY_DOWNLOADS + "/" + appname
                    + " is new.");
        }

        registerBroadcastReceiver(request, appname);
    }


    /**
     * Installing a specific Application by name.
     * @param appname
     */
    private void installUpdates(String appname) {
        Log.d("installing ", appname);
        File filepath = android.os.Environment
                .getExternalStoragePublicDirectory(android.os.Environment.DIRECTORY_DOWNLOADS
                        + "/" + appname);

        Intent install_intent = new Intent(Intent.ACTION_VIEW);
        install_intent.setDataAndType(Uri.fromFile(filepath),
                "application/vnd.android.package-archive");
        install_intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);     
        startActivity(install_intent);
    }


    private void registerBroadcastReceiver(DownloadManager.Request request, final String appname){
        // get download service and enqueue file
        manager.enqueue(request);

        BroadcastReceiver downloadDone = new BroadcastReceiver() {
            public void onReceive(Context ctxt, Intent intent) {
                Log.d("!--->", "File Download Completed + appname");
                installUpdates(appname);
            }
        };

        registerReceiver(downloadDone, new IntentFilter(
                DownloadManager.ACTION_DOWNLOAD_COMPLETE));
    }
}

And this is how I initialize my UpdateHandler as requested. This happens in my home activity.

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //Initialization
    init();

    loadApps();
    update_handler = new UpdateHandler((DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE));
    setupButtons();
    loadListView();
}

The android version I'm running is 4.2.2 in case that matters.


Solution

  • You need to have an activity/Application context for registering the broadcast receiver.

    As I can see from screenshot, you are registering receiver from utils.UpdateHandler file so you need to pass application context to this method.

    So code will be

    /**
         * Class Constructor will only be used for non-static Referencing in a static condition.
         */
        public UpdateHandler(Context context, DownloadManager dm){
            this.mContext = context;
            this.manager = dm;  
        }
    

    and then call context.registerReceiver()