【发布时间】:2015-05-04 06:18:44
【问题描述】:
当我从子类调用父方法时,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("...");
};
};
【问题讨论】:
-
你在哪里使用
ChildClass类? -
检查是否可以在 Child 类中直接调用 MyParentMethod()。(没有 super)
-
当我直接调用该方法
MyParentMethod()(没有超级)时,我得到一个 NPE 错误。我正在使用ChildClass,因为我有 2 个已编译的 jar 库,用于 2 个不同的集成硬件,需要注意的是这两个库都使用具有重复名称的处理方法。子类允许我将这些方法分开。
标签: java android inheritance webview parent-child