mu_sa
mu_sa

Reputation: 2725

Syntax error on token, delete this token

I am having this syntax error on token, delete this token error. Below are my two related files

MainActivity.java

package com.example.androidwebviewexample;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.webkit.WebView;

import com.example.androidwebviewexample.R;

public class MainActivity extends Activity {

WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadUrl("www.yahoo.com");  // ERROR !!!!!!

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}


}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:padding="@dimen/padding_medium"
    android:text="@string/hello_world"
    tools:context=".MainActivity" />
<WebView 
    android:id="@+id/webview" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
</RelativeLayout>

Upvotes: 1

Views: 4121

Answers (1)

user370305
user370305

Reputation: 109237

Where is onCreate() of your Activity?

You forgot to define onCreate() of your Activity. (From your code)

Now try this,

public class MainActivity extends Activity {
@Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
                WebView myWebView = (WebView) findViewById(R.id.webview);
                myWebView.loadUrl("www.yahoo.com");    
         }
  }

UPDATE:

After updated your question,

myWebView.loadUrl("www.yahoo.com");

you can not initialize or declare any method directly outside of Activity's any method.

Just put this line in onCreate() after of setContentView(R.layout.activity_main);

as I write in my above code, Then its works.

Upvotes: 6

Related Questions