未处理的异常:System.NullReferenceException:发生<超时,获取异常详细信息>

时间:2019-06-04 00:12:04

标签: android visual-studio xamarin compiler-errors

我使用Visual Studio 2019时,在构建xamarin android应用程序时,它显示此错误-“未处理的异常:System.NullReferenceException:发生”。

我看了其他类似问题的问题,但无济于事。

MainActivity.cs

using Android.App;
using Android.OS;
using Android.Support.V7.App;
using Android.Runtime;
using Android.Widget;
using App2;
using Android.Content;

namespace App1
{
    [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
    public class MainActivity : AppCompatActivity
    {
        EditText myUserName;
        EditText myPass;
        Button myLoginbtn;
        Button signupBtn;

        //variables for signup page
        // prefix "s_" used for signup page variables
        EditText s_username;
        EditText s_pass;
        EditText s_email;
        EditText s_age;
        Button s_signupBtn;

        Android.App.AlertDialog.Builder alert;
        DBHelperClass myDB;
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.activity_main);
            //fatching information from login page
            myUserName = FindViewById<EditText>(Resource.Id.userNameID);
            myPass = FindViewById<EditText>(Resource.Id.pass);
            myLoginbtn = FindViewById<Button>(Resource.Id.button1);
            signupBtn = FindViewById<Button>(Resource.Id.signup);


            //fatching information from signup page
            s_username = FindViewById<EditText>(Resource.Id.s_userNameID);
            s_email = FindViewById<EditText>(Resource.Id.s_email);
            s_age = FindViewById<EditText>(Resource.Id.s_age);
            s_pass = FindViewById<EditText>(Resource.Id.s_pass);
            s_signupBtn = FindViewById<Button>(Resource.Id.s_button1);
            alert = new Android.App.AlertDialog.Builder(this);
            myDB = new DBHelperClass(this); //create constructor



            signupBtn.Click += delegate
            {
                SetContentView(Resource.Layout.signup);
                //Intent newScreen = new Intent(this, typeof(Signuppage));

                //StartActivity(newScreen);
            };


            myLoginbtn.Click += delegate
            {

                var value = myUserName.Text;
                var value2 = myPass.Text;

                System.Console.WriteLine("Username: ---- > " + value);
                System.Console.WriteLine("Password: ---- > " + value2);
                if (value.Trim().Equals("") || value.Length < 0 || value2.Trim().Equals("") || value2.Length < 0)
                {

                    alert.SetTitle("Error");
                    alert.SetMessage("Please Enter Valid Data");
                    alert.SetPositiveButton("OK", alertOKButton);
                    alert.SetNegativeButton("Cancel", alertOKButton);
                    Dialog myDialog = alert.Create();
                    myDialog.Show();
                }
                else
                {  // some value 

                    //myDB.insertValue(1, value);

                    //myDB.selectMydata();



                }

            };
            s_signupBtn.Click += delegate
            {
                var value5 = s_username.Text;
                var value6 = s_email.Text;
                var value7 = s_age.Text;
                var value8 = s_pass.Text;
                if (value5.Trim().Equals("") || value5.Length < 0 || value6.Trim().Equals("") ||
                value6.Length < 0 || value7.Trim().Equals("") ||
                value7.Length < 0 || value8.Trim().Equals("") ||
                value8.Length < 0)
                {

                    alert.SetTitle("Error");
                    alert.SetMessage("Please Enter Valid Data");
                    alert.SetPositiveButton("OK", alertOKButton);
                    alert.SetNegativeButton("Cancel", alertOKButton);
                    Dialog myDialog = alert.Create();
                    myDialog.Show();
                }
                else
                {
                    //insert in database here
                    //order to insert (int id, string value_username, string value_email, string value_age, string value_pass)

                    myDB.InsertValue(1, value5, value6, value7, value8);
                    //System.Console.WriteLine(value,value2, value3, value4);
                    myDB.SelectMydata();

                    SetContentView(Resource.Layout.activity_main);


                }


            };
        }

        public void alertOKButton(object sender, Android.Content.DialogClickEventArgs e)
        {

            System.Console.WriteLine("OK Button Pressed");
        }

        public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
        {
            Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);

            base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }
}

DBHelperclass.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Database;
using Android.Database.Sqlite;
namespace App2
{


        public class DBHelperClass : SQLiteOpenHelper
        {
            Context myContex;


            public static string DBName = "myDatabse.db";
            public static string tableName = "UserTable";
            public static string nameFiled = "names";
            public static string id = "id";
            public static string email = "email";
            public static string age = "age";
            public static string pass = "password";

            //create database
            public string creatTable = "Create Table " +
                tableName + "(" + id + " Interger" + ", " + nameFiled + " Text, " + email + " Text, " +
            age + " Text, " + pass + " Text" + ");";

            SQLiteDatabase connectionObj;

            public DBHelperClass(Context context) : base(context, name: DBName, factory: null, version: 1)
            {
                myContex = context;
                connectionObj = WritableDatabase;
            }

            public override void OnCreate(SQLiteDatabase db)
            {
                System.Console.WriteLine("My Create Table STM \n \n" + creatTable);

                db.ExecSQL(creatTable);
            }

            //insert data in database
            public void InsertValue(int id, string value_username, string value_email, string value_age, string value_pass)
            {

                string insertStm = "Insert into " +
                tableName + " values (" + id + "," + "'" + value_username + "'" + "," + "'" +
                value_email + "'" + "," + "'" +
            value_age + "'" + "," + "'" + value_pass + "'" + ");";
                Console.WriteLine(insertStm);

                System.Console.WriteLine("My SQL  Insert STM \n  \n" + insertStm);

                connectionObj.ExecSQL(insertStm);

            }

            //show data on screen
            public void SelectMydata()
            {
                String selectStm = "Select * from " + tableName;

                ICursor myresut = connectionObj.RawQuery(selectStm, null);


                //String selectStmwithId = "Select * from "+ tableName " where id="+id +"and name="+nameFiled;
                //myresut.Count >0


                while (myresut.MoveToNext())
                {

                    var myId = myresut.GetInt(myresut.GetColumnIndexOrThrow(id));
                    System.Console.WriteLine("ID from BD " + myId);


                    var nameValue = myresut.GetString(myresut.GetColumnIndexOrThrow(nameFiled));
                    System.Console.WriteLine("ID from BD " + nameValue);


                }

            }





            public override void OnUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
            {
                throw new NotImplementedException();
            }
        }

}

signup.axml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffff"
    android:gravity="center"
    android:orientation="vertical">

    <!--
<ImageView
        android:layout_height="90dp"
        android:layout_width="150dp"
        android:id="@+id/image1"
        android:src="@drawable/pin"
    />-->


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/userNameLblID"
        android:text="Please enter your info"
        android:textSize="24dp"
        android:textAlignment="center"
        android:textStyle="bold"
        android:textColor="#333333"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="0dp"
    />

    <LinearLayout
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:orientation="vertical">

        <EditText
            android:layout_width="250dp"
            android:layout_height="50dp"
            android:id="@+id/s_userNameID"
            android:background="#dadee5"
            android:textStyle="bold"
            android:hint="Username"
            android:layout_marginBottom="10dp"
            />
        <EditText
            android:layout_width="250dp"
            android:layout_height="50dp"
            android:id="@+id/s_email"
            android:background="#dadee5"
            android:textStyle="bold"
            android:hint="Email address"
            android:layout_marginBottom="10dp"
            />
        <EditText
            android:layout_width="250dp"
            android:layout_height="50dp"
            android:id="@+id/s_age"
            android:background="#dadee5"
            android:textStyle="bold"
            android:hint="Age"
            android:layout_marginBottom="10dp"



            />

        <!-- Same as Input-->
        <EditText
            android:layout_width="250dp"
            android:layout_height="50dp"
            android:id="@+id/s_pass"
            android:background="#dadee5"
            android:textStyle="bold"
            android:hint="Password"

            />


    </LinearLayout>





    <Button
        android:layout_marginTop="10dp"
        android:id="@+id/s_button1"
        android:layout_width="250dp"
        android:layout_height="50dp"
        android:background="#E60023"
        android:textColor="#FFFFFF"
        android:textStyle="bold"
        android:text="Sign UP" />


</LinearLayout>

activity_main.axml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffff"
    android:gravity="center"
    android:orientation="vertical">

<!--
<ImageView
        android:layout_height="90dp"
        android:layout_width="150dp"
        android:id="@+id/image1"
        android:src="@drawable/pin"
    />-->


<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/userNameLblID"
        android:text="Log in to see more"
        android:textSize="24dp"
        android:textAlignment="center"
        android:textStyle="bold"
        android:textColor="#333333"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="0dp"
    />

<LinearLayout
      android:layout_height="wrap_content"
      android:layout_width="wrap_content"
      android:orientation="vertical">

<EditText
                android:layout_width="250dp"
                android:layout_height="50dp"
                android:id="@+id/userNameID"
                android:background="#dadee5"
                android:textStyle="bold"
                android:hint="Email or Phone number"
                android:layout_marginBottom="10dp"



            />

<!-- Same as Input-->
<EditText
                android:layout_width="250dp"
                android:layout_height="50dp"
                android:id="@+id/pass"
                android:background="#dadee5"
                android:textStyle="bold"
                android:hint="Password"

            />

</LinearLayout>





<Button
        android:layout_marginTop="10dp"
        android:id="@+id/button1"
        android:layout_width="250dp"
        android:layout_height="50dp"
        android:background="#E60023"
        android:textColor="#FFFFFF"
        android:textStyle="bold"
        android:text="Log in" />

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/text2"
        android:text="OR"
        android:textSize="18dp"
        android:textAlignment="center"
        android:textColor="#126eba"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="10dp"
    />

<LinearLayout
      android:layout_height="50dp"
      android:layout_width="250dp"
      android:orientation="horizontal"
        android:background="#3b5998"
>

<!--
<ImageView
        android:layout_height="45dp"
        android:layout_width="40dp"
        android:gravity="center"
        android:id="@+id/image1"
        android:src="@drawable/fb"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="2dp"
    />-->

<TextView

        android:layout_marginTop="0dp"
        android:id="@+id/fbbtn"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:textColor="#FFFFFF"
        android:text="Continue with Facebook"
        android:gravity="center"
        android:layout_marginLeft="20dp"
        android:textSize="16dp"
        />

</LinearLayout>


<LinearLayout
      android:layout_height="50dp"
      android:layout_width="250dp"
      android:orientation="horizontal"
        android:background="#4885ed"
        android:layout_marginTop="5dp"
>

<!--
<ImageView
        android:layout_height="45dp"
        android:layout_width="40dp"
        android:gravity="center"
        android:id="@+id/image2"
        android:src="@drawable/google"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="2dp"
    />-->

<TextView

        android:layout_marginTop="0dp"
        android:id="@+id/googlebtn"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:textColor="#FFFFFF"
        android:text="Continue with Google"
        android:gravity="center"
        android:layout_marginLeft="20dp"
        android:textSize="16dp"
        />

</LinearLayout>

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/text3"
        android:text="Forgot your password?"
        android:textSize="10dp"
        android:textAlignment="center"
        android:textColor="#717171"
        android:layout_marginBottom="0dp"
        android:layout_marginTop="10dp"
    />
<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/text4"
        android:text="Are you a business? Get started here"
        android:textSize="10dp"
        android:textAlignment="center"
        android:textColor="#717171"
        android:layout_marginBottom="10dp"
    />

<View
    android:layout_width="70dp"
    android:layout_height="1dp"
    android:background="@android:color/darker_gray"/>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="15dp"
            android:orientation="horizontal"
    >
<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/text5"
        android:text="Not on Pintrest yet?"
        android:textSize="10dp"

        android:textColor="#717171"

    />
    <Button
        android:id="@+id/signup"
        android:layout_width="50dp"
        android:layout_height="15dp"
        android:textSize="10dp"
        android:background="#ffffff"
        android:textColor="#717171"
        android:textStyle="bold"
        android:text="Sign Up" />
    </LinearLayout>
</LinearLayout>

有4个文件,这些是错误屏幕截图,谢谢   error

error pic

1 个答案:

答案 0 :(得分:0)

这是解决方法:)谢谢

using Android.App;
using Android.OS;
using Android.Support.V7.App;
using Android.Runtime;
using Android.Widget;
using App2;

using Android.Content;

namespace App2
{
    [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
    public class MainActivity : AppCompatActivity
    {

        EditText myUserName;

        EditText myPass;

        Button myLoginbtn;

        Button signupBtn;



        //variables for signup page 

        // prefix "s_" used for signup page variables 

        EditText s_username;

        EditText s_pass;

        EditText s_email;

        EditText s_age;

        Button s_signupBtn;



        Android.App.AlertDialog.Builder alert;

        DBHelperClass myDB;

        protected override void OnCreate(Bundle savedInstanceState)

        {

            base.OnCreate(savedInstanceState);

            // Set our view from the "main" layout resource 

            SetContentView(Resource.Layout.activity_main);

            //fatching information from login page 

            myUserName = FindViewById<EditText>(Resource.Id.userNameID);

            myPass = FindViewById<EditText>(Resource.Id.pass);

            myLoginbtn = FindViewById<Button>(Resource.Id.button1);

            signupBtn = FindViewById<Button>(Resource.Id.signup);







            alert = new Android.App.AlertDialog.Builder(this);

            myDB = new DBHelperClass(this); //create constructor 







            signupBtn.Click += delegate

            {

                SetContentView(Resource.Layout.signup);

               // Intent newScreen = new Intent(this, typeof(DBHelperClass)); 



               // StartActivity(newScreen);
                //fatching information from signup page 

                s_username = FindViewById<EditText>(Resource.Id.s_userNameID);

                s_email = FindViewById<EditText>(Resource.Id.s_email);

                s_age = FindViewById<EditText>(Resource.Id.s_age);

                s_pass = FindViewById<EditText>(Resource.Id.s_pass);

                s_signupBtn = FindViewById<Button>(Resource.Id.s_button1);

                s_signupBtn.Click += delegate

                {

                    var value5 = s_username.Text;

                    var value6 = s_email.Text;

                    var value7 = s_age.Text;

                    var value8 = s_pass.Text;

                    if (value5.Trim().Equals("") || value5.Length < 0 || value6.Trim().Equals("") ||

                    value6.Length < 0 || value7.Trim().Equals("") ||

                    value7.Length < 0 || value8.Trim().Equals("") ||

                    value8.Length < 0)

                    {



                        alert.SetTitle("Error");

                        alert.SetMessage("Please Enter Valid Data");

                        alert.SetPositiveButton("OK", alertOKButton);

                        alert.SetNegativeButton("Cancel", alertOKButton);

                        Dialog myDialog = alert.Create();

                        myDialog.Show();

                    }

                    else

                    {

                        //insert in database here 

                        //order to insert (int id, string value_username, string value_email, string value_age, string value_pass) 



                        myDB.InsertValue(1, value5, value6, value7, value8);

                        //System.Console.WriteLine(value,value2, value3, value4); 

                        myDB.SelectMydata();



                        //SetContentView(Resource.Layout.activity_main);
                        Intent newScreen = new Intent(this, typeof(MainActivity)); 



                        StartActivity(newScreen);




                    }





                };

            };





            myLoginbtn.Click += delegate

            {



                var value = myUserName.Text;

                var value2 = myPass.Text;



                System.Console.WriteLine("Username: ---- > " + value);

                System.Console.WriteLine("Password: ---- > " + value2);

                if (value.Trim().Equals("") || value.Length < 0 || value2.Trim().Equals("") || value2.Length < 0)

                {



                    alert.SetTitle("Error");

                    alert.SetMessage("Please Enter Valid Data");

                    alert.SetPositiveButton("OK", alertOKButton);

                    alert.SetNegativeButton("Cancel", alertOKButton);

                    Dialog myDialog = alert.Create();

                    myDialog.Show();

                }

                else

                {  // some value  



                    //myDB.insertValue(1, value); 



                    myDB.SelectMydata();







                }



            };



        }



        public void alertOKButton(object sender, Android.Content.DialogClickEventArgs e)

        {



            System.Console.WriteLine("OK Button Pressed");

        }



        public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)

        {

            //Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);



            base.OnRequestPermissionsResult(requestCode, permissions, grantResults);

        }

    }
}