如何调用有参数的抽象类覆盖方法
问题描述:
我有一个接口:如何调用有参数的抽象类覆盖方法
然后:
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
在:
Log.v("MyApp", ">>>> " + myDBObject.get_myObjectDescription());
这是我怎么称呼它:
MyDBObject myObject = new MyObject();
myObject.set_myObjectDescription("HELLO WORLD");
ICustomObjectListingViews iCustomObjectListingViews = new MyCustomObjectListingView();
iCustomObjectListingViews.createCustomObjectListingView(myObject);
什么我收到错了吗?我应该如何调用覆盖类的overriden方法?我怎样才能使上述尝试工作?
谢谢大家提前。
答
如果你不知道如何实现createCustomObjectListingView(MyDBObject myDBObject)
在AbstractICustomObjectListingViews
,让具体类MyCustomObjectListingView
(这样你可以避免null
收益)直接实现它。
从AbstractICustomObjectListingViews
删除的方法,使其看起来像
public abstract class AbstractICustomObjectListingViews implements ICustomObjectListingViews {
//other methods
}
,并留下MyCustomObjectListingView
,因为它是。
不,你不会错。它看起来不错。它在我的电脑上正常工作。 – motis10