nullpointerexception .....如何提取用户名并传递数据库?

时间:2013-07-16 15:09:17

标签: java android eclipse sqlite

这是login.java。当我开始运行该程序时,它会强制关闭。这是代码:

package com.example.login1;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.widget.EditText;
import android.widget.Button;


public class Login extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        sqlite as = new sqlite();
        SQLiteDatabase sqLiteDB = null;
        as.sqlitetry(sqLiteDB);
        as.sqlinit();

        final Button regs = (Button) findViewById(R.id.regs);
        final Button login = (Button) findViewById(R.id.login);
        final EditText userName1 = (EditText) findViewById(R.id.userName1);
        final EditText pw1 = (EditText) findViewById(R.id.pw1);

        login.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                if (userName1.getText().toString().equals("admin")
                        && pw1.getText().toString().equals("admin")) {
                    // if the user and pass is correct
                    AlertDialog.Builder good = new AlertDialog.Builder(
                            Login.this);
                    good.setMessage("Logging in");
                    userName1.setText("");
                    pw1.setText("");
                    good.setPositiveButton("Login",
                            new DialogInterface.OnClickListener() {

                                @Override
                                public void onClick(DialogInterface dialog,
                                        int arg1) {
                                    // TODO Auto-generated method stub
                                    dialog.cancel();
                                }
                            });
                    AlertDialog alert = good.create();
                    alert.show();
                    Intent it = new Intent(Login.this, Homepage.class);
                    startActivity(it);
                }
                // if the user or pass is wrong
                else {
                    AlertDialog.Builder sorry = new AlertDialog.Builder(
                            Login.this);
                    sorry.setMessage("Wrong UserId/Password");
                    sorry.setPositiveButton("Cancel",
                            new DialogInterface.OnClickListener() {

                                @Override
                                public void onClick(DialogInterface dialog,
                                        int which) {
                                    // TODO Auto-generated method stub
                                    userName1.setText("");
                                    pw1.setText("");
                                    dialog.cancel();
                                }
                            });
                    AlertDialog alert = sorry.create();
                    alert.show();
                }
            }
        });

        regs.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent it = new Intent(Login.this, Registration.class);
                startActivity(it);
            }
        });
        final Button Exit = (Button) findViewById(R.id.exit);
        Exit.setOnClickListener(new OnClickListener() {

            // alert button for exit
            public void onClick(View arg0) {
                AlertDialog.Builder builder = new AlertDialog.Builder(
                        Login.this);
                builder.setMessage("Are you sure you want to Exit?");
                builder.setPositiveButton("Yes",
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface arg0, int arg1) {
                                // TODO Auto-generated method stub
                                moveTaskToBack(true);
                            }
                        });
                builder.setNegativeButton("No",
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int arg0) {
                                // TODO Auto-generated method stub
                                dialog.cancel();
                            }
                        });
                AlertDialog alert = builder.create();
                alert.show();
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.login, menu);
        return true;
    }

}

这是login.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".Login" 
    android:background="@drawable/bmi">

    <TextView
        android:id="@+id/uname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/regs"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="113dp"
        android:layout_marginLeft="16dp"
        android:text="UserId" />

    <TextView
        android:id="@+id/pass"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/userName1"
        android:layout_marginTop="24dp"
        android:text="Password" />

    <Button
        android:id="@+id/login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/uname"
        android:layout_marginLeft="14dp"
        android:layout_toRightOf="@+id/regs"
        android:text="Login" />

    <Button
        android:id="@+id/regs"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/userName1"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="107dp"
        android:text="Register" />

    <EditText
        android:id="@+id/pw1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/pass"
        android:layout_alignBottom="@+id/pass"
        android:layout_alignLeft="@+id/userName1"
        android:ems="10"
        android:inputType="textPassword" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/userName1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/uname"
        android:layout_toRightOf="@+id/pass"
        android:ems="10" />

    <Button
        android:id="@+id/exit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignRight="@+id/login"
        android:layout_marginBottom="33dp"
        android:text="Exit" />

</RelativeLayout>

Registration.java

package com.example.login1;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.view.Menu;
import android.view.View;
import android.content.DialogInterface;
import android.content.Intent;
import android.widget.Button;
import android.widget.EditText;

public class Registration extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.registration);

        // function of cancel button

        final Button cancelreg = (Button) findViewById(R.id.cancelreg);

        final EditText regsuname = (EditText) findViewById(R.id.regsuname);
        final EditText pass = (EditText) findViewById(R.id.pass);
        final EditText confpass = (EditText) findViewById(R.id.confpass);
        final EditText fname = (EditText) findViewById(R.id.fname);
        final EditText lname = (EditText) findViewById(R.id.lname);

        final Button regreg = (Button) findViewById(R.id.regereg);
        regreg.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent it = new Intent(Registration.this,
                        RegsConfirmation.class);
                if (checker(regsuname) && checker(pass) && checker(confpass)
                        && checker(fname) && checker(lname)) {
                    if (pass.getText().toString()
                            .equals(confpass.getText().toString())) {
                        /*regsuname.setText(regsuname.getText());
                            DatabaseAdapter databaseAdapter = 
                                    new DatabaseAdapter(getApplicationContext());
                            databaseAdapter.open();

                            databaseAdapter.createRecord(regsuname.getText().toString());
                            databaseAdapter.close();*/
                        it.putExtra("regsuname", regsuname.getText().toString());
                        it.putExtra("pass", pass.getText().toString());
                        it.putExtra("fname", fname.getText().toString());
                        it.putExtra("lname", lname.getText().toString());
                        startActivity(it);
                    } 
                    else {
                        AlertDialog.Builder xpass = new AlertDialog.Builder(
                                Registration.this);
                        xpass.setMessage("Password didn't match");
                        xpass.setNegativeButton("Okay",
                                new DialogInterface.OnClickListener() {

                                    @Override
                                    public void onClick(DialogInterface dialog,
                                            int which) {
                                        // TODO Auto-generated method stub
                                        dialog.cancel();
                                    }
                                });
                        AlertDialog alert = xpass.create();
                        alert.show();
                    }
                }

                else {
                    AlertDialog.Builder incomplete = new AlertDialog.Builder(
                            Registration.this);
                    incomplete.setMessage("Incomplete Text Fields");
                    incomplete.setNegativeButton("Okay",
                            new DialogInterface.OnClickListener() {

                                @Override
                                public void onClick(DialogInterface dialog,
                                        int which) {
                                    // TODO Auto-generated method stub
                                    dialog.cancel();
                                }
                            });
                    AlertDialog alert = incomplete.create();
                    alert.show();
                }
            }
        });

        cancelreg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                AlertDialog.Builder cancel = new AlertDialog.Builder(
                        Registration.this);
                cancel.setMessage("Are you sure you want to Cancel?");
                cancel.setPositiveButton("Yes",
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface arg0, int arg1) {
                                // TODO Auto-generated method stub
                                Intent it = new Intent(Registration.this,
                                        Login.class);
                                startActivity(it);
                            }
                        });
                cancel.setNegativeButton("No",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int arg1) {
                                dialog.cancel();
                            }

                        });
                AlertDialog alert = cancel.create();
                alert.show();
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.registration, menu);
        return true;
    }

    public boolean checker(EditText et) {
        String input = et.getText().toString().trim();
        boolean b = true;
        int in = input.length();
        if (in == 0) {
            b = false;

        } else {
            b = true;
        }
        return b;
    }

}

Registration.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".Registration" 
     android:background="@drawable/bmi">
    <Button
        android:id="@+id/cancelreg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="22dp"
        android:layout_toRightOf="@+id/textView2"
        android:text="Cancel" />

    <TextView
        android:id="@+id/lnametext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/fnametext"
        android:layout_below="@+id/fnametext"
        android:layout_marginTop="12dp"
        android:text="Last Name" />

    <Button
        android:id="@+id/regereg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/cancelreg"
        android:layout_alignBottom="@+id/cancelreg"
        android:layout_toRightOf="@+id/cancelreg"
        android:text="Register" />

    <EditText
        android:id="@+id/fname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/fnametext"
        android:layout_alignBottom="@+id/fnametext"
        android:layout_alignLeft="@+id/confpass"
        android:layout_alignParentRight="true"
        android:ems="10" />

    <EditText
        android:id="@+id/lname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/fname"
        android:layout_alignRight="@+id/fname"
        android:layout_alignTop="@+id/lnametext"
        android:ems="10" />

    <EditText
        android:id="@+id/regsuname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/usertxt"
        android:layout_alignLeft="@+id/pass"
        android:ems="10" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/pass"
        android:layout_alignRight="@+id/fnametext"
        android:text="Password" />

    <EditText
        android:id="@+id/confpass"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/pass"
        android:layout_toRightOf="@+id/textView3"
        android:ems="10"
        android:inputType="textPassword" />

    <EditText
        android:id="@+id/pass"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/confpasstext"
        android:layout_toRightOf="@+id/textView3"
        android:ems="10"
        android:inputType="textPassword" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/fnametext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView3"
        android:layout_alignTop="@+id/textView3"
        android:layout_marginTop="39dp"
        android:text="First Name" />

    <TextView
        android:id="@+id/usertxt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/confpass"
        android:layout_alignLeft="@+id/textView1"
        android:text="UserId:" />

    <TextView
        android:id="@+id/confpasstext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/fnametext"
        android:layout_alignLeft="@+id/textView3"
        android:layout_marginBottom="26dp"
        android:text="Confrim Password" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/cancelreg"
        android:layout_alignLeft="@+id/textView2"
        android:layout_marginBottom="104dp"
        android:text="Personal information: " />

</RelativeLayout>

RegsConfirmation.java

package com.example.login1;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.widget.Button;


public class RegsConfirmation extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.regs_confirmation);

        sqlite as = new sqlite();
        SQLiteDatabase sqLiteDB = null;
        as.sqlitetry(sqLiteDB);
        as.sqlinit();

        TextView uname = (TextView) findViewById(R.id.uname);
        TextView pass = (TextView) findViewById(R.id.password);
        TextView fname = (TextView) findViewById(R.id.fname);
        TextView lname = (TextView) findViewById(R.id.lname);

        uname.setText(getIntent().getExtras().getString("regsuname"));
        pass.setText(getIntent().getExtras().getString("pass"));
        fname.setText(getIntent().getExtras().getString("fname"));
        lname.setText(getIntent().getExtras().getString("lname"));

        Button confirm = (Button) findViewById(R.id.confirm);
        confirm.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

                // TODO Auto-generated method stub
                Intent it = new Intent(RegsConfirmation.this, Login.class);
                startActivity(it);
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.regs_confirmation, menu);
        return true;
    }

}

RegsConfirmation.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".RegsConfirmation" 
    android:background="@drawable/bmi">

    <TextView
        android:id="@+id/password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView3"
        android:layout_alignLeft="@+id/uname"
        android:text="TextView"
        />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView3"
        android:layout_below="@+id/textView3"
        android:layout_marginTop="15dp"
        android:text="First Name" />

    <TextView
        android:id="@+id/fname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView5"
        android:layout_alignLeft="@+id/password"
        android:text="TextView" />

    <TextView
        android:id="@+id/lname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView5"
        android:layout_alignBottom="@+id/textView5"
        android:layout_alignLeft="@+id/fname"
        android:text="TextView" />

    <Button
        android:id="@+id/confirm"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignRight="@+id/lname"
        android:layout_marginBottom="81dp"
        android:layout_marginRight="28dp"
        android:text="Comfirm" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView4"
        android:layout_marginTop="16dp"
        android:text="Last Name" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="18dp"
        android:text="Password" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_below="@+id/textView2"
        android:layout_marginTop="69dp"
        android:text="Username" />

    <TextView
        android:id="@+id/uname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView3"
        android:layout_centerHorizontal="true"
        android:text="TextView" />

</RelativeLayout>

sqlile.java

package com.example.login1;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class sqlite extends Activity {
    public void sqlitetry(final SQLiteDatabase sqLiteDB) {
        String  createSql = "CREATE TABLE" +
                "(_id integer primary key autoincrement, " +
                "name text not null);";
        sqLiteDB.execSQL(createSql);
    }

    public void sqlinit() {
        SQLiteDatabase db = openOrCreateDatabase("MyDatabase",MODE_PRIVATE,null);
        db.execSQL("CREATE TABLE id note exist MyTable(username Varchar, password Varchar PRIMARY KEY);");
        Cursor c = db.rawQuery("Select * from MyTable", null);
        c.moveToFirst();
        while(c.isAfterLast() == false){

        }       
    }``
}

它运行代码没有错误。但在安装apk之后,它会强行关闭。这就是Logcat显示RuntimeExceptionNullPointerException

的内容

0 个答案:

没有答案