6

I am trying to implement one of the solutions found here.

My problem is that I'm not sure if I am implementing and using my subclass correctly. I am subclassing a WebView here:

public class myWebView extends WebView{

  public myWebView(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
}

  @Override
protected void onSizeChanged(int w, int h, int ow, int oh) {
    // TODO Auto-generated method stub

      scrollTo(xScroll - (widthScroll/2), yScroll - (heightScroll/2));


    super.onSizeChanged(w, h, ow, oh);
}

   }}

It should be private but forget that for now. I have the code inside of one of my activities that has an inner webview in the view hierarchy. Outside of the onCreate method of that activity.

Inside the onCreate method, I have:myWebView mapImage = (myWebView) findViewById(R.id.mapcroppic);

This gives me a ClassCastException for that call. (Does the xml layout file need to use <myWebView>? Can it?) How do I use this the correct way?

Community
  • 1
  • 1
joepetrakovich
  • 1,344
  • 3
  • 24
  • 42

1 Answers1

9

(Does the xml layout file need to use <myWebView>? Can it?) How do I use this the correct way?

Yes:

<your.package.name.myWebView
    android:layout_with="blah"/>

Well, that works if the myWebView is a public class. If it's an inner one:

<view class="your.package.name.myWebView.YourActivity$myWebView" />
Cristian
  • 198,401
  • 62
  • 356
  • 264
  • Thanks for the update, I was just going to ask for that additional info. – joepetrakovich Jan 19 '11 at 02:18
  • I'm getting ClassNotFoundException. Are you sure that is the right order of things? For example, my app is called SlingShot. The package is com.joe.sling, the activity that holds the myWebView subclass is ShootActivity, so I should have – joepetrakovich Jan 19 '11 at 02:23
  • Unfortunately it is confidential, but what I have given you is exactly how I did it, just with names changed. – joepetrakovich Jan 19 '11 at 02:44
  • 1
    I think I have it working, but now I'm getting a ERROR/AndroidRuntime(7699): Caused by: java.lang.NoSuchMethodException: myWebView(Context,AttributeSet) – joepetrakovich Jan 19 '11 at 03:48
  • 2
    Add that constructor to your subclass. That's because that's the constructor that is called when a view is inflated from an XML – Cristian Jan 19 '11 at 03:57
  • I mean, you have this constructor already: `public myWebView(Context context)`, then create this one: `public myWebView(Context context, AttributeSet ppp)` – Cristian Jan 19 '11 at 03:57