我调用这个android.os.Process.killProcess(android.os.Process.myPid())时出错了; 我的logcat说它无法启动活动。因为我使用kill进程的理解是杀死正在运行的任务但是当我使用它时它表明它开始调用另一个活动。 这是我的代码:
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();
}
任何帮助将不胜感激,谢谢。我之前使用过这个,我也是以同样的方式再次使用它,我只是不明白它为什么会这样做。
答案 0 :(得分:2)
像这样尝试
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) {
finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
在您的onDestroy()
方法调用中android.os.Process.killProcess(android.os.Process.myPid());
答案 1 :(得分:0)
EditText txt_username;
TextView lbl_user;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt_username= (EditText)findViewById(R.id.yourEditTextId);
lbl_user= (TextView )findViewById(R.id.yourTextViewId);
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();
}
答案 2 :(得分:0)
尝试使用它。
String name = LoginActivity.txt_username.getText().toString();
if(name != null)
lbl_user.setText(name);
else
lbl_user.setText("");