c# - 我正在尝试使单声道 android 简单 "hello world"但我在模拟器中看不到 "hello world"为什么?

标签 c# android mono xamarin.android

我卸载了android ask并重新安装到没有空格的c:\androidSdktools。 所以现在模拟器可以工作了,我可以看到模拟器了。

但尝试了一些“hello world”文本的代码,当我运行应用程序和运行模拟器时,我看到了 android 模拟器,但我看不到 wnyhere“hello world”。 我不确定我是否没有做任何好事,或者可能是因为我在退出模拟器并停止调试时仍然遇到错误,我得到错误说:部署错误。如果我继续说"is",那么他会要求我在运行/调试之前重建/构建我的解决方案,但我确实这样做了,但它没有解决这个错误。

我不知道这个错误是否与我在 android 模拟器中没有看到任何“hello world”文本这一事实有关。

我现在尝试编写这段代码:

using System;

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

namespace MonoAndroidApplication1
{
    [Activity(Label = "MonoAndroidApplication1", MainLauncher = true, Icon = "@drawable/icon")]
    public class Activity1 : 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++); };
        }
    }
}

我有 visual studio c# 2010 pro。

我试过了,这是当我选择制作新的 android mono 应用程序时自动创建的代码。

但我在模拟器中没有看到任何文本。我尝试将此代码添加为:

using System;

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

namespace MonoAndroidApplication1
{
    [Activity(Label = "MonoAndroidApplication1", MainLauncher = true, Icon = "@drawable/icon")]
    public class Activity1 : Activity
    {
        int count = 1;
        TextView tv;

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

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);
            tv = new TextView(this);
            tv.Text = "hello world";
            SetContentView(tv);
            // 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++); };
        }
    }
}

再一次,当运行它时,我在模拟器中没有看到任何文本“hello world” 并且在退出模拟器并停止调试/运行应用程序时仍然出现此部署错误。模拟器现在运行正常,但退出时仍然出现此错误。

为什么我在模拟器上看不到文本“hello world”?

谢谢。

最佳答案

通过第二次调用 SetContentView 更改布局后,布局中不再有 Button,因此您的 FindViewById 返回 null。那么您正试图在空引用上设置事件。

查看如何读取错误日志以便找到错误:

http://android.xamarin.com/Documentation/Guides/Android_Debug_Log

关于c# - 我正在尝试使单声道 android 简单 "hello world"但我在模拟器中看不到 "hello world"为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6907109/

相关文章:

c# - 如何将 "resource principal named https://management.azure.net"添加到 Azure 中的默认目录?

c# - 当源是 https uri 时如何播放 wpf MediaElement

iphone - 是否有适用于 Android 或 iPhone 的图像效果库(例如 lomo)?

java - 我希望在 android 期间一遍又一遍地检查它,直到它在 android 上等于

c# - Monodevelop 和 Gtk Treeview

c# - 更改 Action 的线程优先级

c# - Reporting Services - 在 C# WinForm 中呈现到 Excel

fragment 中的 Android Camera 预览

macos - 在Mono资源访问错误上运行ASP.NET

c# - Xamarin 表格 : Default Image if Url not found