Custom MapView throwing NoSuchMethodException, but it's there!

本秂侑毒 提交于 2019-12-10 16:28:15

问题


I'm trying to implement a custom MapView. Inside my MapActivity (named mainmap) I have an inner class which extends MapView:

private class Lmapview extends MapView{

    public Lmapview(Context context, AttributeSet attrs) {
        super(context, attrs);
        gestures = new GestureDetector(mainmap.this, new GestureListener(this));
    }

    public boolean OnTouchEvent(MotionEvent event){
        return gestures.onTouchEvent(event);

    }
}

I have my main.xml formatted to find the inner class like so:

<?xml version="1.0" encoding="utf-8"?>
<view
    xmlns:android="http://schemas.android.com/apk/res/android"
    class="com.mondo.tbuddy.mainmap$Lmapview"
    android:id="@+id/mapview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:clickable="true"
    android:apiKey=*****
/>

Also, in Androidmanifest.xml, I have the appropriate <uses-library android:name="com.google.android.maps"/> entry.

When I try to run my app, I get (amongst other things) in logcat:

ERROR/AndroidRuntime(14999): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class com.mondo.tbuddy.mainmap$Lmapview

This is caused by this entry I find in logcat:

ERROR/AndroidRuntime(14999): Caused by: java.lang.NoSuchMethodException: Lmapview(Context,AttributeSet)

If I understand correctly, my app is crashing because Android is saying it doesn't find the appropriate constructor for my custom MapView (the Lmapview class). As you can see above, however, it is defined and it matches the signature it is looking for.

Can anyone give me some insight?

Thanks.


回答1:


You cannot instantiate an inner non-static class before you have a super class object. Because of this you have to make the inner class static, or move it to a separate class.



来源:https://stackoverflow.com/questions/3682139/custom-mapview-throwing-nosuchmethodexception-but-its-there

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!