First Last
First Last

Reputation: 11

load web view in an activity by clicking a button

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.

Upvotes: 0

Views: 3001

Answers (2)

nano_nano
nano_nano

Reputation: 12523

change that line

mWebView = findViewById(R.id.webView1);

to this

WebView mWebView = (WebView) findViewById(R.id.webView1);

Upvotes: 1

user1537319
user1537319

Reputation:

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

Upvotes: 0

Related Questions