如何处理xamarin.android中按钮处理程序中的未处理(nullexception)异常 [英] How to handle unhandled(nullexception) exception in button handlers in xamarin.android

查看:100
本文介绍了如何处理xamarin.android中按钮处理程序中的未处理(nullexception)异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个示例Android应用程序来测试一下,创建一个按钮处理程序的错误如下,虽然我的构建成功。



请帮助



错误:未处理的异常:System.NullReferenceException:未将对象引用设置为对象的实例。发生了



代码如下:



使用Android.App; 
使用Android.Widget;
使用Android.OS;

名称空间Android_Picture
{
[活动(标签=Android图片,MainLauncher = true,图标=
@ drawable / icon)]
公共类MainActivity:Activity
{
Button ButtonPrev;
Button ButtonNext;
TextView TextTitle;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);

//从主布局资源设置我们的视图
// SetContentView(Resource.Layout.Main);

ButtonPrev = FindViewById< Button>(Resource.Id.buttonPrev);
ButtonNext = FindViewById< Button>(Resource.Id.buttonNext);
TextTitle = FindViewById< TextView>(Resource.Id.textTitle);

ButtonPrev.Click + = ButtonPrev_Click; //错误
ButtonNext.Click + = ButtonNext_Click;
}

private void ButtonNext_Click(object sender,System.EventArgs e)
{
TextTitle.Text =Next Clicked;
//抛出新的System.NotImplementedException();
}

private void ButtonPrev_Click(object sender,System.EventArgs e)
{
TextTitle.Text =Previous Clicked;
//抛出新的System.NotImplementedException();
}
}
}









我的Main.axml如下:





<?xml version =1.0 encoding =utf-8?> 
< RelativeLayout xmlns:android =http://schemas.android.com/apk/res/android
android:layout_width =match_parent
android:layout_height =match_parent >
< Button
android:text =Prev
android:layout_width =wrap_content
android:layout_height =wrap_content
android:id =@ + id / buttonPrev
android:layout_alignParentBottom =true/>
< Button
android:text =Next
android:layout_width =wrap_content
android:layout_height =wrap_content
android:id =@ + id / buttonNext
android:layout_alignParentRight =true
android:layout_alignParentBottom =true/>
< TextView
android:text =Medium Text
android:textAppearance =?android:attr / textAppearanceMedium
android:layout_width =wrap_content
android:layout_height =wrap_content
android:id =@ + id / textTitle
android:layout_centerHorizo​​ntal =true
android:layout_alignParentBottom =true
android: layout_marginBottom =150dp/>
< / RelativeLayout>





我尝试过:



我尝试取消注释该行



 SetContentView(Resource.Layout.Main); 





但仍然收到此错误



请帮忙!!!

解决方案

虽然要处理错误,但您需要使用 try ... catch 阻止,而在这种情况下,如果要使用,那么它将是块。

 ButtonPrev = FindViewById< Button>(Resource.Id.buttonPrev); 
ButtonNext = FindViewById< Button>(Resource.Id.buttonNext);
TextTitle = FindViewById< TextView>(Resource.Id.textTitle);

if(ButtonPrev!= null){
ButtonPrev.Click + = ButtonPrev_Click; //只会执行,如果不是null。
}

//这里相同。
// ButtonNext.Click + = ButtonNext_Click;



这样,您将确保事件处理程序仅在对象存在时才关联,因为如果按钮不存在 - 有几个原因,例如使用错误的ID,当前布局中没有的按钮,布局问题膨胀,ID不同等等 - 您甚至不需要在它们上实现任何处理程序。这也可以确保不会引发错误。



主要错误



然而,代码中的主要问题是您已经注释掉了布局;如果包含布局,并且按钮存在,则按钮无法以null结尾。在你的代码中,你故意这样做。

 // SetContentView(Resource.Layout.Main); 



取消注释这一行,它会起作用。


I was making a sample android app to test things out and the error is as follows on creating a button handler, though my build succeeded.

Please help

Error: Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object. occurred

Code is as follows:

using Android.App;
using Android.Widget;
using Android.OS;

namespace Android_Picture
{
[Activity(Label = "Android Picture", MainLauncher = true, Icon = 
"@drawable/icon")]
public class MainActivity : Activity
{
    Button ButtonPrev;
    Button ButtonNext;
    TextView TextTitle;
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

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

        ButtonPrev = FindViewById<Button>(Resource.Id.buttonPrev);
        ButtonNext = FindViewById<Button>(Resource.Id.buttonNext);
        TextTitle = FindViewById<TextView>(Resource.Id.textTitle);

        ButtonPrev.Click += ButtonPrev_Click; //error
        ButtonNext.Click += ButtonNext_Click;
    }

    private void ButtonNext_Click(object sender, System.EventArgs e)
    {
        TextTitle.Text = "Next Clicked";
        //throw new System.NotImplementedException();
    }

    private void ButtonPrev_Click(object sender, System.EventArgs e)
    {
        TextTitle.Text = "Previous Clicked";
        //throw new System.NotImplementedException();
    }
}
}





My Main.axml is as follows:


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
    android:text="Prev"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/buttonPrev"
    android:layout_alignParentBottom="true" />
<Button
    android:text="Next"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/buttonNext"
    android:layout_alignParentRight="true"
    android:layout_alignParentBottom="true" />
<TextView
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textTitle"
    android:layout_centerHorizontal="true"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="150dp" />
</RelativeLayout>



What I have tried:

I tried uncommenting the line

SetContentView (Resource.Layout.Main);



But still getting this error

Please help!!!

解决方案

Although to handle the errors, you would need to use a try...catch block, whereas in this case it would be an if...else block to be used.

ButtonPrev = FindViewById<Button>(Resource.Id.buttonPrev);
ButtonNext = FindViewById<Button>(Resource.Id.buttonNext);
TextTitle = FindViewById<TextView>(Resource.Id.textTitle);

if(ButtonPrev != null) {
   ButtonPrev.Click += ButtonPrev_Click; // Would only execute, if not null.
}

// Same here.
// ButtonNext.Click += ButtonNext_Click;


This way, you will make sure that the event handlers are only associated if the objects exist, because if the buttons do not exist — there are several reasons for this, such as wrong ID being used, buttons not available in current layout, problem inflating the layout, ID different etc. — you do not need to even implement any handler on them. That would also make sure that the errors do not get raised.

The main error

Whereas, the main problem in your code is that you have commented out the layout; if the layout is included, and the button exists, there is no way that a button would end up null. Where as, in your code, you have intentionally done that.

// SetContentView (Resource.Layout.Main);


Uncomment this line, and it would work.


这篇关于如何处理xamarin.android中按钮处理程序中的未处理(nullexception)异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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