视图 - 没有零参数构造函数

时间:2015-11-11 00:31:23

标签: java android

我按照https://github.com/codepath/android_guides/wiki/Basic-Painting-with-Views上的教程说明要开始我应该创建一个视图:

public class SimpleDrawingView extends View {
    public SimpleDrawingView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
}

请注意,到目前为止一切都很好。然后我添加XML布局:

<com.codepath.example.simpledrawapp.SimpleDrawingView
        android:id="@+id/simpleDrawingView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true" />

现在我已尝试在自己的代码上执行以下操作,即使每次运行模拟器时都提供了确切的代码,我也会收到以下错误:

java.lang.InstantiationException: java.lang.Class<com.company.cw.MainActivity> has no zero argument constructor
    enter code here

关于如何解决这个看似微不足道的问题的任何想法?

1 个答案:

答案 0 :(得分:1)

问题与您的观点无关。它与com.company.cw.MainActivity有关。如果您查看错误消息,您会看到它没有提及您的观点,但确实提到了com.company.cw.MainActivity

您的问题不包含com.company.cw.MainActivity的代码,但显然它实现了一个构造函数,特别是一个带参数的构造函数。 不要这样做。对于初学者来说,它永远不会被调用,因此它会浪费击键次数。无论您要执行什么初始化,都应该在onCreate()中,通常在super.onCreate()之后。