简体   繁体   中英

View - Has no zero argument constructor

I'm following the tutorial at https://github.com/codepath/android_guides/wiki/Basic-Painting-with-Views which states that to get started I should create a view:

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

Note that up to here everything is fine. I then add the XML layout:

<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" />

Now I've tried to do the following on my own code, and even with the exact code provided every time I run the emulator I get the following error:

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

Any ideas on how to solve this seemingly trivial issue?

The problem has nothing to do with your view. It has to do with com.company.cw.MainActivity . If you look at your error message, you will see that it does not mention your view, but does mention com.company.cw.MainActivity .

Your question does not include the code for com.company.cw.MainActivity , but apparently it implements a constructor, particularly one that takes parameters. Do not do this . For starters, it will never be invoked, so it's a waste of keystrokes. Whatever initialization you want to do should be in onCreate() , typically after super.onCreate() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM