MainActivity中的类型不正确

时间:2013-10-01 19:52:23

标签: android button webview

我正在创建一个webView浏览器,我正在尝试创建一个向下滑动以显示更多按钮的按钮,我已经显示了下面发生错误的代码,我还说明了错误发生的行上。提前谢谢!

ExpandableListView expandableList = getExpandableListView(); *<-The method*             -   *getExpandableListView() is undefined for the type MainActivity IS THE ERROR I GET ON  -   THAT LINE*
(ExpandableListView) findViewById(R.id.list)

expandableList.setDividerHeight(2);
expandableList.setGroupIndicator(null);
expandableList.setClickable(true);

setGroupParents();
setChildData();

MyExpandableAdapter adapter = new MyExpandableAdapter(parentItems,childItems);

adapter.setInflater((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE),  -   this);
expandableList.setAdapter(adapter);
expandableList.setOnChildClickListener(this); *<-The method                                 -   setOnChildClickListener(ExpandableListView.OnChildClickListener) in the type            -   ExpandableListView is not applicable for the arguments (MainActivity) IS THE ERROR I   -   GET ON THAT LINE*
}

2 个答案:

答案 0 :(得分:0)

我从未使用过ExpandableListView,但我确信它与ListView相似,如果你没有延长ExpandableListActivity那么你就会得到错误。

您需要在View layout中创建自己的ExpandableListView,或者extends ExpandableListView中需要Activity才能使用其方法以这种方式和听众,如getExpandableListView()

答案 1 :(得分:0)

MainActivity必须扩展ExpandableListActivity。我怀疑你刚刚使用了一个Activity。您必须提供声明您的活动的行。

expandableList.setOnChildClickListener()接受ExpandableListView.OnChildClickListener类型的参数。在你提交的代码中,我没有看到你已经实例化了一个Listener。如果您的活动声明还声明您实现了ExpandableListView.OnChildClickListener接口,则我看不到onChildClic()的覆盖。

简而言之,你应该

public class MainActivity extends 
    ExpandableListActivity implements ExpandableListView.OnChildClickListener {
    ...
    @Override
    public boolean onChildClick(ExpandableListView parent, View v, int 
            groupPosition, int childPosition, long id) {
        ...
    }
    ...
}