【问题标题】:Error: This class should provide a default constructor (a public constructor with no arguments)错误:此类应提供默认构造函数(不带参数的公共构造函数)
【发布时间】:2014-09-09 03:48:04
【问题描述】:

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

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)

【问题讨论】:

    标签: java android default-constructor


    【解决方案1】:
    Error:Error: This class should provide a default constructor  public constructor with no arguments) ViewPagerAdapter) [Instantiatable]
    

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

    我通过转到我的 Android 清单文件并删除 ViewPagerAdapter 活动标签来解决它

    【讨论】:

      【解决方案2】:

      您必须提供默认的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

      【讨论】:

      • 但是“我可以使用 USB 调试运行应用程序而没有任何错误。但是,当我构建 APK 时会发生错误。”
      • 它仍然给出错误按摩。是的,我可以运行应用程序。但是当我构建 apk 时它给出了错误
      • 发布完整版stack trace
      • 错误与你在问题中提到的尝试在真实设备中运行无关
      • 但是应用程序在devide中运行。它没有构建apk
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多