• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Webview open as new screen

GIR

Member
Hi Folks,

Ive been searching for a while (here, in other forums, and good ol' google), but i cant find a solution.

My goal is this, to have a button click open a webview and display embedded HTML.
Ive got a button press to show a 'toast' pop-up, so i know where to hook the code to open the webview.

when I try to set up the web view with :
Code:
WebView webview = new WebView(this);

I get this error:
Code:
The constructor WebView(new View.OnClickListener(){}) is undefined

Any help appreciated,
GIR
 
Because your 'WebView webview = new WebView(this);' code is in a nested method you need to put your context in a variable since 'this' doesn't point to your activity in a nested method.
 
  • Like
Reactions: GIR
Thank you very much id0001!

As soon as funds allow I'll be investing in Android Programming books so I'm not posting silly questions, but quite simply its people like you that take the time to answer these types of questions that help newbies to learn.

All the best,
GIR
 
It's a java thing actually. The 'this' keyword refers to a new scope which you create by nesting.

example:
Code:
// start of scope 1
class Foo {

   int x = 0;

   public Foo {
      this.x = 3 // this works because this refers to scope 1

      // start of scope 2
      buttonx.setOnClickListener(new OnClickListener() {
         
         // this is a nested class

         public void onClick() {
            this.x = 3 // this wil fail since x is not in this scope.

            // a new class scope is define by the new OnClickListener() {}
            // the 'this' keyword will refer to the OnClickListener() {}
         }

      });
   }
}

I hope this explains enough.
 
  • Like
Reactions: GIR
Back
Top