使用webview加载网站只会出错

时间:2017-01-13 15:56:27

标签: android android-webview

我在Java文件中有这个代码,名为Principal:

package com.vistaweb.vistaweb;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class Principal extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_principal);

        WebView myWebView = (WebView) findViewById(R.id.webView);
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        myWebView.setWebViewClient(new WebViewClient());
        myWebView.loadUrl("http://mywebsite.com");
    }

}

我的布局被称为: activity_principal.xml并正确引用

但是所有时间都给我错误我认为没关系,但是当运行模拟器没有显示任何内容时会给我错误,例如无法找到符号webview

这是什么问题? ,谢谢你的帮助,Regard

我看到了这些错误:

C:\Users\pol\Desktop\VistaWeb\app\src\main\java\com\vistaweb\vistaweb\Principal.java:13: error: cannot find symbol
        WebView myWebView = (WebView) findViewById(R.id.webView);
        ^
  symbol:   class WebView
  location: class Principal
C:\Users\pol\Desktop\VistaWeb\app\src\main\java\com\vistaweb\vistaweb\Principal.java:13: error: cannot find symbol
        WebView myWebView = (WebView) findViewById(R.id.webView);
                             ^
  symbol:   class WebView
  location: class Principal
C:\Users\pol\Desktop\VistaWeb\app\src\main\java\com\vistaweb\vistaweb\Principal.java:13: error: cannot find symbol
        WebView myWebView = (WebView) findViewById(R.id.webView);
                                                       ^
  symbol:   variable webView
  location: class id
C:\Users\pol\Desktop\VistaWeb\app\src\main\java\com\vistaweb\vistaweb\Principal.java:14: error: cannot find symbol
        WebSettings webSettings = myWebView.getSettings();
        ^
  symbol:   class WebSettings
  location: class Principal
C:\Users\pol\Desktop\VistaWeb\app\src\main\java\com\vistaweb\vistaweb\Principal.java:16: error: cannot find symbol
        myWebView.setWebViewClient(new WebViewClient());
                                       ^
  symbol:   class WebViewClient
  location: class Principal
5 errors

:app:compileDebugJavaWithJavac FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 1.423 secs

1 个答案:

答案 0 :(得分:1)

添加所需的导入

import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebSettings;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

或者替代android studio在输入时显示选项,所以如果你通过按Enter选择一个选项,则会自动为你添加导入

或 您可以使用ctrl+space,使用导入选项自动添加导入