Android从课堂开始活动

Android从课堂开始活动,android,xamarin.android,Android,Xamarin.android,正如您遇到的编译器错误消息所述,它是的实例方法,而不是静态方法,因此不能这样调用 您没有将其包含在代码示例中,但在活动中,您将“this”(活动)传递到CustomWebChromeClient的构造函数中,因此我假设CustomWebChromeClient保留了对它的引用。您可以使用该引用来调用活动上所需的实例方法,无论是StartActivity、Finish还是其他您需要的方法。例如: public class WeeklyInspection : Activity { WebVie

正如您遇到的编译器错误消息所述,它是的实例方法,而不是静态方法,因此不能这样调用

您没有将其包含在代码示例中,但在活动中,您将“this”(活动)传递到CustomWebChromeClient的构造函数中,因此我假设CustomWebChromeClient保留了对它的引用。您可以使用该引用来调用活动上所需的实例方法,无论是StartActivity、Finish还是其他您需要的方法。例如:

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
    }
}

正如您遇到的编译器错误消息所述,它是的实例方法,而不是静态方法,因此不能这样调用

您没有将其包含在代码示例中,但在活动中,您将“this”(活动)传递到CustomWebChromeClient的构造函数中,因此我假设CustomWebChromeClient保留了对它的引用。您可以使用该引用来调用活动上所需的实例方法,无论是StartActivity、Finish还是其他您需要的方法。例如:

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
    }
}

就用这个吧

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();
        }
    }
}

就用这个吧

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();
        }
    }
}

如果您需要任何帮助,请发布您的问题…它位于CustomWebChromeClient中。请阅读我的评论。如果你读了,问题就在他的评论中。尝试如下格式化您的Intent/StartActivity:Intent myIntent=newintent(CurrentActivity.this,NextActivity.class);CurrentActivity.this.startActivity(myIntent);如果您需要任何帮助,请发布您的问题…它位于CustomWebChromeClient中。请阅读我的评论。如果你读了,问题就在他的评论中。尝试如下格式化您的Intent/StartActivity:Intent myIntent=newintent(CurrentActivity.this,NextActivity.class);CurrentActivity.this.startActivity(myIntent);