在布局中添加内容会导致崩溃

时间:2016-10-02 21:54:21

标签: android xml xamarin

所以有一个我想改变的布局。所以我为它添加了一个线性布局,并以某种方式崩溃了与之无关的代码。所以这里我会发布一些代码。

这是崩溃发生的主要文件和Ill点。          protected override void OnCreate(Bundle bundle)          {

        base.OnCreate(bundle);

        SetContentView(Resource.Layout.layoutLogin);



        gbtnSignUp = FindViewById<Button>(Resource.Id.btnSignUp);
        gbtnSignIn = FindViewById<Button>(Resource.Id.btnSignIn);
        gbtnSignUp.Click += gbtnSignUp_Click;
        gbtnSignIn.Click += gbtnSignIn_Click; // here is where it crashes
    }

现在我的布局。我精确指出我添加的部分会增加崩溃。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:weightSum="100"
android:layout_height="fill_parent">
<ListView
    android:layout_weight="80"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:background="@drawable/ListViewHighlight"
    android:id="@+id/companyListView" />
<ProgressBar
    android:layout_weight="10"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:background="@drawable/ListViewHighlight"
    android:id="@+id/progressBar1" />
<LinearLayout// just from this linear layout existing it causes a crash
    android:orientation="horizontal"
    android:minWidth="25px"
    android:minHeight="25px"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="10"
    android:weightSum="100"
    android:id="@+id/linearLayout1">
    <TextView
        android:text="Text"
        android:layout_width="0dp"
        android:layout_weight="10"
        android:layout_height="match_parent"
        android:id="@+id/textView1" />
    <ImageButton
        android:src="@drawable/defaultAdd"
        android:layout_width="0dp"
        android:layout_weight="90"
        android:layout_height="match_parent"
        android:background="@android:color/transparent"
        android:id="@+id/imageButton1" />
</LinearLayout>
</LinearLayout>

此布局甚至不会导致导致崩溃的视图。哎呀我甚至从项目中删除了对该视图的所有引用,它仍然崩溃了。

这是强制我在指定的行中断的错误消息。

System.NullReferenceException:未将对象引用设置为对象的实例

此外,我可以评论这一行,但它会使它能够运行,但仍然能够按下按钮,然后做一些非常古怪的行为,我不应该按它开始。< / p>

登录按钮的xml

   <Button
    android:text="Sign In"
    android:layout_width="match_parent"
    android:layout_weight="15"
    android:layout_height="0dp"
    android:id="@+id/btnSignIn"
    android:textSize="25sp"
    android:background="@drawable/ButtonSignInStyle"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp" />

2 个答案:

答案 0 :(得分:1)

您的布局不包含ID为btnSignUpbtnSignIn的按钮。您需要添加它们或删除活动代码中的4行。

布局中缺少

<Button
    android:id="@+id/btnSignUp" 
    .../>

<Button
    android:id="@+id/btnSignIn" 
    .../>

答案 1 :(得分:0)

实际上,当我导出程序以将其移动到另一个文件夹时,这个问题出现了一些混乱的情况。我将我的文件复制并粘贴到原始文件夹中的旧版本,并且完美无缺。因此,如果任何人以不太好的方式移动文件,可能会导致无法预料的问题,就像这个一样没有意义。

相关问题