xuexiangjys / XUpdate

🚀A lightweight, high availability Android version update framework.(一个轻量级、高可用性的Android版本更新框架)
https://github.com/xuexiangjys/XUpdate/wiki
Apache License 2.0
2.33k stars 406 forks source link

Attempt to invoke interface method 'void com.xuexiang.xupdate.proxy.e.h()' on a null object reference #42

Closed tanglufeng closed 5 years ago

tanglufeng commented 5 years ago

java.lang.NullPointerException: Attempt to invoke interface method 'void com.xuexiang.xupdate.proxy.e.h()' on a null object reference at com.xuexiang.xupdate.widget.UpdateDialogFragment.onClick(UpdateDialogFragment.java) at android.view.View.performClick(View.java:5642) at android.view.View$PerformClick.run(View.java:22489) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6278) at java.lang.reflect.Method.invoke(Method.java) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:793)

xuexiangjys commented 5 years ago

理论上IUpdateProxy是不可能为null的,你得把错误发生的详情仔细描述清楚

tanglufeng commented 5 years ago

你好,感谢您的回复。这是友盟统计的数据,我没有复现,但是这个崩溃数量是最高的。截图已发送到您的邮箱,请查收

xuexiangjys commented 5 years ago

@tanglufeng 貌似并没有收到你的邮件

tanglufeng commented 5 years ago

估计是到垃圾箱了,麻烦您再找找。github怎么不能传图啊

tanglufeng commented 5 years ago

app

             XUpdate.get()
            .debug(false)
            .isWifiOnly(false)     //默认设置只在wifi下检查版本更新
            .isGet(true)          //默认设置使用get请求检查版本
            .param("VersionCode", UpdateUtils.getVersionCode(this)) //设置默认公共请求参数
            .param("AppKey", getPackageName())
            .setOnUpdateFailureListener(new OnUpdateFailureListener() { //设置版本更新出错的监听
                @Override
                public void onFailure(UpdateError error) {
                    if (!MyApplication.isHome) {
                        ToastUtil.showShortToast(mContext, error.toString());
                    }

                }
            })
            .setIUpdateHttpService(new OKHttpUpdateHttpService()) //这个必须设置!实现网络请求功能。
            .init(this);   //这个必须初始化

首页

     String mUpdateUrl = Constant.WEBROOT + "api/version";
    XUpdate.newBuild(MainActivity.this)
            .updateUrl(mUpdateUrl)
            .update();

接口实现

public class OKHttpUpdateHttpService implements IUpdateHttpService { @Override public void asyncGet(@NonNull String url, @NonNull Map<String, Object> params, @NonNull final Callback callBack) { OkGo.get(url).params(transform(params)).execute(new StringCallback() { @Override public void onSuccess(Response response) { callBack.onSuccess(response.body()); }

        @Override
        public void onError(Response<String> response) {
            callBack.onError(response.getException());
        }
    });
}

@Override
public void asyncPost(@NonNull String url, @NonNull Map<String, Object> params, @NonNull final Callback callBack) {
    OkGo.<String>post(url)
            .params(transform(params))
            .execute(new StringCallback() {
                @Override
                public void onSuccess(Response<String> response) {
                    callBack.onSuccess(response.body());
                }

                @Override
                public void onError(Response<String> response) {
                    callBack.onError(response.getException());
                }
            });
}

/**
 * 下载
 *
 * @param url      下载地址
 * @param path     文件保存路径
 * @param fileName 文件名称
 * @param callback 回调
 */
@Override
public void download(@NonNull String url, @NonNull String path, @NonNull String fileName, @NonNull final DownloadCallback callback) {
    OkGo.<File>get(url).execute(new FileCallback(path, fileName) {
        @Override
        public void onSuccess(Response<File> response) {
            callback.onSuccess(response.body());
        }

        @Override
        public void onError(Response<File> response) {
            callback.onError(response.getException());
        }

        @Override
        public void onStart(Request<File, ? extends Request> request) {
            callback.onStart();
        }

        @Override
        public void downloadProgress(Progress progress) {
            callback.onProgress(progress.fraction, progress.totalSize);
        }
    });

}

@Override
public void cancelDownload(@NonNull String url) {

}

private Map<String, String> transform(Map<String, Object> params) {
    Map<String, String> map = new TreeMap<>();
    for (Map.Entry<String, Object> entry : params.entrySet()) {
        map.put(entry.getKey(), entry.getValue().toString());
    }
    return map;
}

}

xuexiangjys commented 5 years ago

我大概猜到是为什么了,可能是屏幕横竖屏切换的时候,资源被回收了

xuexiangjys commented 5 years ago

已修复: 49b9261c99a422fe7431605e8019b7f7f51be916

tanglufeng commented 5 years ago

对,刚刚已经复现了。谢谢修复。