我在Xamarin.Android解决方案中创建了以下片段:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Util;
using Android.Views;
using Android.Widget;
using MvvmCross.Binding.Droid.Views;
using MvvmCross.Binding.Droid.BindingContext;
using ActiveNomad.Droid.Models;
using ActiveNomad.Droid.Settings;
using Android.Support.V4.App;
using MvvmCross.Droid.ViewModels;
namespace ActiveNomad.Droid.Views
{
[Register("activenomad.droid.views.adpagefragment")]
public class AdPageFragment : Fragment
{
ImageView sponsorImageView;
VideoView videoView;
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
}
protected override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
base.OnCreateView (inflater, container, savedInstanceState);
ActiveNomadSettings settings = SettingsManager.Instance.GetSettings ();
sponsorImageView.FindViewById<ImageView> (Resource.Id.sponsorlogo);
Android.Net.Uri imgUri = Android.Net.Uri.Parse (settings.SponsorImagePath);
sponsorImageView.SetImageURI (imgUri);
videoView.FindViewById<VideoView> (Resource.Id.AdVideo);
Android.Net.Uri vidUri = Android.Net.Uri.Parse (settings.AdPageVideoPath);
videoView.SetVideoURI (vidUri);
videoView.Start ();
return inflater.Inflate (Resource.Layout.AdPageView, container, true);
}
}
}
我的活动如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Support.V4.App;
using ActiveNomad.Droid.Views;
namespace ActiveNomad
{
[Activity (Label = "AdPageActivity")]
public class AdPageActivity : FragmentActivity
{
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
var ad = new AdPageFragment{Arguments = new Bundle()};
var fragmentTransaction = SupportFragmentManager.BeginTransaction ();
fragmentTransaction.Add (Android.Resource.Id.Content, ad);
fragmentTransaction.Commit ();
}
}
}
我更改了片段,使OnCreateView
现在成为公共覆盖。但是,抛出了一个新错误:
Error: error: class adpagefragment is public, should be declared in
a file named adpagefragment.java
Xamarin Studio打开java文件但不确定这是否是系统生成的,以及是否应该修改它。该问题应如何解决?
答案 0 :(得分:3)
将OnCreateView
方法从protected
更改为public
。 public
上的Fragment
,如错误消息中所述,您无法将访问修饰符更改为protected
。
public override View OnCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)