从子类调用父方法时,WebView引用为空

时间:2015-05-04 06:18:44

标签: java android inheritance webview parent-child

当我从子类调用父方法时,WebView引用为空,我无法弄清楚为什么或如何修复。提前感谢您的帮助!

示例父类:

public class ParentClass extends Activity {

public WebView MyWebView;

@Override
protected onCreate (Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    MyWebView = (WebView) findViewById(R.id.MyWebView);
};

public void MyParentMethod(String sURL) {
    //This "MyWebView" reference is null and nothing happens when called from child class???
    MyWebView.loadUrl(sURL);
};

};

示例子类:

public class ChildClass extends ParentClass {

    public void MyChildMethod() {
        super.MyParentMethod("...");
    };

};

1 个答案:

答案 0 :(得分:0)

为什么要使用MyParentMethod()关键字呼叫ParentClass的{​​{1}}?

super成员和super成员(方法/变量)具有相同名称时,通常会使用

ChildClass关键字。

您可以使用方法名称调用 ParentClass方法(如果ParentClass没有相同的方法),因为该方法也属于ChildClass ,如ChildClass

因此,您应该将此methodName(arguments)替换为super.MyParentMethod("...");new ParentClass().MyParentMethod("...");

相关问题