在Android Studio 3.1.1上定期刷新WebView的源代码不起作用

时间:2018-04-17 20:49:59

标签: android android-webview

我从StackOverflow老问答中得到了这个答案。但是当我把它放在Android Studio上时,它会出现错误。怎么解决?请看一下这个截图和这段代码:

enter image description here

public class MainActivity extends AppCompatActivity {
    public WebView webView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        webView= (WebView) findViewById(R.id.webView);
        WebSettings webSettings= webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webView.loadUrl("https://obs6.com/");
        webView.setWebViewClient(new WebViewClient());
        Toast.makeText(LogThirdPager.this, "Hello", Toast.LENGTH_SHORT).show();

        reload();


    }

    public void reload() {
        final Handler handler = new Handler() {
            @Override
            public void publish(LogRecord record) {

            }

            @Override
            public void flush() {

            }

            @Override
            public void close() throws SecurityException {

            }
        };
        webView.postDelayed(new Runnable() {
            Toast.makeText(LogThirdPager.this, "Hello", Toast.LENGTH_SHORT).show();
            reload();
        },5000);
    }
    }

1 个答案:

答案 0 :(得分:0)

LogThirdPager.this两行更改为MainActivity.this。无法从LogThirdPager.this引用MainActivity