Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/211.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 从菜单单击重新加载时Webview崩溃_Android_Webview - Fatal编程技术网

Android 从菜单单击重新加载时Webview崩溃

Android 从菜单单击重新加载时Webview崩溃,android,webview,Android,Webview,我正在处理一个Android简单演示上的错误。我有我的internet检查例程和一个Web视图,当我单击菜单时会调用重载。这是我的代码 package com.example.test; import android.app.Activity; import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.os.Bund

我正在处理一个Android简单演示上的错误。我有我的internet检查例程和一个Web视图,当我单击菜单时会调用重载。这是我的代码

package com.example.test;

import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {
    public static final int DIALOG_NONETWORK = 0;
    WebView mywebview=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final WebView mywebview = (WebView) findViewById(R.id.webview);


        if(!Connection()){
            String data = "<html>"  +  
                    "<body><h1>NO INTERNET</h1></body>"  +  
                    "</html>";
            mywebview.loadData(data, "text/html", "UTF-8"); 
            Log.v("INTERNET","NO");

        }else{
            mywebview.loadUrl("http://www.google.com"); 
            Log.v("INTERNET","YES");

        }
        mywebview.setWebViewClient(new WebViewClient() {
            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                String data = "<html>"  +  
                        "<body><h1>NO INTERNET</h1></body>"  +  
                        "</html>";
                mywebview.loadData(data, "text/html", "UTF-8"); 

            } 
        });

    }
    public boolean Connection() {
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfoMob = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        NetworkInfo netInfoWifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if ((netInfoMob != null || netInfoWifi != null) && (netInfoMob.isConnectedOrConnecting() || netInfoWifi.isConnectedOrConnecting())) {
            return true;
        }
        return false;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) 
        {
        switch (item.getItemId()) 
            {
                case R.id.action_settings:
                    mywebview.reload();
                    Log.v("INTERNET","RELOAD");

                    break;

            }
                    return super.onOptionsItemSelected(item);
        }
}
嗯,正如我所怀疑的

final WebView mywebview = (WebView) findViewById(R.id.webview);
这就是问题所在

我把它改成了

 mywebview = (WebView) findViewById(R.id.webview);
工作起来很有魅力


无论如何,谢谢你,希望它能帮助其他人。

从LogCat发布你的stacktrace。是的,忘了这么做。现在编辑。尝试删除空值,仅Webview mywebview;
 mywebview = (WebView) findViewById(R.id.webview);