初始化扩展FrameLayout的类时的InstantiationException

时间:2012-11-23 12:22:50

标签: android android-view

我创建了一个扩展FrameLayout的类:

public class EnhancedMapView extends FrameLayout
{
private CustomMapView mapViewComponent;

public EnhancedMapView(Context context,String apiKey)
{
    super(context);
    /*mapViewComponent=new CustomMapView(context,apiKey);
    catch (Exception exception) 
    { 
        StackTraceElement[] stackTraceElements=exception.getStackTrace();
        for (StackTraceElement stackTraceElement:stackTraceElements)
            Log.i("Blabla",stackTraceElement.toString());
    }
    buildMapViewLayout();*/
}
}

这是我正在使用该课程的活动:

public class TestMapViewActivity extends Activity 
{
private EnhancedMapView enhancedMapView;

@Override protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    String apiKey=getResources().getString(R.string.google_maps_api_key);
    enhancedMapView=new EnhancedMapView(this,apiKey);
    setContentView(enhancedMapView);
}
}

我得到了:

11-23 11:43:44.425: E/AndroidRuntime(440): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{shira.android.mapview/shira.android.mapview.EnhancedMapView}: java.lang.InstantiationException: shira.android.mapview.EnhancedMapView
11-23 11:43:44.425: E/AndroidRuntime(440):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
11-23 11:43:44.425: E/AndroidRuntime(440):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
11-23 11:43:44.425: E/AndroidRuntime(440):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
11-23 11:43:44.425: E/AndroidRuntime(440):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
11-23 11:43:44.425: E/AndroidRuntime(440):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-23 11:43:44.425: E/AndroidRuntime(440):  at android.os.Looper.loop(Looper.java:130)
11-23 11:43:44.425: E/AndroidRuntime(440):  at android.app.ActivityThread.main(ActivityThread.java:3683)
11-23 11:43:44.425: E/AndroidRuntime(440):  at java.lang.reflect.Method.invokeNative(Native Method)
11-23 11:43:44.425: E/AndroidRuntime(440):  at java.lang.reflect.Method.invoke(Method.java:507)
11-23 11:43:44.425: E/AndroidRuntime(440):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-23 11:43:44.425: E/AndroidRuntime(440):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-23 11:43:44.425: E/AndroidRuntime(440):  at dalvik.system.NativeStart.main(Native Method)
11-23 11:43:44.425: E/AndroidRuntime(440): Caused by: java.lang.InstantiationException: shira.android.mapview.EnhancedMapView
11-23 11:43:44.425: E/AndroidRuntime(440):  at java.lang.Class.newInstanceImpl(Native Method)
11-23 11:43:44.425: E/AndroidRuntime(440):  at java.lang.Class.newInstance(Class.java:1409)
11-23 11:43:44.425: E/AndroidRuntime(440):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
11-23 11:43:44.425: E/AndroidRuntime(440):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)

最初,我有一些使用谷歌地图的代码,但我评论了所有这些。似乎异常来自FrameLayout构造函数,我从我的自定义视图类调用(使用super),我不知道是什么导致它发生...

有人可以帮我这个吗?感谢。

2 个答案:

答案 0 :(得分:0)

尝试添加默认构造函数:

public EnhancedMapView(Context context){
     this(context, null);
}

public EnhancedMapView(Context context, AttributeSet attrs){
     this(context, attrs, 0);
}

public EnhancedMapView(Context context, AttributeSet attrs, int defStyle){
     super(context, attrs, defStyle);
}

答案 1 :(得分:0)

对不起,这是我犯的一个愚蠢的错误。我在清单中的活动声明中放入了视图的名称而不是活动。我向任何试图帮助的人道歉。