You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Studio开发启动打开网页应用遇Error <identifier> expected报错求助

Fixing "Error expected" in your Android WebView App

Hey there! Let's sort out that "Error expected" error you're seeing. There are a couple of straightforward issues with your code structure and some typos that are causing this problem. Here's what's wrong and how to fix it:

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:
    • string should be String (the Java string class is capitalized)
    • Webview should be WebView (Android's WebView class uses camel case with a capital V)
    • getsettings() should be getSettings() (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 INTERNET permission to your AndroidManifest.xml (required to load web pages):
    <uses-permission android:name="android.permission.INTERNET" />
    
  • Double-check that your activity_main.xml layout actually contains a WebView with the id webview, otherwise findViewById will return null and cause a crash.

内容的提问来源于stack exchange,提问作者Christian Frandsen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:16:23