无法实例化 ExpandableListAdapter,因为它是抽象的

Posted

技术标签:

【中文标题】无法实例化 ExpandableListAdapter,因为它是抽象的【英文标题】:Can't instantiate ExpandableListAdapter because it is abstract 【发布时间】:2019-03-01 05:41:56 【问题描述】:

我正在尝试使用来自互联网的代码,但是在将上述代码插入我的个人项目时,我收到以下错误:

“ExpandableListAdapter”是抽象的;无法实例化

我已尝试自行研究以解决我的问题,但我仍然无法解决上述问题...

这是我的代码:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MainActivity extends AppCompatActivity 

private ExpandableListView listView;
private ExpandableListAdapter AdapterMedia;
private List<String> listDataHeader;
private HashMap<String,List<String>> listHash;

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

    listView = (ExpandableListView)findViewById(R.id.);
    initData();


    AdapterMedia = new ExpandableListAdapter(this, listDataHeader, listHash);


    listView.setAdapter(AdapterMedia);

任何帮助将不胜感激!

【问题讨论】:

您复制的代码是否还包含一个名为 ExpandableListAdapter 的类? 您无法实例化它,因为ExpandableListAdapter 是一个接口。您必须实例化它的一个实现类...请参阅this 并查看已知的间接子类。看看它们也会为您带来一些要使用的直接子类,例如 SimpleExpandableListAdapter 我明白了!我更改了班级的名称._。我的错,谢谢你的帮助! 【参考方案1】:

@deHaar 是正确的,ExpandableListAdapter接口,不能直接实例化。我会查看此tutorial 了解更多信息。

另外,作为一般的附注:findViewById(R.id.&lt;id&gt;;) 需要从您相应的 XML 布局定义的关联 &lt;id&gt;setContentView(R.layout.&lt;layout&gt;);

希望对您有所帮助!

【讨论】:

【参考方案2】:

您需要创建一个自定义的适配器类,例如,命名为“MyExpandableListAdapter”,然后在该类中实现ExpandableListAdapter。您还需要实现接口中声明的所有方法。

public class MyExpandableListAdapter implements ExpandableListAdapter 
   @Override
   public void registerDataSetObserver(DataSetObserver observer) ...

上网查教程:https://www.javacodegeeks.com/2013/06/android-expandablelistview-with-custom-adapter-baseexpandablelistadapter.html

【讨论】:

以上是关于无法实例化 ExpandableListAdapter,因为它是抽象的的主要内容,如果未能解决你的问题,请参考以下文章

无法实例化页面(net.thucydides.core.webdriver.DriverConfigurationError:无法实例化类 io.appium.java_client.AppiumDr

无法实例化 p5.js 代码(实例模式)

无法实例化 NSManagedObject 的子类

无法在 main() 方法中实例化字段(实例变量)。为啥??爪哇

“无法实例化活动 ComponentInfo”代表啥?

由于以下原因无法实例化模块: