简体   繁体   English

在其他类中使用摘要中的变量

[英]use variable from abstract in other class

I am new to java and android development.I have created a abstract class for to display a listview in my app.I idea is that i will use common listview only changing its adapter.But i am not getting the listview defined in abstract class to use in the class which extends it.Please do help me out 我是java和android开发的新手,我创建了一个抽象类以在我的应用程序中显示列表视图。我的想法是我将只使用普通的listview来更改其适配器。但是我没有将抽象类中定义的listview更改为在扩展它的类中使用。请帮助我

Abstract Class Code 抽象类代码

 public abstract class GeneralListView extends MasterFragment {

    protected ListView listView;



    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.common_list, container, false);
        listView = (ListView) view.findViewById(R.id.lv_common_list);
        return view;
    }

    public abstract void initializeList();

    public ListView getListView() {
        return listView;
    }


}

Class which extends the above class 扩展上述课程的课程

 public class Test extends GeneralListView {

    private ListView listView;

    @Override
    public void initializeList() {
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(contextFragment, android.R.layout.activity_list_item, new String[]{"a", "b", "c", "d", "e", "f", "g", "h", "i", "a", "b", "c", "d", "e", "f", "g", "h", "i"});
        listView.setAdapter(adapter);

    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getListView();
        initializeList();
    }

    @Override
    public ListView getListView() {
        return listView;
    }
}

Exception 例外

listView.setAdapter(adapter); //null pointer exception

No need to override getListVIew() in child class "Test" class and you dont need the private ListView listView; 无需在子类“ Test”类中​​重写getListVIew(),并且不需要私有ListView listView; attribute in your "Test" class, 属性在“测试”类中,

To call a method or a variable in the super class you can call it directly if your not using any variable with the same name in your case you can call listView by using the super keyword 要调用超类中的方法或变量,如果您不使用同名的任何变量,则可以直接调用它,可以使用super关键字调用listView

super.listView 

and call the method getListView() with super keyword 并使用super关键字调用方法getListView()

super.getListView();

You have to either remove the 您必须删除

private ListView listView;

statement, or else in constructor do add the following, 语句,否则在构造函数中添加以下内容,

public Test(){
listView = super.getListView;//I assume this would return initialized listview
}

Remove 去掉

private ListView listView; 

and

@Override
public ListView getListView() {
    return listView;
}

from

public class Test extends GeneralListView {

You override the listView object in your Test subclass and that one is used instead of the instance from GeneralListView. 您在Test子类中重写listView对象,并且使用该对象代替GeneralListView中的实例。

您是否可以检查是否调用了onCreateView方法的调用,因为我只能看到此方法初始化了listView引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM