Android Studio 3.0.1中的Android WebView LoadURL错误

时间:2018-06-25 15:41:50

标签: java android android-studio webview android-studio-3.0

我正在制作一个用于显示网页(仅此而已)的基本测试应用程序。

但是在MainActivity.java中,loadURL出现“无法解析符号”错误,而Atl + Enter则不执行任何操作。

WebView myWebView = (WebView) findViewById(R.id.webView);
        myWebView.loadUrl("WEBSITE");

在MainActivity.xml中:

<WebView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webView"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" />

这就是我输入的代码全部

errors

肯定是这些日志

BUILD FAILED in 14s

15 actionable tasks: 14 executed, 1 up-to-date
Executing tasks: [:app:assembleDebug]

:app:buildInfoDebugLoader
:app:preBuild UP-TO-DATE
:app:preDebugBuild UP-TO-DATE
:app:compileDebugAidl UP-TO-DATE
:app:compileDebugRenderscript UP-TO-DATE
:app:checkDebugManifest UP-TO-DATE
:app:generateDebugBuildConfig UP-TO-DATE
:app:prepareLintJar UP-TO-DATE
:app:generateDebugResValues UP-TO-DATE
:app:generateDebugResources UP-TO-DATE
:app:mergeDebugResources UP-TO-DATE
:app:createDebugCompatibleScreenManifests UP-TO-DATE
:app:processDebugManifest
:app:splitsDiscoveryTaskDebug UP-TO-DATE
:app:processDebugResources
:app:generateDebugSources
:app:javaPreCompileDebug
:app:compileDebugJavaWithJavac
C:\Users\Simon\AndroidStudioProjects\Project7io\app\src\main\java\com\example\simon\project7io\MainActivity.java:19: error: <identifier> expected
myWebView.loadUrl("http://project7.io");
                 ^
C:\Users\Simon\AndroidStudioProjects\Project7io\app\src\main\java\com\example\simon\project7io\MainActivity.java:19: error: illegal start of type
myWebView.loadUrl("http://project7.io");
                  ^
2 errors

 FAILED
:app:buildInfoGeneratorDebug

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.

* Get more help at https://help.gradle.org

BUILD FAILED in 3s

1 个答案:

答案 0 :(得分:0)

public class YourActivity extends AppCompatActivity {
    WebView myWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yourlayout);
        myWebView = (WebView) findViewById(R.id.webView);
        myWebView.loadUrl("https://example.com/");
    }
}

您应该在onCreate()内调用loadUrl方法