1

In my onCreate, I have:

MyWebView webView = new MyWebView(this, null);
webView = (MyWebView) findViewById(R.id.pdf);

The custom webview class inside the Deal class:

public class MyWebView extends WebView {

     Paint p = new Paint();

     public MyWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override protected void onDraw(Canvas canvas) {

         super.onDraw(canvas);
         p.setColor (Color.RED);
         canvas.drawLine (20, 10, 300, 400, p);
     }
 }

The XML:

<im.magic.dealpdf.Deal.MyWebView
    android:id="@+id/pdf"
    android:layout_weight="0.4"
    android:layout_margin="5dp"
    android:layout_width="0dp"
    android:layout_height="match_parent"/>

The error states it didn't find the class im.magic.dealpdf.Deal.MyWebView. Why is the error coming up? Changing the xml to im.magic.dealpdf.Deal results in cannot be cast to android.view.View

Update: I think I need to extend View in my Deal class. But it already extends Activity

ono
  • 2,752
  • 9
  • 38
  • 78

1 Answers1

2

You should not use inner classes in such way, declare MyWebView as

public static class MyWebView extends WebView {
...
}

Otherwise it's not possible to create a MyWebView instance outside a Deal instance. Inner classes can't be referenced in such way in the layout file, as here you have to wrote something like this:

<view class="im.magic.dealpdf.Deal@MyWebView" 
    android:id="@+id/pdf"
    android:layout_weight="0.4"
    android:layout_margin="5dp"
    android:layout_width="0dp"
    android:layout_height="match_parent"/>
Community
  • 1
  • 1
spacifici
  • 2,158
  • 2
  • 15
  • 28