Java RunOnUiThread无法加载,需要对象引用(android)
我有两门课,一门叫做“活动1”,第二门叫做“JS2CS”。在JS2C中,我试图更新我在Activity1中声明的android小部件(SwipeRefreshLayout),我需要小部件是静态的(以防万一这很重要) 以下是活动1的简短版本Java RunOnUiThread无法加载,需要对象引用(android),java,c#,android,xamarin,Java,C#,Android,Xamarin,我有两门课,一门叫做“活动1”,第二门叫做“JS2CS”。在JS2C中,我试图更新我在Activity1中声明的android小部件(SwipeRefreshLayout),我需要小部件是静态的(以防万一这很重要) 以下是活动1的简短版本 public class Activity1 : Activity { public static SwipeRefreshLayout refresher; protected override void OnCreate (B
public class Activity1 : Activity
{
public static SwipeRefreshLayout refresher;
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
// The refresher - SwipeRefreshLayout
refresher = FindViewById<SwipeRefreshLayout> (Resource.Id.refresher);
refresher.SetColorScheme (Resource.Color.xam_dark_blue,
Resource.Color.xam_purple,
Resource.Color.xam_gray,
Resource.Color.xam_green);
refresher.Refresh += HandleRefresh;
}
}
公共类活动1:活动
{
公共静态布局刷新器;
创建时受保护的覆盖无效(捆绑包)
{
base.OnCreate(bundle);
//从“主”布局资源设置视图
SetContentView(Resource.Layout.Main);
//刷新器-刷新布局
refresher=findviewbyd(Resource.Id.refresher);
refresher.SetColorScheme(Resource.Color.xam_深蓝,
Resource.Color.xam_purple,
Resource.Color.xam_gray,
Resource.Color.xam_green);
Refresh.Refresh+=HandlerRefresh;
}
}
这是第二类JS2C
public class JS2CS : Java.Lang.Object, Java.Lang.IRunnable
{
Context context;
public JS2CS (Context context)
{
this.context = context;
}
public void Run ()
{
Toast.MakeText (context, "true", ToastLength.Short).Show ();
Activity1.RunOnUiThread (() => refresher.Enabled = false); // <-- error !
}
}
公共类JS2CS:Java.Lang.Object、Java.Lang.iRunTable
{
语境;
公共JS2CS(上下文)
{
this.context=上下文;
}
公开作废运行()
{
Toast.MakeText(上下文“true”,ToastLength.Short).Show();
Activity1.RunOnUiThread(()=>refresh.Enabled=false);//
非静态字段、方法或对象需要对象引用
“财产”
因为这里的Activity1.RunOnUiThread
您试图从Activity
访问非静态方法RunOnUiThread
,而不使用实例
而不是从Activity1
参数化JS2CS
类构造函数以静态方式创建对象或访问视图以获取所有值:
Activity activity;
SwipeRefreshLayout refresher
public JS2CS (Context context,Activity activity,
SwipeRefreshLayout refresher)
{
this.context = context;
this.activity=activity;
this.refresher=refresher;
}
现在将RunOnUiThread调用为:
activity.RunOnUiThread (() => refresher.Enabled = false);
在活动中,将JS2CS
对象传递到AddJavascriptInterface
方法,如下所示:
web_view.AddJavascriptInterface (new JS2CS (this,this,refresher),"JS2CS");
您在onCreate中使用JS2CS
的位置?它在同一个活动中,我称之为Web视图的java库。我将更新此帖子以显示它。更新。谢谢!这很有效,很好的回答并解释了错误的原因,感谢您,我今天肯定学到了一些新东西。
web_view.AddJavascriptInterface (new JS2CS (this,this,refresher),"JS2CS");