如何调用具有参数的Abstract类overriden方法

时间:2017-12-03 07:46:09

标签: java android polymorphism abstraction

我有一个界面:

public interface ICustomObjectListingViews {
    View createCustomObjectListingView(MyDBObject myDBObject);
}  

然后:

public abstract class AbstractICustomObjectListingViews implements ICustomObjectListingViews {

@Override
    public View createCustomObjectListingView(MyDBObject myDBObject) {
        return null;
    }
}  

然后我尝试通过扩展抽象类来实现接口:

public class MyCustomObjectListingView extends AbstractICustomObjectListingViews {

@Override
    public VIew createCustomObjectListingView(MyDBObject myDBObject) {
        Log.v("MyApp", ">>>> " + myDBObject.get_myObjectDescription());

        TextView textView = new TextView(mContext);
        textView.setText(myDBObject.get_myObjectDescription());

        return textView;
    }
}  

我使用MyObject来映射我的数据库结果:

public class MyDBObject {
    public MyDBObject() {
    }

    private String _myObjectDescription;

    public void set_myObjectDescription(String _myObjectDescription) {
        this._myObjectDescription = _myObjectDescription;
    }

    public String get_myObjectDescription() {
        return _myObjectDescription;
    }
} 
但是,每当我尝试调用MyCustomObjectListingView createCustomObjectListingView(MyDBObject myDBObject)的实现时,我都会得到一个空指针。

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String MyDBObject.get_myObjectDescription()' on a null object reference  

at:

Log.v("MyApp", ">>>> " + myDBObject.get_myObjectDescription());  

这就是我所说的:

MyDBObject myObject = new MyObject();
myObject.set_myObjectDescription("HELLO WORLD");

ICustomObjectListingViews iCustomObjectListingViews = new  MyCustomObjectListingView();
iCustomObjectListingViews.createCustomObjectListingView(myObject);  

我错了什么?我应该如何打电话给最重要的班级'覆盖方法?我怎样才能使上述尝试奏效?

提前谢谢大家。

1 个答案:

答案 0 :(得分:0)

如果您不知道如何在createCustomObjectListingView(MyDBObject myDBObject)中实施AbstractICustomObjectListingViews,请直接在具体类MyCustomObjectListingView中实施(以这种方式避免使用null } return。。

AbstractICustomObjectListingViews中删除该方法,使其显示为

   public abstract class AbstractICustomObjectListingViews implements ICustomObjectListingViews {
     //other methods
} 

并保持MyCustomObjectListingView原样。