当我按下后退按钮时,出现以下错误:
12-01 11:10:07.952: E/AndroidRuntime(3171): FATAL EXCEPTION: main 12-01 11:10:07.952: E/AndroidRuntime(3171): java.lang.RuntimeException: Unable to destroy activity {com.example.IranNara/com.example.IranNara.FirstPage}: java.lang.NullPointerException
。我的活动代码是: Button Nara;
SQLiteDatabase mydb;
String Text="";
//private static String DBNAME = "PDA";
private static String TABLE = "Users";
public static Integer StartFlag;
//LinearLayout linearL;
//public static String storedPersonelNo = "";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_xml);
Nara = (Button)findViewById(R.id.btnNara);
StartFlag =0;
final NaraAdapter mydb = new NaraAdapter(this);
mydb.createDatabase();
mydb.open();
//System.out.println("My DB is:::: "+mydb);
//For Login
final Dialog dialog = new Dialog(FirstPage.this);
dialog.setContentView(R.layout.login);
dialog.setTitle("Login");
final EditText editTextUserName=(EditText)dialog.findViewById(R.id.editTextUserNameToLogin);
final EditText editTextPassword=(EditText)dialog.findViewById(R.id.editTextPasswordToLogin);
//----------for entering on Exit Button------------------------------------------------------------------------------------------
if( getIntent().getBooleanExtra("Exit", false)){
finish();
return;
}
//-----------------------------------------------------------------------------------------------------------------------//********click on IranNara Button
Nara.setOnClickListener(new View.OnClickListener() {
//Click on Cancel Button
public void onClick(View v)
{
Button btnLogin=(Button)dialog.findViewById(R.id.buttonLogin);
Button Cancel = (Button)dialog.findViewById(R.id.btncancel);
dialog.show();
Cancel.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
editTextPassword.setText("") ;
}});
//Click on Login Button
btnLogin.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// get The User name and Password
String userName=editTextUserName.getText().toString();
String password=editTextPassword.getText().toString();
// fetch the Password
String storedPassword = getSinlgeEntry(userName,1);
System.out.println("storedPassword:"+storedPassword);
// check if the Stored password matches with Password entered by user
if(password.equals(storedPassword))
{
Toast.makeText(FirstPage.this, "Congrats: Login Successfull", Toast.LENGTH_LONG).show();
// Entered to Messages
String storedPersonelNo = getSinlgeEntry(userName,2);
Intent Startmain = new Intent(FirstPage.this, Main.class);
Startmain.putExtra("PersonelNumber", storedPersonelNo);
// SharedPreferences settings = getSharedPreferences(storedPersonelNo, 0);
dialog.dismiss();
StartFlag=1;
startActivityForResult(Startmain, 0);
}
else
{
Toast.makeText(FirstPage.this, "User Name or Password does not match", Toast.LENGTH_LONG).show();
}
}
});
}
});
}
//--------------------------------------------------------------------方法---------------------------------------------------------------------------------------------------------------
public String getSinlgeEntry(String userName,Integer n)
{
NaraDatabaseHandler DH = new NaraDatabaseHandler(this);
mydb = DH.getReadableDatabase();
//System.out.println("My DB is:::: "+mydb);
Cursor cursor=mydb.query(TABLE, null, " UserName=?", new String[]{userName}, null, null, null);
if(cursor.getCount()<1)
{
cursor.close();
return "NOT EXIST";
}
cursor.moveToFirst();
if (n==1){
Text= cursor.getString(cursor.getColumnIndex("Password"));}
if(n==2){
Text= cursor.getString(cursor.getColumnIndex("PersonelNo"));}
cursor.close();
return Text;
}
@Override
protected void onDestroy() {
super.onDestroy();
mydb.close();
//StartFlag = null;
}`