我有一个onClick()方法,在调用时启动一个新活动,并在当前活动上调用finish()。调用此方法时;但是,抛出以下异常。在onDestroy()方法中只进行了两次调用(抛出异常的位置)。它看起来像一个被弃用的方法是原因的根本。任何人都可以解释为什么会发生这种情况吗?
07-30 23:20:01.774: D/dalvikvm(623): GC_EXTERNAL_ALLOC freed 746 objects / 54680 bytes in 689ms
07-30 23:20:09.114: E/global(623): Deprecated Thread methods are not supported.
07-30 23:20:09.114: E/global(623): java.lang.UnsupportedOperationException
07-30 23:20:09.114: E/global(623): at java.lang.VMThread.stop(VMThread.java:85)
07-30 23:20:09.114: E/global(623): at java.lang.Thread.stop(Thread.java:1379)
07-30 23:20:09.114: E/global(623): at java.lang.Thread.stop(Thread.java:1344)
07-30 23:20:09.114: E/global(623): at com.home.saket.SplashScreen$1.run(SplashScreen.java:38)
07-30 23:20:09.665: D/dalvikvm(623): GC_EXTERNAL_ALLOC freed 393 objects / 22200 bytes in 106ms
07-30 23:20:13.223: I/System.out(623): In constructor
07-30 23:20:13.363: D/NewUserActivity(623): Checkkkkkkkk
07-30 23:20:13.363: D/NewUserActivity(623): Checkkkkkkkk22222
07-30 23:20:14.914: D/AndroidRuntime(623): Shutting down VM
07-30 23:20:14.964: W/dalvikvm(623): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
07-30 23:20:15.044: E/AndroidRuntime(623): FATAL EXCEPTION: main
07-30 23:20:15.044: E/AndroidRuntime(623): java.lang.RuntimeException: Unable to destroy activity {com.home.saket/com.home.saket.DatabaseActivity}: java.lang.NullPointerException
07-30 23:20:15.044: E/AndroidRuntime(623): at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3655)
07-30 23:20:15.044: E/AndroidRuntime(623): at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3673)
07-30 23:20:15.044: E/AndroidRuntime(623): at android.app.ActivityThread.access$2900(ActivityThread.java:125)
07-30 23:20:15.044: E/AndroidRuntime(623): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
07-30 23:20:15.044: E/AndroidRuntime(623): at android.os.Handler.dispatchMessage(Handler.java:99)
07-30 23:20:15.044: E/AndroidRuntime(623): at android.os.Looper.loop(Looper.java:123)
07-30 23:20:15.044: E/AndroidRuntime(623): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-30 23:20:15.044: E/AndroidRuntime(623): at java.lang.reflect.Method.invokeNative(Native Method)
07-30 23:20:15.044: E/AndroidRuntime(623): at java.lang.reflect.Method.invoke(Method.java:521)
07-30 23:20:15.044: E/AndroidRuntime(623): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-30 23:20:15.044: E/AndroidRuntime(623): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-30 23:20:15.044: E/AndroidRuntime(623): at dalvik.system.NativeStart.main(Native Method)
07-30 23:20:15.044: E/AndroidRuntime(623): Caused by: java.lang.NullPointerException
07-30 23:20:15.044: E/AndroidRuntime(623): at com.home.saket.DatabaseActivity.onDestroy(DatabaseActivity.java:127)
07-30 23:20:15.044: E/AndroidRuntime(623): at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3642)
07-30 23:20:15.044: E/AndroidRuntime(623): ... 11 more
07-30 23:20:17.444: I/Process(623): Sending signal. PID: 623 SIG: 9
我的源代码......
DatabaseActivity.java
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mUsername = (EditText) findViewById(R.id.editUsername);
mPassword = (EditText) findViewById(R.id.editPassword);
mNewUser = (Button) findViewById(R.id.buttonNewUser);
mLogin = (Button) findViewById(R.id.buttonLogin);
mShowAll = (Button) findViewById(R.id.buttonShowAll);
mShowAll.setOnClickListener(this);
mNewUser.setOnClickListener(this);
mLogin.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.buttonLogin:
String uname = mUsername.getText().toString();
String pass = mPassword.getText().toString();
if (uname.equals("") || uname == null) {
Toast.makeText(getApplicationContext(), "Username Empty",
Toast.LENGTH_SHORT).show();
} else if (pass.equals("") || pass == null) {
Toast.makeText(getApplicationContext(), "Password Empty",
Toast.LENGTH_SHORT).show();
} else {
boolean validLogin = validateLogin(uname, pass,
DatabaseActivity.this);
if (validLogin) {
System.out.println("In Valid");
Intent i_login = new Intent(DatabaseActivity.this,
UserLoggedInPage.class);
startActivity(i_login);
finish();
}
}
break;
case R.id.buttonNewUser:
try {
Intent i = new Intent(DatabaseActivity.this,
NewUserActivity.class);
startActivity(i);
} catch (Exception e) {
// TODO: handle exception
}
finish();
break;
case R.id.buttonShowAll:
Intent i_admin = new Intent(DatabaseActivity.this, AdminPage.class);
startActivity(i_admin);
finish();
break;
}
}
public boolean validateLogin(String uname, String pass, Context context) {
mydb = new DbHelper(context);
SQLiteDatabase db = mydb.getReadableDatabase();
// SELECT
String[] columns = { "_id" };
// WHERE clause
String selection = "username=? AND password=?";
// WHERE clause arguments
String[] selectionArgs = { uname, pass };
Cursor cursor = null;
try {
// SELECT _id FROM login WHERE username=uname AND password=pass
cursor = db.query(DbHelper.SAKET_TABLE_NAME, columns, selection,
selectionArgs, null, null, null);
startManagingCursor(cursor);
} catch (Exception e) {
e.printStackTrace();
}
int numberOfRows = cursor.getCount();
if (numberOfRows <= 0) {
Toast.makeText(getApplicationContext(),
"Login Failed..\nTry Again", Toast.LENGTH_SHORT).show();
return false;
}
return true;
}
protected void onDestroy() {
super.onDestroy();
mydb.close();
}
}
NewUserActivity.java
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.signup);
Log.d(TAG, "Checkkkkkkkk");
mRegister = (Button) findViewById(R.id.buttonRegister);
mCancel = (Button) findViewById(R.id.buttonCancel);
mUsername = (EditText) findViewById(R.id.editUsername);
mPassword = (EditText) findViewById(R.id.editPassword);
mEmail = (EditText) findViewById(R.id.editEmail);
mRegister.setOnClickListener(this);
mCancel.setOnClickListener(this);
Log.d(TAG, "Checkkkkkkkk22222");
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.buttonCancel:
Intent i = new Intent(NewUserActivity.this, DatabaseActivity.class);
startActivity(i);
finish();
break;
case R.id.buttonRegister:
String uname = mUsername.getText().toString();
String pass = mPassword.getText().toString();
String email = mEmail.getText().toString();
boolean invalid = false;
if (uname.equals("")) {
invalid = true;
Toast.makeText(getApplicationContext(), "Username Missing",
Toast.LENGTH_SHORT).show();
} else if (pass.equals("")) {
invalid = true;
Toast.makeText(getApplicationContext(), "Password Missing",
Toast.LENGTH_SHORT).show();
} else if (email.equals("")) {
invalid = true;
Toast.makeText(getApplicationContext(), "Email ID Missing",
Toast.LENGTH_SHORT).show();
}
if (invalid == false) {
try {
addEntry(uname, pass, email);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Intent i_register = new Intent(NewUserActivity.this,
DatabaseActivity.class);
startActivity(i_register);
finish();
}
break;
}
}
public void addEntry(String uname, String pass, String email)
throws Exception {
SQLiteDatabase db = myDb.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("username", uname);
values.put("password", pass);
values.put("email", email);
try {
db.insert(DbHelper.SAKET_TABLE_NAME, null, values);
Toast.makeText(getApplicationContext(), "Inserted",
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
}
protected void onDestroy() {
super.onDestroy();
myDb.close();
}
答案 0 :(得分:5)
java.lang.NullPointerException because your object mydb is null
1。在mydb = new DbHelper(context);
onCreate()
或
2。删除mydb.close(); from onDestroy()
或检查null
onDestroy()
像
protected void onDestroy() {
super.onDestroy();
if(myDb!=null)
{
myDb.close();
}
}