android - 'MyFirstApp.Resource.Id' 不包含 'myButton' (CS0117) (MyFirstApp) 的定义

标签 android android-layout xamarin

主要 Activity 我将我的项目命名为“MyFirstApp”

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

namespace MyFirstApp
{
    [Activity (Label = "MyFirstApp", MainLauncher = true)]
    public class MainActivity : Activity
    {
        int count = 1;

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

            // 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
            Button button = FindViewById<Button> (Resource.Id.myButton);

            button.Click += delegate {
                button.Text = string.Format ("{0} clicks!", count++);
            };
        }
    }
}

布局 res/布局

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
    <EditText
        android:id="@+id/edit_message"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:hint="Enter Message" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Send"
        android:id="@+id/button_send" />
</LinearLayout>

字符串

    <?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">My First App</string>
    <string name="edit_message">Enter a message</string>
    <string name="button_send">Send</string>
    <string name="action_settings">Settings</string>
    <string name="title_activity_main">MainActivity</string>
</resources>

请帮助我,我是 android 开发的初学者 如果有什么不清楚我会尝试添加更多细节 我正在使用 Xamarin 进行开发

谢谢

最佳答案

添加属性:

android:id="@+id/myButton"

到布局 xml 文件中的按钮。

然后在您的 Activity 中,您可以找到这样的按钮:

Button button = (Button) findViewById(R.id.myButton);

代替:

Button button = FindViewById<Button> (Resource.Id.myButton);

更新:好吧,我提出的 findViewById 是普通的 Android,不确定它在 Xamarin 中的工作方式,但可以肯定的是你的按钮需要 xml 中的 id

更新 2:实际上您的按钮已经有了 ID,但它不是 myButton,它是 button_send ==> 所以您可能想使用那个 myButton

关于android - 'MyFirstApp.Resource.Id' 不包含 'myButton' (CS0117) (MyFirstApp) 的定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22465625/

相关文章:

java - RecyclerView Item 显示只有一个背景的文本

android - 2.3 崩溃应用程序中的 fragment

Android:以编程方式在表格中创建两列

android - 为什么我的 Xamarin Forms 应用程序位于 Android 4.4 - API 19 (Samsung Galaxy S3) 中的所有其他应用程序之上?

Android ICS 网络位置未更新

android - 有人可以向我推荐适用于 android 的 svg 库吗?

java - 没有显示 RSS 数据

android - layout_weight =".4"看起来比 layout_weight =".6"大

c# - MvvmCross - MvxListView绑定(bind)多次点击

c# - 当我的代码不存在时,我如何让我的代码创建一个 sqlite 数据库?