错误:此类应提供默认构造函数(不带参数的公共构造函数)

时间:2014-07-18 05:15:00

标签: java android default-constructor

当我要构建我的项目时,它会给出此错误消息。

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

我可以使用USB调试运行应用程序,没有任何错误。但是,当我构建APK时会发生错误。

这是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;
    }
}

这是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)

2 个答案:

答案 0 :(得分:2)

Error:Error: This class should provide a default constructor  public constructor with no arguments) ViewPagerAdapter) [Instantiatable]

我在android studio中创建了一个活动类时遇到了这个错误,默认情况下,在Android Manifest中自动添加了一个活动标签,当我想创建/使用java类时。

我通过转到我的Android清单文件并删除ViewPagerAdapter活动代码解决了这个问题 enter image description here

答案 1 :(得分:1)

你必须提供默认super()并更改适配器的名称,因为android中已经有ExpandableListAdapter。假设您将adapter class重命名为CustomExpandableListAdapter。所以,构造函数应该是

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