简体   繁体   中英

Error: This class should provide a default constructor (a public constructor with no arguments)

When I'm going to Build my project it give this error message.

Error:Error: This class should provide a default constructor (a public constructor with no arguments) (status.PURPLE.sameera.phonestatus.ExpandableListAdapter) [Instantiatable]

I can run the app with USB Debugging without any errors. However, the error happens when I build an APK.

This is ExpandableListAdapter.java

public class ExpandableListAdapter extends BaseExpandableListAdapter {

    private Context _context;
    private List<String> _listDataHeader; // header titles

    private HashMap<String, List<String>> _listDataChild;

    public ExpandableListAdapter(Context context, List<String> listDataHeader,
                                 HashMap<String, List<String>> listChildData) {
        this._context = context;
        this._listDataHeader = listDataHeader;
        this._listDataChild = listChildData;
    }




    @Override
    public Object getChild(int groupPosition, int childPosititon) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .get(childPosititon);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public View getChildView(int groupPosition, final int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {

        final String childText = (String) getChild(groupPosition, childPosition);

        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_item,null);
        }

        TextView txtListChild = (TextView) convertView
                .findViewById(R.id.lblListItem);

        txtListChild.setText(childText);
        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .size();
    }


    @Override
    public Object getGroup(int groupPosition) {
        return this._listDataHeader.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return this._listDataHeader.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        String headerTitle = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_group, null);
        }

        TextView lblListHeader = (TextView) convertView
                .findViewById(R.id.lblListHeader);
        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(headerTitle);

        return convertView;
    }


    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}

This is MyActivity.java

public class MyActivity extends ActionBarActivity {

    //Listview
    ExpandableListAdapter listAdapter;
    ExpandableListView expListView;
    List<String> listDataHeader;
    HashMap<String, List<String>> listDataChild;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);


        // get the listview
        expListView = (ExpandableListView) findViewById(R.id.lvExp);

        // preparing list data
        prepareListData();

        listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);

        // setting list adapter
        expListView.setAdapter(listAdapter);
    }

    //preparing list data
    private void prepareListData(){
        listDataHeader = new ArrayList<String>();
        listDataChild = new HashMap<String, List<String>>();

        listDataHeader.add("Top 250");
        listDataHeader.add("Now Showing");
        listDataHeader.add("Coming Soon..");

        List<String> top250 = new ArrayList<String>();
        top250.add("The Shawshank Redemption");

        listDataChild.put(listDataHeader.get(0), top250); // Header, Child data
        listDataChild.put(listDataHeader.get(1), nowShowing);
        listDataChild.put(listDataHeader.get(2), comingSoon);
    }
}

Logcat

07-18 10:00:01.380    1250-1250/status.PURPLE.sameera.phonestatus W/EGL_genymotion﹕ eglSurfaceAttrib not implemented
07-18 10:00:01.388    1250-1250/status.PURPLE.sameera.phonestatus E/OpenGLRenderer﹕   Getting MAX_TEXTURE_SIZE from GradienCache
07-18 10:00:01.396    1250-1250/status.PURPLE.sameera.phonestatus E/OpenGLRenderer﹕ MAX_TEXTURE_SIZE: 16384
07-18 10:00:01.424    1250-1250/status.PURPLE.sameera.phonestatus E/OpenGLRenderer﹕ Getting MAX_TEXTURE_SIZE from Caches::initConstraints()
07-18 10:00:01.432    1250-1250/status.PURPLE.sameera.phonestatus E/OpenGLRenderer﹕   MAX_TEXTURE_SIZE: 16384
07-18 10:00:09.788    1250-1250/status.PURPLE.sameera.phonestatus E/eglCodecCommon﹕ **** ERROR unknown type 0xfc00 (glSizeof,73)
07-18 10:00:09.892    1250-1250/status.PURPLE.sameera.phonestatus E/eglCodecCommon﹕ **** ERROR unknown type 0xfc00 (glSizeof,73)
07-18 10:00:09.964    1250-1250/status.PURPLE.sameera.phonestatus E/eglCodecCommon﹕ **** ERROR unknown type 0xfc00 (glSizeof,73)
07-18 10:00:10.028    1250-1250/status.PURPLE.sameera.phonestatus E/eglCodecCommon﹕ ****    ERROR unknown type 0xfc00 (glSizeof,73)
07-18 10:00:10.076    1250-1250/status.PURPLE.sameera.phonestatus E/eglCodecCommon﹕ ****    ERROR unknown type 0xfc00 (glSizeof,73)
07-18 10:00:10.120    1250-1250/status.PURPLE.sameera.phonestatus E/eglCodecCommon﹕ ****    ERROR unknown type 0xfc00 (glSizeof,73)
07-18 10:00:10.164    1250-1250/status.PURPLE.sameera.phonestatus E/eglCodecCommon﹕ ****    ERROR unknown type 0xfc00 (glSizeof,73)
07-18 10:00:10.212    1250-1250/status.PURPLE.sameera.phonestatus E/eglCodecCommon﹕ ****    ERROR unknown type 0xfc00 (glSizeof,73)
07-18 10:00:10.260    1250-1250/status.PURPLE.sameera.phonestatus E/eglCodecCommon﹕ **** ERROR unknown type 0xfc00 (glSizeof,73)
Error:Error: This class should provide a default constructor  public constructor with no arguments) ViewPagerAdapter) [Instantiatable]

I experienced this error when I created an activity class in android studio, by default an activity tag was automatically added in Android Manifest, when idealy I was supposed to create/use a java class.

I solved it by going to my Android manifest file and deleting the ViewPagerAdapter activity tag 在此处输入图片说明

you have to provide default super() and change your adapter's name because there is already ExpandableListAdapter in android. Suppose you rename your adapter class to CustomExpandableListAdapter . So, constructor should be

public CustomExpandableListAdapter(Context context, List<String> listDataHeader,
                             HashMap<String, List<String>> listChildData) {
    super();
    this._context = context;
    this._listDataHeader = listDataHeader;
    this._listDataChild = listChildData;
}

Example

Official doc of BaseAdapter

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