StartActivityForResult に関して問題があります。Web サービスからのフォームがあり、カムを開いて写真を撮り、ビューに表示して送り返す必要があるフィールドが表示されることがあります。
問題は、StartActivityForResult で常に意味のない Java.Lang.NullPointerException エラーが発生することです。
そして、ここにコードがあります
PrevzemVozila.cs
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 System.Data;
using Android.Content.PM;
using Android.Provider;
namespace UUIDev
{
[Activity(Label = "Prevzem vozila v servis", NoHistory = true, Icon="@drawable/Icon", ConfigurationChanges = Android.Content.PM.ConfigChanges.Orientation)]
public class PrevzemVozila : Activity
{
si.rcg.hat._hat_Tablet chk = new si.rcg.hat._hat_Tablet();
Java.IO.File _file;
PackageManager _pm;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Prevzem);
_pm = this.PackageManager;
//Window.SetSoftInputMode(SoftInput.StateAlwaysVisible);
var Ticket = Intent.GetStringExtra("sentTicket") ?? "";
var FormIdStr = Intent.GetStringExtra("sentFormId") ?? "";
var EventNoStr = Intent.GetStringExtra("sentEventNo") ?? "";
int FormId = int.Parse(FormIdStr);
int EventNo = int.Parse(EventNoStr);
DataSet GetSingleEvent = new DataSet();
GetSingleEvent = chk.GetSingleEvent(Ticket, FormId, EventNo);
IList<Prevzem> listPrevzem = new JavaList<Prevzem>();
var lstPrevzem = FindViewById<ListView>(Resource.Id.lstPrevzem);
var prevzemAdapter = new PrevzemAdapter(this, Resource.Layout.ListPrevzem, new JavaList<Prevzem>());
lstPrevzem.Adapter = prevzemAdapter;
foreach (DataRow row in GetSingleEvent.Tables[0].Rows)
{
var item = new Prevzem();
item.IdRec = row["IdRec"].ToString();
if (item.IdRec.Equals(""))
item.IdRec = null;
item.FieldId = row["FieldId"].ToString();
if (item.FieldId.Equals(""))
item.FieldId = null;
item.FieldDesc = row["FieldDesc"].ToString();
if (item.FieldDesc.Equals(""))
item.FieldDesc = null;
item.FieldType = row["FieldType"].ToString();
if (item.FieldType.Equals(""))
item.FieldType = null;
item.isReadOnly = row["isReadOnly"].ToString();
if (item.isReadOnly.Equals(""))
item.isReadOnly = null;
item.FieldValue = row["FieldValue"].ToString();
if (item.FieldValue.Equals(""))
item.FieldValue = null;
item.FieldTextValue = row["FieldTextValue"].ToString();
if (item.FieldTextValue.Equals(""))
item.FieldTextValue = null;
item.OptBox_Options = row["OptBox_Options"].ToString();
if (item.OptBox_Options.Equals(""))
item.OptBox_Options = null;
listPrevzem.Add(item);
RunOnUiThread(() => {prevzemAdapter.Add(item);});
}
}
public override void OnConfigurationChanged(Android.Content.Res.Configuration newConfig)
{
base.OnConfigurationChanged(newConfig);
}
public void OnGetCam(Intent _intent, IList<ResolveInfo> _avalableActivities)
{
if (_avalableActivities != null && _avalableActivities.Count > 0)
{
var dir = new Java.IO.File(
Android.OS.Environment.GetExternalStoragePublicDirectory(
Android.OS.Environment.DirectoryPictures), "RCG_Foto");
if (!dir.Exists())
{
dir.Mkdirs();
}
_file = new Java.IO.File(dir, String.Format("avto_{0}.jpg", Guid.NewGuid()));
_intent.PutExtra(MediaStore.ExtraOutput, Android.Net.Uri.FromFile(_file));
this.StartActivityForResult(_intent, 5);
}
}
protected override void OnActivityResult(int requestCode, Result resultCode, Android.Content.Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
var imageView = FindViewById<ImageView>(Resource.Id.imageSending);
// make it available in the gallery
var mediaScanIntent = new Intent(Intent.ActionMediaScannerScanFile);
var contentUri = Android.Net.Uri.FromFile(_file);
mediaScanIntent.SetData(contentUri);
SendBroadcast(mediaScanIntent);
// display in ImageView
var fotka = MediaStore.Images.Media.GetBitmap(ContentResolver, contentUri);
imageView.SetImageBitmap(fotka);
fotka.Dispose();
}
}
}
PrevzemAdapter.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using System.Data;
using Android.Text;
using Android.Content.PM;
using Android.Provider;
namespace UUIDev
{
[Activity(Label = "Prevzem Adapter")]
public class PrevzemAdapter : ArrayAdapter<Prevzem>
{
int resource;
PrevzemVozila pa;
Intent availIntent;
IList<ResolveInfo> availableActivities;
Context p_context = null;
//Initialize adapter
public PrevzemAdapter(Context context, int resource, IList<Prevzem> items)
: base(context, resource, items)
{
this.resource = resource;
this.p_context = context;
this.AvailIntent = new Intent(MediaStore.ActionImageCapture);
this.AvailActivities = p_context.PackageManager.QueryIntentActivities(AvailIntent, PackageInfoFlags.MatchDefaultOnly);
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
LinearLayout view;
//Get our items
var item = GetItem(position);
//Inflate the view
if (convertView == null)
{
view = new LinearLayout(Context);
var inflater = Context.LayoutInflaterService;
LayoutInflater vi;
vi = (LayoutInflater)Context.GetSystemService(inflater);
vi.Inflate(resource, view, true);
}
else
{
view = (LinearLayout)convertView;
}
var FieldDesc = view.FindViewById<TextView>(Resource.Id.txtFieldDesc);
FieldDesc.Text = item.FieldDesc != null ? item.FieldDesc : "";
var IdRec = view.FindViewById<TextView>(Resource.Id.txtIdRec);
IdRec.Text = item.IdRec != null ? item.IdRec : "";
var FieldId = view.FindViewById<TextView>(Resource.Id.txtFieldId);
FieldId.Text = item.FieldId != null ? item.FieldId : "";
var FieldTextValue = view.FindViewById<EditText>(Resource.Id.editVal);
FieldTextValue.Text = item.FieldTextValue != null ? item.FieldTextValue : "";
var ButtonValue = view.FindViewById<Button>(Resource.Id.button1);
ButtonValue.Visibility = view.Visibility == ViewStates.Gone ? ViewStates.Gone : ViewStates.Gone;
var CamImage = view.FindViewById<ImageView>(Resource.Id.imageSending);
CamImage.Visibility = view.Visibility == ViewStates.Gone ? ViewStates.Gone : ViewStates.Gone;
var SpinnerValue = view.FindViewById<Spinner>(Resource.Id.spinnerVal);
bool isReadOnly = bool.Parse(item.isReadOnly);
if (isReadOnly == true)
{
FieldTextValue.Enabled = false;
SpinnerValue.Enabled = false;
}
else
{
FieldTextValue.Enabled = true;
SpinnerValue.Enabled = true;
}
// if field is TextBOX_INT
if (item.FieldType == "TextBOX_INT")
{
FieldTextValue.InputType = InputTypes.ClassNumber;
}
// if field is TextBOX or TextBOX_INT or TextAREA
if (item.FieldType == "TextBOX" || item.FieldType == "TextBOX_INT" || item.FieldType == "TextAREA")
{
FieldTextValue.SetSingleLine(true);
FieldTextValue.Visibility = view.Visibility == ViewStates.Visible ? ViewStates.Visible : ViewStates.Visible;
SpinnerValue.Visibility = view.Visibility == ViewStates.Gone ? ViewStates.Gone : ViewStates.Gone;
}
// if field is TextAREA
if (item.FieldType == "TextAREA")
{
FieldTextValue.InputType = InputTypes.ClassText;
FieldTextValue.SetSingleLine(false);
}
// if field is ComboBOX or OptionBox
if (item.FieldType == "ComboBOX" || item.FieldType == "OptionBOX")
{
FieldTextValue.Visibility = view.Visibility == ViewStates.Gone ? ViewStates.Gone : ViewStates.Gone;
SpinnerValue.Visibility = view.Visibility == ViewStates.Visible ? ViewStates.Visible : ViewStates.Visible;
List<string> entries = new List<string>();
String rawXML = item.OptBox_Options;
StringReader stream = null;
XmlTextReader reader = null;
DataSet xmlDS = new DataSet();
stream = new StringReader(rawXML);
// Load the XmlTextReader from the stream
reader = new XmlTextReader(stream);
xmlDS.ReadXml(reader);
DataSet myOPTvalues = new DataSet();
myOPTvalues = xmlDS;
foreach (DataRow row in myOPTvalues.Tables[0].Rows)
{
var optItem = new PrevzemSpin();
optItem.FieldValue = row["FieldValue"].ToString();
if (optItem.FieldValue.Equals(""))
optItem.FieldValue = null;
optItem.FieldTextValue = row["FieldTextValue"].ToString();
if (optItem.FieldTextValue.Equals(""))
optItem.FieldTextValue = null;
entries.Add(optItem.FieldTextValue);
SpinnerValue.Tag = optItem.FieldValue;
}
var adapter = new ArrayAdapter<string>(p_context, Android.Resource.Layout.SelectDialogItem, entries);
adapter.SetDropDownViewResource(Android.Resource.Layout.SelectDialogSingleChoice);
SpinnerValue.Adapter = adapter;
if (SpinnerValue.Adapter.Count.Equals(2))
{
SpinnerValue.SetSelection(1);
}
}
// if field is Picture_REQ
//if (item.FieldType == "Picture_REQ")
// {
ButtonValue.Visibility = view.Visibility == ViewStates.Visible ? ViewStates.Visible : ViewStates.Visible;
CamImage.Visibility = view.Visibility == ViewStates.Visible ? ViewStates.Visible : ViewStates.Visible;
FieldTextValue.Visibility = view.Visibility == ViewStates.Gone ? ViewStates.Gone : ViewStates.Gone;
SpinnerValue.Visibility = view.Visibility == ViewStates.Gone ? ViewStates.Gone : ViewStates.Gone;
ButtonValue.Click += delegate
{
pa = new PrevzemVozila();
if (pa != null)
{
pa.OnGetCam(this.AvailIntent, this.AvailActivities);
}
};
//}
return view;
}
public IList<ResolveInfo> AvailActivities
{
set { this.availableActivities = value; }
get { return this.availableActivities; }
}
public Intent AvailIntent
{
set { this.availIntent = value; }
get { return this.availIntent; }
}
}
}