有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java试图调用虚拟方法“安卓”。所容纳之物上下文安卓。所容纳之物上下文空对象引用上的getApplicationContext()

我正在使用webview构建一个应用程序。当我试图重新加载webview时,我遇到了一个错误

错误:

java.lang.NullPointerException: Attempt to invoke virtual method '安卓.content.Context 安卓.content.Context.getApplicationContext()' on a null object reference
    at 安卓.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.安卓.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:163)
    at 安卓.os.Handler.dispatchMessage(Handler.java:102)
    at 安卓.os.Looper.loop(Looper.java:157)
    at 安卓.app.ActivityThread.main(ActivityThread.java:5613)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774)
    at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:652)

主要活动。爪哇

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。爪哇

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

当我按下警报对话框上的重试按钮时,我得到了上面提到的错误。我正在从HandleWebview的警报调用MainActivity的reLoad()方法。java文件。我知道这个对话。生成器=新建警报对话框。生成器(view.getContext());正在产生此错误。但不知道如何解决这个错误。请帮忙。请告诉我如何从对话的肯定响应调用MainActivity中的方法,该响应位于基本Java类中


共 (1) 个答案

  1. # 1 楼答案

    MainActivity activity = new MainActivity();
    

    永远不要自己实例化活动。它们对任何你想要的活动都没有好处,比如用作上下文

    相反,您可以将MainActivity引用作为参数传递给HandleWebview