Android Studio开发启动打开网页应用遇Error <identifier> expected报错求助
Fixing "Error expected" in your Android WebView App
Hey there! Let's sort out that "Error
Key Issues:
- Code placement: You've put your variable declarations and WebView logic directly in the class body, not inside a method (like
onCreate). In Java, executable code and variable initializations (that aren't class-level members) need to live inside a method. - Case sensitivity typos: Java is case-sensitive, so:
stringshould beString(the Java string class is capitalized)Webviewshould beWebView(Android's WebView class uses camel case with a capital V)getsettings()should begetSettings()(method names follow camel case too)
Corrected Code:
package com.cryptocrea.sitr; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.webkit.WebView; // Don't forget to import WebView! public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Move all your WebView logic inside onCreate String url = "http://cryptocrea.com/"; // Fixed capitalization and variable name typo WebView view = (WebView) this.findViewById(R.id.webview); // Uncomment and fix the method name if you need JavaScript enabled view.getSettings().setJavaScriptEnabled(true); view.loadUrl(url); } }
Additional Notes:
- Make sure you've added the
INTERNETpermission to yourAndroidManifest.xml(required to load web pages):<uses-permission android:name="android.permission.INTERNET" /> - Double-check that your
activity_main.xmllayout actually contains a WebView with the idwebview, otherwisefindViewByIdwill return null and cause a crash.
内容的提问来源于stack exchange,提问作者Christian Frandsen
相关产品推荐
相关产品推荐

