Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 调用单击事件时出错_C#_Android_Xamarin_Visual Studio 2017 - Fatal编程技术网

C# 调用单击事件时出错

C# 调用单击事件时出错,c#,android,xamarin,visual-studio-2017,C#,Android,Xamarin,Visual Studio 2017,试图找出如何在多页面Android应用程序中导航 我的第一页的XAML如下所示: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:l

试图找出如何在多页面Android应用程序中导航

我的第一页的XAML如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:text="WELFARE"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/cmdWelfare"
        android:onClick="cmdWelfareClicked" />
    <Button
        android:text="SETTINGS"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/cmdSettings"
        android:onClick="cmdSettingsClicked" />
</LinearLayout>
[Export("cmdWelfareClicked")]
public void cmdWelfareClicked(View v)
{
    SetContentView(Resource.Layout.p1);
}


[Export("cmdSettingsClicked")]
public void cmdSettingsClicked(View v)
{
    v.SetBackgroundColor(Android.Graphics.Color.Azure);
}
但当单击按钮打开下一页时,会引发未知异常

错误文本(来自副本详细信息)为
发生了未处理的异常。发生


我正在努力学习如何做到这一点,不知道会出什么问题,因此,如果您能提供任何帮助,我们将不胜感激。

您需要导出该方法。试着这样做:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:text="WELFARE"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/cmdWelfare"
        android:onClick="cmdWelfareClicked" />
    <Button
        android:text="SETTINGS"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/cmdSettings"
        android:onClick="cmdSettingsClicked" />
</LinearLayout>
[Export("cmdWelfareClicked")]
public void cmdWelfareClicked(View v)
{
    SetContentView(Resource.Layout.p1);
}


[Export("cmdSettingsClicked")]
public void cmdSettingsClicked(View v)
{
    v.SetBackgroundColor(Android.Graphics.Color.Azure);
}
但是为什么要做
SetContentView(Resource.Layout.p1)
inside
CMDWelfare单击
?,如果要使用不同的布局,应启动另一个
活动

另一种方法是从
xml
中的
按钮
中删除
onClick
属性
,并在
OnCreate
中使用id

比如:

protectedoverride void OnCreate(捆绑包)
{
base.OnCreate(bundle);
//从“主”布局资源设置视图
SetContentView(Resource.Layout.Main);
Button buttonCmdWelfare=FindViewById(Resource.Id.cmdWelfare);
按钮CMD福利。单击+=委派{
//点击
};
Button buttonCmdSettings=FindViewById(Resource.Id.cmdSettings);
按钮MDSettings。单击+=委派{
//点击
};
}

别忘了删除android:onClick=“cmdsetingsclicked”和
android:onClick=“cmdWelfareClicked”

你能发布堆栈跟踪吗?我很乐意,但我得到的只是上面的内容。如何获取堆栈跟踪?嗯。我还建议您尝试将事件处理程序分配给onCreate方法中的按钮,看看这是否解决了问题。建议这样做,而不是在xml中指定单击。这在单页应用程序中效果很好,但如果重新加载页面,事件将再次添加,并将中断。除非你能告诉我怎么…:)@Suraj-S例如,在我添加新事件之前,是否有办法清除现有事件?非常感谢。那么你如何开始另一项活动呢?(我是新来的)这是你的更新,是的,这是我以前做的。问题在于它是一个多页面应用程序,当重新打开页面时,会再次调用“活动”,并再次分配“单击”事件,这会导致错误。@companydronefromssector7g要启动新活动,请参阅。你能把错误贴出来吗?它不应该导致任何错误,因为只有在调用OnDestroy时才会调用OnCreate,因此它需要重新分配侦听器。我猜这是因为您正在执行
SetContentView(Resource.Layout.p1)在点击事件中(您不应该这样做)谢谢:)我将改为那样做。