我在使用Xamarin点击列表视图中的textview字段时遇到错误尝试发送电子邮件,所有这些都与活动实例无关,无法调用电子邮件Intent。
我尝试了不同的方法,但根本没有成功。 首先我试着在活动clas中调用一个方法(我在适配器中使用了一个方法,但它不起作用); 所以我改变了尝试调用textview click事件。在java中,似乎可以使用表达式轻松完成:
if (mContext instanceof MyActivity) { methodCall() };
在C#中,我尝试使用
if (mContext.GetType() == TypeOf(ListaVereadoresActivity))
但这不起作用,
此外,下面的代码中提供的解决方案,尝试调用StartActivity(email);
错误:未处理的异常: Java.Lang.Exception:找不到处理Intent的Activity { act = android.intent.action.SEND typ = message / rfc822 flg = 0x10000000(has 额外的)}
代码样本:
[Activity(Label = "Vereadores")]
public class ListaVereadoresActivity : Activity
{
List<Vereador> listaVer = new List<Vereador>();
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.listaVereadores_layout);
GenerateListaVereadores();
ListView vereadorListView = FindViewById<ListView>(Resource.Id.lstVereadores2);
vereadorListView.ItemClick += OnListItemClick;
VereadorAdapter adapter = new VereadorAdapter(this, listaVer, this.ApplicationContext);
vereadorListView.Adapter = adapter;
}
//不再使用了
public void SendEmail(String emailAdd)
{
var email = new Intent(Android.Content.Intent.ActionSend);
email.SetType("message/rfc822");
email.PutExtra(Android.Content.Intent.ExtraEmail, new string[] { emailAdd });
StartActivity(email);
}
private void GenerateListaVereadores()
{
var azuaite = new Vereador();
azuaite.Nome = "Azuaite";
azuaite.Foto = Resource.Drawable.azuaite;
azuaite.Email = "some@email.address";
listaVer.Add(azuaite);
var aleksander = new Vereador();
aleksander.Nome = "Aleksander";
aleksander.Foto = Resource.Drawable.aleksander;
aleksander.Email = "some@email.address";
listaVer.Add(aleksander);
var dhony = new Vereador();
dhony.Nome = "dhony";
dhony.Foto = Resource.Drawable.dhony;
dhony.Email = "other@email.address";
listaVer.Add(dhony);
}
void OnListItemClick(object sender, AdapterView.ItemClickEventArgs e)
{
var listView = sender as ListView;
var t = listaVer[e.Position];
Toast.MakeText(this, t.Nome, ToastLength.Short).Show();
}
我的自定义适配器:
public class VereadorAdapter : BaseAdapter
{
private List<Vereador> vereadorList;
Activity activity;
protected Context mContext;
public VereadorAdapter(Activity act, List<Vereador> v, Context appContext)
{
activity = act;
vereadorList = v;
mContext = appContext;
}
public override int Count
{
get { return vereadorList.Count; }
}
class ViewHolder : Java.Lang.Object
{
public TextView nomeVereadorTxt { get; set; }
public TextView emailVereadorTxt { get; set; }
public ImageView fotoVereadorImg { get; set; }
}
public override Java.Lang.Object GetItem(int position)
{
var vereador = (Vereador)vereadorList[position];
return vereador;
}
public override long GetItemId(int position)
{
return 0;
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
var item = vereadorList[position];
ViewHolder holder;
var view = convertView;
if (view != null)
{
holder = view.Tag as ViewHolder;
}
else
{
holder = new ViewHolder();
view = activity.LayoutInflater.Inflate(Resource.Layout.CustomView, parent, false);
holder.nomeVereadorTxt = view.FindViewById<TextView>(Resource.Id.VereadorNome);
holder.fotoVereadorImg = view.FindViewById<ImageView>(Resource.Id.VereadorFoto);
holder.emailVereadorTxt = view.FindViewById<TextView>(Resource.Id.VereadorEmail);
view.Tag = holder;
}
//setar as propriedades dos componentes
holder.emailVereadorTxt.Text = item.Nome;
holder.fotoVereadorImg.SetImageResource(item.Foto);
holder.emailVereadorTxt.Text = item.Email;
holder.emailVereadorTxt.Click += EmailVereadorTxt_Click;
return view;
}
private void EmailVereadorTxt_Click(object sender, EventArgs e)
{
TextView tv = (sender as TextView);
string mailAddress = tv.Text;
Toast.MakeText(activity.BaseContext, mailAddress, ToastLength.Short).Show();
var email = new Intent(Android.Content.Intent.ActionSend);
email.AddFlags(ActivityFlags.NewTask);
email.SetType("message/rfc822");
email.PutExtra(Android.Content.Intent.ExtraEmail, new string[] { mailAddress });
Application.Context.ApplicationContext.StartActivity(email);
}
}
这是我的自定义视图:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:background="@drawable/CustomSelector">
<ImageView
android:id="@+id/VereadorFoto"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:src="@drawable/Icon" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/VereadorNome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:paddingTop="5dp" />
<TextView
android:id="@+id/VereadorEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:paddingTop="5dp" />
</LinearLayout>
</LinearLayout>
那么,通过点击电子邮件文本来实现发送电子邮件操作的正确方法是什么?
答案 0 :(得分:0)
我认为你可能误解了你所得到的错误。
当您尝试在Android上启动活动时,您会发送一个意图,操作系统会尝试以最佳方式响应该意图。如果您设置了一个特定活动子类的意图,那么它将打开您的活动子类。但是,如果您设置了一个像动作一样更通用的意图,则将其留给操作系统的解释。通常,这意味着它会尝试查找已经注册为能够处理该动作意图的活动。
在这种情况下,您正在使用ACTION_SEND启动活动,并且操作系统发出错误,因为它无法找到任何已注册为能够处理ACTION_SEND的活动。通常这意味着您运行的设备(或模拟器)没有安装电子邮件应用。
如果要首先检查操作系统以查看它是否可以找到intent的活动,则应使用resolveActivity(intent),如果找不到,则返回null。
希望这有帮助。