[英]Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
I'm building an app with webview. 我正在使用webview构建应用程序。 When I'm trying to reload the webview I'm getting an error. 当我尝试重新加载Webview时出现错误。
Error: 错误:
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 in.bongtech.apps.bongtechlite.MainActivity.reLoad(MainActivity.java:118)
at in.bongtech.apps.bongtechlite.HandleWebview$1.onClick(HandleWebview.java:30)
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:163)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5613)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652)
MainActivity.java MainActivity.java
public class MainActivity extends AppCompatActivity {
private WebView content;
private String url;
String appCachePath;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
appCachePath = this.getCacheDir().getAbsolutePath();
content = (WebView) findViewById(R.id.content);
content.setWebViewClient(new HandleWebview());
content.loadUrl("https://www.example.com");
WebSettings webViewSettings = content.getSettings();
webViewSettings.setJavaScriptEnabled(true);
webViewSettings.setDomStorageEnabled(true);
webViewSettings.setAppCacheEnabled(true);
webViewSettings.setAppCachePath(appCachePath);
url = content.getUrl();
}
public void reLoad(){
content.reload();
}}
HandleWebview.java HandleWebview.java
public class HandleWebview extends WebViewClient {
MainActivity activity = new MainActivity();
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().contains("www.example.com")) {
// This is my website, so do not override; let my WebView load the page
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(intent);
return true;
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
builder.setMessage(R.string.errorMessage).setTitle("Error");
builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
activity.reLoad();
}
});
builder.setNegativeButton("Dismiss", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
builder.create();
builder.show();
}}
When I'm pressing the RETRY button on the Alert Dialog I'm getting the error mentioned above. 当我按下“警报”对话框上的“重试”按钮时,出现了上面提到的错误。 I'm calling the reLoad() method of MainActivity from the alert of HandleWebview.java file. 我正在从HandleWebview.java文件的警报中调用MainActivity的reLoad()方法。 I know AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext()); 我知道AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext()); is producing this error. 正在产生此错误。 But don't know how to solve the error. 但是不知道如何解决错误。 Please help. 请帮忙。 Please tell me how to call a method inside MainActivity from a dialogue's positive response, which is inside a basic Java class. 请告诉我如何从对话的肯定响应中调用MainActivity内部的方法,该方法位于基本Java类中。
MainActivity activity = new MainActivity();
Never instantiate activities yourself. 切勿自己实例化活动。 They won't be good for anything you'd want an Activity for, such as to be used as a Context. 它们不会对您想要活动的任何内容(例如用作上下文)有用。
Instead, you can pass your MainActivity
reference to your HandleWebview
as an argument. 相反,您可以将MainActivity
引用作为参数传递给HandleWebview
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.