views:

831

answers:

1

I'm new to Android.

I would like to simply know where on a page the user has scrolled. When a certain point on the web page appears at the bottom of the screen, I want to trigger an event. But this code causes an exception. I know that WebView inherits getScrollY() from View. Am I not implementing it correctly?

Thanks in advance.

public class Scroll extends Activity {

    public WebView webview;
    public float yPos;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        WebView webview = new WebView(this);
        setContentView(webview);
        webview.loadUrl("file:///android_asset/Scroll.html");
    }


    public boolean onTouchEvent(final MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_MOVE) {
            yPos = webview.getScrollY();
            Log.v("Scroll", "yPos = " + yPos); 
        }
        return false;
    }
}
A: 

And the exception is?
Here is a note on WebView screen heights from one of my apps:

// current position (top) = getScrollY();  
// max position = getContentHeight();  
// screen height = getHeight();
alex
Thanks for answering!It's a NullPointerException.The debugger stops at the line "yPos = webview.getScrollY()" but I can't figure out where the null pointer is coming from.As for your note, I am using getScrollY() so you have confirmed that's the right method but the way I'm using it is apparently wrong.
Dave
Actually I found that in the line "yPos = webview.getScrollY()" , the "webview" variable is null, even though in the onCreate method, "WebView webview = new WebView(this);" is working. Does the "webview" variable not have a scope that extends out of the onCreate method? And if not, how do I get a handle on it?
Dave
@DaveIt should be webview = new WebView(this); instead of WebView webview = new WebView(this); in onCreate(..).
alex