2015-05-04 37 views
0

當我從子類調用父方法時,WebView引用爲空,我找不出爲什麼或如何修復。預先感謝您的幫助!從子類調用父方法時,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("..."); 
    }; 

}; 
+0

你在哪裏使用'ChildClass'類? –

+0

檢查是否可以直接調用Child類中的MyParentMethod()(不包含超類) – Harry

+0

當我直接調用MyParentMethod()方法時(沒有超類),我得到一個NPE錯誤。 我正在使用'ChildClass',因爲我有2個編譯的jar庫,用於2個不同的集成硬件塊,警告的是這兩個庫都使用具有重複名稱的處理方法。子分類允許我將這些方法分開。 – Michael

回答

0

你爲什麼要打電話的ParentClassMyParentMethod()super關鍵字?

super關鍵字通常用於ChildClass成員和ParentClass成員(方法/變量)具有相同的名稱。

你可以調用父類方法剛剛與方法名(如果ChildClass沒有相同的方法)的方法也屬於ChildClass,像methodName(arguments)

因此,您應該將super.MyParentMethod("...");替換爲new ParentClass().MyParentMethod("...");MyParentMethod("...");

+0

我嘗試以推薦的方式調用父方法'new ParentClass()。MyParentMethod(「...」);'''MyParentMethod(「...」);'。兩人都返回** NPE **。同樣,當我打電話使用「超級」的方法工作,但是,原始參考爲空。 – Michael