我在Android中的新功能...当我按下后退按钮时,它会给我以下错误... 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
12-01 11:10:07.952: E/AndroidRuntime(3171): at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3655)
12-01 11:10:07.952: E/AndroidRuntime(3171): at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3673)
12-01 11:10:07.952: E/AndroidRuntime(3171): at android.app.ActivityThread.access$2900(ActivityThread.java:125)
12-01 11:10:07.952: E/AndroidRuntime(3171): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
12-01 11:10:07.952: E/AndroidRuntime(3171): at android.os.Handler.dispatchMessage(Handler.java:99)
12-01 11:10:07.952: E/AndroidRuntime(3171): at android.os.Looper.loop(Looper.java:123)
12-01 11:10:07.952: E/AndroidRuntime(3171): at android.app.ActivityThread.main(ActivityThread.java:4627)
12-01 11:10:07.952: E/AndroidRuntime(3171): at java.lang.reflect.Method.invokeNative(Native Method)
12-01 11:10:07.952: E/AndroidRuntime(3171): at java.lang.reflect.Method.invoke(Method.java:521)
12-01 11:10:07.952: E/AndroidRuntime(3171): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-01 11:10:07.952: E/AndroidRuntime(3171): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-01 11:10:07.952: E/AndroidRuntime(3171): at dalvik.system.NativeStart.main(Native Method)
12-01 11:10:07.952: E/AndroidRuntime(3171): Caused by: java.lang.NullPointerException
12-01 11:10:07.952: E/AndroidRuntime(3171): at com.example.IranNara.FirstPage.onDestroy(FirstPage.java:133)
12-01 11:10:07.952: E/AndroidRuntime(3171): at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3642)
我的活动代码是:
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;
}`
答案 0 :(得分:5)
当password
不等于storedPassword
时,mydb
实例为空,您尝试调用close()
方法,因此请在调用close之前检查NULL
({1}}中的():
onDestroy
答案 1 :(得分:1)
@Override
protected void onDestroy() {
super.onDestroy();
if(mydb != null)
mydb.close();
}
您应该在getSinlgeEntry方法中打开和关闭数据库。你不需要在onDestroy中关闭它