[英]Android Studio variable cannot be found WebView
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.webkit.WebSettings;
import android.webkit.WebView;
import static com.example.tencho.cst.R.id.*;
public class MainActivity extends AppCompatActivity {
private WebView MywebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MywebView =(WebView)this.findViewById(WebView);
WebSettings webSettings=MywebView.getSettings();
webSettings.setJavaScriptEnabled(true);
MywebView.loadUrl("www.cst.edu.bt");
}
}
This is the MainActivity.java
. 这是
MainActivity.java
。 it's always giving me an error cannot find symbol variable WebView
. 它总是给我一个错误,
cannot find symbol variable WebView
。
I did read the past similar post but I couldn't solve it error. 我确实读了过去的类似文章,但无法解决它。 Please need help!
请帮忙!
At this line 在这条线
MywebView =(WebView)this.findViewById(id);
MywebView =(WebView)this.findViewById(id);
id should be like R.id.webview1 id应该像R.id.webview1
webview1 is the id in your XML layout
webview1是XML布局中的ID
The error is on this line: 错误在此行上:
MywebView =(WebView)this.findViewById(WebView);
The cause of this error is that the generated R.id
class has no field named WebView
(the argument you are passing to the findViewById()
method). 导致此错误的原因是,生成的
R.id
类没有名为WebView
字段(正在传递给findViewById()
方法的参数)。 This is probably happening because your layout's <WebView>
tag is missing an id
attribute. 这可能是由于布局的
<WebView>
标记缺少id
属性引起的。 Add this line to your <WebView>
tag: 将此行添加到您的
<WebView>
标记中:
android:id="@+id/WebView"
Ben's answer is correct. 本的答案是正确的。 In addition, if your new to Android, if you added this part in your tag which can be found in your activity_main.xml layout..
此外,如果您是Android的新手,则将其添加到可以在activity_main.xml布局中找到的标记中。
android:id="@+id/webview"
then in your MainActivity.java, it should be like this: 然后在您的MainActivity.java中,应该是这样的:
MywebView =(WebView) findViewById(R.id.webview);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.