gpt4 book ai didi

java - 委托(delegate)类中 getApplicationContext() 的 NullPointerException

转载 作者:行者123 更新时间:2023-12-01 17:55:00 24 4
gpt4 key购买 nike

我有第二个类,我从中调用一些方法来处理应用程序更新。其中有一个进度对话框,因此我必须将当前应用程序上下文传递给其构造函数。当直接在 MainActivity onCreate() 中调用时,代码工作正常,但当我将代码委托(delegate)给外部类时,代码会崩溃。出了什么问题?

OnCreate() 中的方法调用:

private AppUpdateHelper appUpdateHelper = new AppUpdateHelper(getApplicationContext());

@Override
protected void onCreate(Bundle savedInstanceState) {
appUpdateHelper.handleAppUpdate();
}

委托(delegate)类:

public class AppUpdateHelper {

private Context mContext;

public AppUpdateHelper(Context mContext) {
this.mContext = mContext;
}

public void handleAppUpdate() {
String versionCode = getVersionCode(); // Get app's current version code

// Is app update to date?
if (isAppCurrent(versionCode)) {
promptAppUpdate();
}
}


private String getVersionCode() {
String versionCode = null;
try {
PackageInfo pInfo = mContext.getPackageManager().getPackageInfo(mContext.getPackageName(), 0);
versionCode = pInfo.versionName;
// Log.w(mContext.getClass().getSimpleName(), "Current Version: " + versionCode);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return versionCode;
}


private boolean isAppCurrent(String versionCode) {
ParseInstallation installation = ParseInstallation.getCurrentInstallation();
String userAppVersion = installation.getString("appVersion");
return !userAppVersion.equals(versionCode);
}


private void promptAppUpdate() {
SweetAlertDialog pDialog = new SweetAlertDialog(mContext, SweetAlertDialog.WARNING_TYPE);
pDialog.setTitleText("Update Available!");
pDialog.setContentText("You must update to continue using Yeet Club!");
pDialog.setConfirmClickListener(sDialog -> {
final String appPackageName = mContext.getPackageName(); // getPackageName() from Context or Activity object
try {
mContext.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
} catch (ActivityNotFoundException anfe) {
mContext.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)));
}

sDialog.dismissWithAnimation();
});
pDialog.setCancelable(false);
pDialog.showCancelButton(false);
pDialog.show();
}

}

异常(exception):

08 - 20 15: 43: 43.874 21456 - 21456 / com.app.android E / AndroidRuntime: FATAL EXCEPTION: main
Process: com.app.android, PID: 21456
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo {
com.app.android / com.app.android.activity.MainActivity
}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()'
on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java: 2458)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java: 2613)
at android.app.ActivityThread.access$900(ActivityThread.java: 180)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java: 1473)
at android.os.Handler.dispatchMessage(Handler.java: 111)
at android.os.Looper.loop(Looper.java: 207)
at android.app.ActivityThread.main(ActivityThread.java: 5710)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java: 900)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java: 761)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()'
on a null object reference
at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java: 117)
at com.app.android.activity.MainActivity. < init > (MainActivity.java: 77)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java: 1072)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java: 2448)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java: 2613) 
at android.app.ActivityThread.access$900(ActivityThread.java: 180) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java: 1473) 
at android.os.Handler.dispatchMessage(Handler.java: 111) 
at android.os.Looper.loop(Looper.java: 207) 
at android.app.ActivityThread.main(ActivityThread.java: 5710) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java: 900) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java: 761) 

最佳答案

这很简单。

调用OnCreate后可以获得上下文实例

private AppUpdateHelper appUpdateHelper;

@Override
protected void onCreate(Bundle savedInstanceState) {
appUpdateHelper = new AppUpdateHelper(getApplicationContext());
appUpdateHelper.handleAppUpdate();
}

关于java - 委托(delegate)类中 getApplicationContext() 的 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45782873/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com