错误 - 找不到符号“webview”

时间:2022-01-10 20:50:03

I have tried to run the below but I am getting the above error on compilation.

我试图运行以下但我在编译时遇到上述错误。

MainActivity.java:

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity {
private WebView myWebView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    myWebView = (WebView)findViewById(R.id.webView);
    WebSettings webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    myWebView.loadUrl("http://www.toptipstricks.com");
    myWebView.setWebViewClient(new WebViewClient());
}

@Override
public void onBackPressed() {
    if(myWebView.canGoBack()){
        myWebView.goBack();
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

Android Manifest.xml:

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

I am getting the error that symbol webview can't be found! I am new to java so I am not sure where to check for errors!

我收到无法找到符号webview的错误!我是java的新手,所以我不确定在哪里检查错误!

1 个解决方案

#1


1  

Make sure that your id of webview component into "activity_main" is "webView". Something like this:

确保您的webview组件的id为“activity_main”为“webView”。像这样的东西:

<WebView
    android:id="@+id/webView"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</WebView>

Please, post yor error log and also your layout.

请发布错误日志以及您的布局。

#1


1  

Make sure that your id of webview component into "activity_main" is "webView". Something like this:

确保您的webview组件的id为“activity_main”为“webView”。像这样的东西:

<WebView
    android:id="@+id/webView"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</WebView>

Please, post yor error log and also your layout.

请发布错误日志以及您的布局。