我是xamarin android的初学者。我想使用我的设备相机拍照,我使用以下代码来捕捉图像但是,我在运行代码时遇到异常
这是我的Fragment.CS
public class ScanFragments : Fragment
{
ImageView imageView;
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
}
public static ScanFragments NewInstance()
{
var scan = new ScanFragments { Arguments = new Bundle() };
return scan;
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
var ignored = base.OnCreateView(inflater, container, savedInstanceState);
imageView =View.FindViewById<ImageView>(Resource.Id.Iv_ScanImg);
var btnCamera =View. FindViewById<Button>(Resource.Id.btnCamera);
btnCamera.Click += BtnCamera_Click;
return inflater.Inflate(Resource.Layout.ScanLayout, null);
}
private void BtnCamera_Click(object sender, EventArgs e)
{
Intent intent = new Intent(MediaStore.ActionImageCapture);
StartActivityForResult(intent, 0);
}
}
答案 0 :(得分:1)
好吧,现在我看到你的错误实际上非常简单,因为以下原因你得到了空引用
1.OnCreateView基本上不包含View的定义我的意思是View是一个用作该类对象的类。
2.我推荐使用片段的实际方法是首先在OnCreateView上将XML或AXML定义为其前景视图,如下所示:
return inflater.Inflate(Resource.Layout.ScanLayout, null);
3.Override OnViewCreated方法并使用其View来完成所有工作,如下所示:
public override void OnViewCreated(View view, Bundle savedInstanceState)
{
base.OnViewCreated(view, savedInstanceState);
imageView =view.FindViewById<ImageView>(Resource.Id.Iv_ScanImg);
var btnCamera =view. FindViewById<Button>(Resource.Id.btnCamera);
btnCamera.Click += BtnCamera_Click;
}
4.有关Android活动和片段生命周期检查的详细信息here
古德勒克!