在Xamarin.Android中不受欢迎的例外情况

时间:2016-04-27 11:42:03

标签: android xamarin.android

你能帮我解决这个问题吗? 我无法理解这个错误! :(

Error Message

活动代码:

using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;

namespace Phoneword2
{
    [Activity(Label = "Phone Word", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {


        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            EditText phoneNumberText = FindViewById<EditText>(Resource.Id.PhoneNumberText);
            Button translateButton = FindViewById<Button>(Resource.Id.TranslateButton);
            Button callButton = FindViewById<Button>(Resource.Id.CallButton);

            callButton.Enabled = false;

            // Add code to translate number

            string translatedNumber = "";

            translateButton.Click += (object sender, EventArgs e) =>
            {
                // Translate user's alphanumeric phone number to numeric
                translatedNumber = Core.PhonewordTranslator.ToNumber(phoneNumberText.Text);
                if (String.IsNullOrWhiteSpace(translatedNumber))
                {
                    callButton.Text = "Call";
                    callButton.Enabled = false;
                }
                else
                {
                    callButton.Text = "Call " + translatedNumber;
                    callButton.Enabled = true;
                }
            };

            callButton.Click += (object sender, EventArgs e) =>
            {
                // On "Call" button click, try to dial phone number.
                var callDialog = new AlertDialog.Builder(this);
                callDialog.SetMessage("Call " + translatedNumber + "?");
                callDialog.SetNeutralButton("Call", delegate {
                    // Create intent to dial phone
                    var callIntent = new Intent(Intent.ActionCall);
                    callIntent.SetData(Android.Net.Uri.Parse("tel:" +
                    translatedNumber));
                    StartActivity(callIntent);
                });
                callDialog.SetNegativeButton("Cancel", delegate { });
                // Show the alert dialog to the user and wait for response.
                callDialog.Show();
            };

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);

            // Get our button from the layout resource,
            // and attach an event to it


        }
    }
}

`

这是main.axml代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
        android:text="Enter a Phoneword"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textView1" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/PhoneNumberText" />
    <Button
        android:text="Translator"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/TranslateButton" />
    <Button
        android:text="Call"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/CallButton" />
</LinearLayout>

它包含两个按钮和一个纯文本... 您可以单击翻译以更新“呼叫”按钮的文本。 按呼叫按钮时,请拨打目标号码。

1 个答案:

答案 0 :(得分:0)

您实际上需要将OnCreate方法的最后一行设为第二行。换句话说,从以下位置删除/删除以下代码行

SetContentView(Resource.Layout.Main);

并在base.OnCreate (bundle);

之后粘贴它

基本上,在调用父OnCreate之后,您需要做的第一件事就是告诉活动要使用哪个布局。一旦活动知道,只有这样它才能在该特定布局中找到控件。

因此,您的OnCreate方法现在变为:

protected override void OnCreate(Bundle bundle)
{
    base.OnCreate(bundle);

    // Set our view from the "main" layout resource
    SetContentView(Resource.Layout.Main);

    EditText phoneNumberText = FindViewById<EditText>(Resource.Id.PhoneNumberText);
    Button translateButton = FindViewById<Button>(Resource.Id.TranslateButton);
    Button callButton = FindViewById<Button>(Resource.Id.CallButton);

    callButton.Enabled = false;

    // Removed code for brevity //
}