尝试对空对象引用调用虚拟方法android.content.Context android.content.Context.getApplicationContext()'

问题描述 投票:0回答:1

我建立与web视图的应用程序。当我试图重新加载网页视图我得到一个错误。

错误:

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)

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

handle Web view.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();
}}

当我按下的警告对话框中的重试按钮,我得到上述错误。我打电话从HandleWebview.java文件的警报MainActivity的RELOAD()方法。我知道AlertDialog.Builder建设者=新AlertDialog.Builder(view.getContext());正在产生该错误。但不知道如何解决这个错误。请帮忙。请告诉我如何从一个对话的积极响应,这是一个基本的Java类中调用内部MainActivity的方法。

java android android-webview
1个回答
1
投票
MainActivity activity = new MainActivity();

千万不要自己实例化的活动。他们将不利于任何你想要一个活动,如被用作上下文。

相反,你可以通过你的MainActivity参考您的HandleWebview作为参数。

推荐问答
热门问答
最新问答