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
}
}
答案 0 :(得分:5)
只需使用
Application.Context.StartActivity(intent);
答案 1 :(得分:2)
当您遇到状态的编译器错误消息时,StartActivity是Context的实例方法,而不是静态方法,因此无法像这样调用。
您没有将它包含在代码示例中,但是在活动中您将“this”(活动)传递给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();
}
}
}