2012-06-18 89 views
1
public class WeeklyInspection : Activity 
{ 
    WebView view = (WebView) FindViewById(Resource.Id.inspectionWV); 
    view.Settings.JavaScriptEnabled = true; 
    view.Settings.CacheMode = CacheModes.CacheElseNetwork; 
    view.SetWebChromeClient(new CustomWebChromeClient(this)); 
    view.SetWebViewClient(new CustomWebViewClient(this)); 
    view.LoadUrl("http://myurl.com"); 
} 

private class CustomWebChromeClient: WebChromeClient 
{ 
    public override void OnConsoleMessage(string message, int lineNumber, string sourceID) 
    { 
     if (message.StartsWith("Submit") 
      //do all my submit stuff here 
      //if without error, I want to go back to the Main Activity. Have tried: 
      Intent intent = new Intent(BaseContext, typeof(Main)); 
      StartActivity(intent); //Both BaseContext and StartActivity throw "Cannot access non-static method in static context" 
      //tried: 
      Finish(); //Same thing 
      //tried: 
      OnBackPressed(); //Same thing 
    } 
} 
+1

和問題是什麼? – Sajmon

+0

如果您需要任何幫助,請發佈您的問題... – Cata

+1

它位於CustomWebChromeClient中。請閱讀我的評論。 – jmease

回答

2

當你遇到狀態的編譯器錯誤消息,StartActivityContext實例方法,而不是一個靜態方法,因此不能稱爲類的。

您沒有將它包含在代碼示例中,而是在將「this」(activity)傳遞給構造函數到CustomWebChromeClient的活動中,所以我假定CustomWebChromeClient保留對它的引用。您可以使用該引用來調用您在活動中需要的實例方法,無論它是StartActivity,Finish還是其他任何您需要的。例如:

private class CustomWebChromeClient: WebChromeClient 
{ 
    private readonly Activity _context; 

    public CustomWebChromeClient(Activity context) 
    { 
     _context = context; 
    } 

    public override void OnConsoleMessage(string message, int lineNumber, string sourceID) 
    { 
     if (message.StartsWith("Submit")) 
     { 
      _context.Finish(); 
     } 
    } 
} 
4

只使用這

Application.Context.StartActivity(intent); 
+0

沒有併發症的竅門。謝謝。 –