我調用這個android.os.Process.killProcess(android.os.Process.myPid())時出現錯誤; 我的logcat說它無法啓動活動。因爲我的理解使用殺死進程是爲了殺死正在運行的任務,但是當我使用它時,它表明它正在開始調用另一個活動。 這裏是我的代碼:android.os.Process.killProcess(android.os.Process.myPid())中的錯誤;
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(S_2nd_LoginActivity.this);
alertDialogBuilder.setTitle("Are you sure you want to cancel?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
android.os.Process.killProcess(android.os.Process.myPid());
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
這裏是我的logcat:
03-04 18:28:43.805: E/AndroidRuntime(19417): java.lang.RuntimeException: Unable to start activity ComponentInfo{ph.com.example.Project/ph.com.example.Project.MainActivity}: java.lang.NullPointerException
03-04 18:28:43.805: E/AndroidRuntime(19417): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1815)
03-04 18:28:43.805: E/AndroidRuntime(19417): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1831)
03-04 18:28:43.805: E/AndroidRuntime(19417): at android.app.ActivityThread.access$500(ActivityThread.java:122)
03-04 18:28:43.805: E/AndroidRuntime(19417): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1024)
03-04 18:28:43.805: E/AndroidRuntime(19417): at android.os.Handler.dispatchMessage(Handler.java:99)
03-04 18:28:43.805: E/AndroidRuntime(19417): at android.os.Looper.loop(Looper.java:132)
03-04 18:28:43.805: E/AndroidRuntime(19417): at android.app.ActivityThread.main(ActivityThread.java:4123)
03-04 18:28:43.805: E/AndroidRuntime(19417): at java.lang.reflect.Method.invokeNative(Native Method)
03-04 18:28:43.805: E/AndroidRuntime(19417): at java.lang.reflect.Method.invoke(Method.java:491)
03-04 18:28:43.805: E/AndroidRuntime(19417): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
03-04 18:28:43.805: E/AndroidRuntime(19417): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
03-04 18:28:43.805: E/AndroidRuntime(19417): at dalvik.system.NativeStart.main(Native Method)
03-04 18:28:43.805: E/AndroidRuntime(19417): Caused by: java.lang.NullPointerException
03-04 18:28:43.805: E/AndroidRuntime(19417): at ph.com.example.Project.MainActivity.get_Territory(MainActivity.java:169)
03-04 18:28:43.805: E/AndroidRuntime(19417): at ph.com.example.Project.MainActivity.onCreate(MainActivity.java:74)
03-04 18:28:43.805: E/AndroidRuntime(19417): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050)
03-04 18:28:43.805: E/AndroidRuntime(19417): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1779)
這裏是我獲得領地的方法:
public void get_Territory() {
String territory = String.valueOf(LoginActivity.g_territory);
lbl_user.setText(LoginActivity.txt_username.getText().toString());
String s = l_databaseHandler.get_territoryCode(territory);
String f = l_databaseHandler.get_doctorFname(s);
String l = l_databaseHandler.get_doctorLname(s);
Cursor cursorAllDetails = l_databaseHandler.get_all_sameDoctorname(f, l);
if (cursorAllDetails.getCount() != 0){
fillData(cursorAllDetails);
}else{
save_territory();
}
}
,我調用這個方法對我的onCreate這樣:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
l_databaseHandler = new DatabaseHandler(this);
if (l_databaseHandler != null) {
l_databaseHandler.close();
l_databaseHandler.createDB();
}
Constants.from_add = false;
Constants.from_edit = false;
Constants.from_view = false;
cast_views();
get_Territory();
log_out();
}
任何幫助將不勝感激,謝謝。我以前用過這個,我用同樣的方法再次使用它,我只是不知道它爲什麼這樣做。
你得到哪個錯誤?你能提供你的logcat輸出嗎? – Solution
好吧,生病吧。等待我的編輯。謝謝。 – Drx
引起:java.lang.NullPointerException 03-04 18:28:43.805:E/AndroidRuntime(19417):at ph.com.example.Project.MainActivity.get_Territory(MainActivity.java:169) – saa