2014-03-04 23 views
0

我調用這個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(); 

      } 

任何幫助將不勝感激,謝謝。我以前用過這個,我用同樣的方法再次使用它,我只是不知道它爲什麼這樣做。

+3

你得到哪個錯誤?你能提供你的logcat輸出嗎? – Solution

+0

好吧,生病吧。等待我的編輯。謝謝。 – Drx

+0

引起:java.lang.NullPointerException 03-04 18:28:43.805:E/AndroidRuntime(19417):at ph.com.example.Project.MainActivity.get_Territory(MainActivity.java:169) – saa

回答

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

如果你發佈你的日誌貓,你會得到準確的答案... – saa

+0

是的,它的工作原理,但它影響到我的登錄按鈕,它與取消按鈕一樣。 – Drx

+0

試試我的第二個解決方案 – saa

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(); 

       } 
0

嘗試使用此。

String name = LoginActivity.txt_username.getText().toString(); 
if(name != null) 
    lbl_user.setText(name); 
else 
    lbl_user.setText("");