0

i want to click on a button and it open an activity with an web view. main activity

package test.example.webviewtest;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.widget.Button;

public class Main extends Activity {

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

    Button page1 = (Button) findViewById(R.id.button1);
    Intent intent1 = new Intent (Main.this,WebViewPage.class);
    intent1.putExtra("page1", "file:///android_asset/1.html");
    Main.this.startActivity(intent1);

    Button page2 = (Button) findViewById(R.id.button1);
    Intent intent2 = new Intent (Main.this,WebViewPage.class);
    intent2.putExtra("page2", "file:///android_asset/2.html");
    Main.this.startActivity(intent2);

}   
}

second activity

package test.example.webviewtest;

import android.app.Activity;
import android.os.Bundle;

public class WebViewPage extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.webviewpage);

    mWebView = findViewById(R.id.webView1);
    Bundle extras = getIntent().getExtras();
    if (extras != null){
    String page1Url = extras.getString("page1");
    String page2Url = extras.getString("page2");
    if (page1Url != null)
    mWebView.loadUrl(page1Url);
    else if (page2Url != null)
    mWebView.loadUrl(page2Url);
    }
}

}

in main activity no problem but in second one, mWebview in 3 place eclipse give me error. what should i do for solve this problem.

1
  • are you able to compile the WebViewPage class? Where you declare the mWebView attribute? Commented Sep 26, 2012 at 14:11

2 Answers 2

1

change that line

mWebView = findViewById(R.id.webView1);

to this

WebView mWebView = (WebView) findViewById(R.id.webView1);
Sign up to request clarification or add additional context in comments.

Comments

0

The following link provides you a solution for loading webview in an activity by clicking a button.

Android: How to open the webview in a new screen

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.