我有一个滑动标签界面,包含2个布局。 在第二个布局上,我有一个按钮,必须启动一个活动,将内容视图设置为新的布局。 我尝试启动活动时收到以下错误:
非静态字段,方法或属性'Fragment.StartActivity(Intent)'需要对象引用
ViewPager类如下所示:
public class SamplePagerAdapter : PagerAdapter { List<string> items = new List<string>(); //This list called items contains the items that we add below public SamplePagerAdapter() : base() //This method creates the tabs on top { items.Add("Productivity Optimization"); items.Add("Remote Health Monitoring"); } public override int Count { get { return items.Count; } //This method returns the count of the tabs } public override bool IsViewFromObject(View view, Java.Lang.Object obj) { return view == obj; } public override Java.Lang.Object InstantiateItem(ViewGroup container, int position) { View view = LayoutInflater.From(container.Context).Inflate(Resource.Layout.PO, container, false); //***Original code*** //View view = LayoutInflater.From(container.Context).Inflate(Resource.Layout.pager_item, container, false); //container.AddView(view); //TextView txtTitle = view.FindViewById<TextView>(Resource.Id.item_title); //int pos = position + 1; //txtTitle.Text = pos.ToString(); //return view; //***end of original code*** //This is where you set which layout to display in which order if (position == 0) { view = LayoutInflater.From(container.Context).Inflate(Resource.Layout.PO, container, false); container.AddView(view); } if (position == 1) { view = LayoutInflater.From(container.Context).Inflate(Resource.Layout.RHM, container, false); container.AddView(view); //New test button that should start the web browser activity var btnRep = (Button)view.FindViewById(Resource.Id.btnReport); btnRep.Click += BtnRep_Click; } return view; } }发生错误的按钮单击方法如下所示:(错误与StartActivity )
private void BtnRep_Click(object sender, EventArgs e) { var BrowserIntent = new Intent(Application.Context,typeof(ReportBrowserActivity)); **StartActivity(BrowserIntent);** }我知道这个问题有很多类似的问题和答案,但我是Xamarin的新手,我似乎无法做到这一点。
任何帮助或推动正确的方向将不胜感激。
I have a sliding tab interface that consists of 2 layouts. On the second layout I have a button that must start an activity that will set the content view to a new layout. I get the following error when I try to start the activity:
An object reference is required for the non-static field, method, or property 'Fragment.StartActivity(Intent)'
The ViewPager class looks like this:
public class SamplePagerAdapter : PagerAdapter { List<string> items = new List<string>(); //This list called items contains the items that we add below public SamplePagerAdapter() : base() //This method creates the tabs on top { items.Add("Productivity Optimization"); items.Add("Remote Health Monitoring"); } public override int Count { get { return items.Count; } //This method returns the count of the tabs } public override bool IsViewFromObject(View view, Java.Lang.Object obj) { return view == obj; } public override Java.Lang.Object InstantiateItem(ViewGroup container, int position) { View view = LayoutInflater.From(container.Context).Inflate(Resource.Layout.PO, container, false); //***Original code*** //View view = LayoutInflater.From(container.Context).Inflate(Resource.Layout.pager_item, container, false); //container.AddView(view); //TextView txtTitle = view.FindViewById<TextView>(Resource.Id.item_title); //int pos = position + 1; //txtTitle.Text = pos.ToString(); //return view; //***end of original code*** //This is where you set which layout to display in which order if (position == 0) { view = LayoutInflater.From(container.Context).Inflate(Resource.Layout.PO, container, false); container.AddView(view); } if (position == 1) { view = LayoutInflater.From(container.Context).Inflate(Resource.Layout.RHM, container, false); container.AddView(view); //New test button that should start the web browser activity var btnRep = (Button)view.FindViewById(Resource.Id.btnReport); btnRep.Click += BtnRep_Click; } return view; } }And the button click method where the error occurs looks like this: (The error is with StartActivity)
private void BtnRep_Click(object sender, EventArgs e) { var BrowserIntent = new Intent(Application.Context,typeof(ReportBrowserActivity)); **StartActivity(BrowserIntent);** }I am aware that there are a lot of similar questions and answers here for this problem, but I am new to Xamarin and I just can't seem to get this right.
Any help or a nudge in the right direction will be appreciated.
最满意答案
需要一个对象引用来调用它的(非静态)方法。 使用以下代码创建引用:
var myObject = new myObject()您可以通过SamplePagerAdapter的构造函数传递此引用。 我假设您正在Fragment创建SamplePagerAdapter (因为OnViewCreated在Activity不可用)
mViewPager.Adapter = new SamplePagerAdapter(Activity);然后构造函数将如下所示:
private Activity _activity; public SamplePagerAdapter(Activity activity) : base() //This method creates the tabs on top { _activity = activity; items.Add("Productivity Optimization"); items.Add("Remote Health Monitoring"); }An object reference is needed in order to call it's (non-static) methods. Creating a reference is done with the following code:
var myObject = new myObject()You can pass this reference through the constructor of SamplePagerAdapter. I'm assuming you're creating the SamplePagerAdapter within a Fragment ( since OnViewCreated isn't available in an Activity )
mViewPager.Adapter = new SamplePagerAdapter(Activity);The constructor will then look like:
private Activity _activity; public SamplePagerAdapter(Activity activity) : base() //This method creates the tabs on top { _activity = activity; items.Add("Productivity Optimization"); items.Add("Remote Health Monitoring"); }更多推荐
发布评论