从子类TextView中创建一个定制的Andr​​oid UI元素时避免一个NullReferenceException [英] Avoiding a NullReferenceException when creating a custom Android UI element from subclassed TextView

查看:298
本文介绍了从子类TextView中创建一个定制的Andr​​oid UI元素时避免一个NullReferenceException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一对夫妇在这里首创的 - 使用MonoDroid的第一款Android应用程序,并第一次(我有很多与C#.NET体验)。

A couple of firsts here - first Android app and first time using MonoDroid (I've got lots of experience with C# .NET).

在我的用户界面我想提请围绕一个TextView边框,发现一个帖子上SO(<一个href=\"http://stackoverflow.com/questions/2026873/android-way-to-appear-bordered-text-on-the-textview\">2026873)所推荐子类的TextView。我也发现了另一篇文章(<一个href=\"http://stackoverflow.com/questions/2695646/declaring-a-custom-android-ui-element-using-xml\">2695646)与使用XML声明定制的Andr​​oid UI元素一些额外的信息。(注:在本例中发布所有code是在Java中,必须翻译成C#/ MonoDroid的环境)

In my user interface I want to draw a border around a TextView and found a post on SO (2026873) that recommended subclassing TextView. I also found another post (2695646) with some additional info on declaring a custom Android UI element using XML. (Note: All code in the example posts were in Java, had to translate into the C#/MonoDroid environment.)

当我运行仿真器中的code我得到一个System.NullReferenceException:对象不设置到对象的实例。

When I run the code in the emulator I get a System.NullReferenceException: Object reference not set to an instance of an object.

下面是我出的现成活动1 code和code的子类的TextView。

Here's my out-of-the-box Activity1 code and the code for the subclassed TextView.

namespace MBTA
{
    [Activity(Label = "MBTA", MainLauncher = true, Icon = "@drawable/icon")]
    public class Activity1 : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.Main);
        }
    }

    public class BorderedTextView : TextView
    {
        public BorderedTextView(Context context, IAttributeSet attrs, int defStyle) : base(context, attrs, defStyle) { }
        public BorderedTextView(Context context, IAttributeSet attrs) : base(context, attrs) { }
        public BorderedTextview(Context context) : base(context) { }

        protected override void OnDraw (Android.Graphics.Canvas canvas)
        {
            base.OnDraw (canvas);

            Rect rect = new Rect();
            Paint paint = new Paint();

            paint.SetStyle(Android.Graphics.Paint.Style.Stoke);
            paint.Color = Android.Graphics.Color.White;
            paint.StrokeWidth = 3;

            GetLocalVisibleRect(rect);
            canvas.DrawRect(rect, paint);
        }
    }
}

我Main.axml布局如下:

My Main.axml layout is as follows:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res/MBTA"     
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1">
        <MBTA.BorderedTextView
            android:text="DATE"
            android:textSize="15pt"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal|center_vertical"
            android:layout_weight="1"/>
    </LinearLayout>
</LinearLayout>

和我attrs.xml文件如下(与它的BuildAction的设置为AndroidResource):

And my attrs.xml file is as follows (with its BuildAction set to AndroidResource):

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="BorderedTextView">
        <attr name="android:text"/>
    <attr name="android:textSize"/>
    <attr name="android:layout_width"/>
    <attr name="android:layout_height"/>
    <attr name="android:gravity"/>
    <attr name="android:layout_weight"/>
    </declare-styleable>
</resources>

先谢谢了。

推荐答案

单为Android产生了Java的 Android的调用包装使用由Android和布局的XML文件。

Mono for Android lowercases the namespace name when generating the Java Android Callable Wrappers for use by Android and layout XML files.

结果(如的previous答案),你需要使用 mbta.BorderedTextView ,而不是 MBTA.BorderedTextView

Consequently (as noted in the previous answer), you need to use mbta.BorderedTextView, not MBTA.BorderedTextView.

Mono的为Android文档铁饼在布局使用自定义的意见,其中涉及本场景。

The Mono for Android documentation discuses using custom views in a layout, which addresses this scenario.

这篇关于从子类TextView中创建一个定制的Andr​​oid UI元素时避免一个NullReferenceException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆