Android BroadcastReceiver nullpointerexception 注册

Android BroadcastReceiver nullpointerexception on registering

我正在尝试接收下载完成的操作,但它说广播接收器在注册时为空。
但是我查了一下,显然不是。

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));
    }
}

这就是我根据要求初始化 UpdateHandler 的方式。 这发生在我家 activity.

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

    //Initialization
    init();

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

我 运行 的 android 版本是 4.2.2 以防万一。

您需要有一个 activity/Application 上下文来注册广播接收器。

正如我从屏幕截图中看到的,您正在从 utils.UpdateHandler 文件注册接收器,因此您需要将应用程序上下文传递给此方法。

所以代码将是

/**
     * 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;  
    }

然后调用 context.registerReceiver()

In your UpdateHandler class the context is not initialized yet. Because you are just extending the Activity class. Without calling the Activity's lifecycle method (eg:onCreate) the context will not be available to you. It will be null.

所以我建议您通过构造函数将上下文从 HomeActivity 传递到 UpdateHandler class 并使用该上下文注册接收者。

像这样更改您的 UpdateHandler

public class UpdateHandler{
    private DownloadManager manager;
    private Context mContext
    public UpdateHandler(Context mContext,DownloadManager dm){
        this.manager = dm;  
        this.mContext = mContext;
    }

并注册您的接收器使用这个

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

你也不想用 Activity

扩展 UpdateHandler class