2013-05-07 146 views
0

我試圖從編輯文本輸入框中檢索值並將它們存儲在數據庫中。我得到一個空指針異常,我不知道爲什麼。在此先感謝您的幫助:)從輸入文本中檢索值android

這是我的代碼我在哪裏獲取的價值和將它們作爲被存儲在數據庫中的參數:

public class MyPage extends Activity { 

    final Context context = this; 
    public String stringName; 
    public int intAge; 
    public int intWeight; 
    public int intHeight; 
    public String stringGoal; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.my_page); 

     final Button button = (Button) findViewById(R.id.btn_addInfo); 
     button.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       // custom dialog 
       final Dialog dialog = new Dialog(context); 
       dialog.setContentView(R.layout.dialogue_userinfo); 
       dialog.setTitle("Add Your Information"); 


       Button dialogButton = (Button) dialog.findViewById(R.id.btnDone); 
       // if button is clicked, close the custom dialog 
       dialogButton.setOnClickListener(new OnClickListener() { 
        public void onClick(View v) { 

         EditText textName = (EditText)findViewById(R.id.txtName); 
         stringName = textName.getText().toString(); 

         EditText textAge = (EditText)findViewById(R.id.txtAge); 
         intAge = Integer.parseInt(textAge.getText().toString()); 

         EditText textWeight = (EditText)findViewById(R.id.txtWeight); 
         intWeight = Integer.parseInt(textWeight.getText().toString()); 

         EditText textHeight = (EditText)findViewById(R.id.txtHeight); 
         intHeight = Integer.parseInt(textHeight.getText().toString()); 

         EditText textGoal = (EditText)findViewById(R.id.txtGoal); 
         stringGoal = textGoal.getText().toString(); 

         DatabaseAccess(); 
         dialog.dismiss(); 

        } 
       }); 

       dialog.show(); 
       Window window = dialog.getWindow(); 
       window.setLayout(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); 
       } 

     }); 
    } 

     private void DatabaseAccess(){ 

      DatabaseHandler db = new DatabaseHandler(this); 

      /** 
       * CRUD Operations 
       * */ 
      // Inserting User 
      Log.d("Insert: ", "Inserting .."); 

      db.addUser(new User(stringName, intAge, intWeight, intHeight, stringGoal)); 

      //db.addUser(new User("Peter Parker", 23, 50, 150, "Hi"));   


      // Reading all users 
      Log.d("Reading: ", "Reading all contacts.."); 
      List<User> users = db.getAllUsers();  

      for (User us : users) { 
       String log = "Id: "+us.getId()+" ,Name: " + us.getName() + " ,Age: " + us.getAge() 
         + " ,Weight: " + us.getWeight() + " ,Height: " + us.getHeight() 
         + " ,Goal: " + us.getGoal() 
         + " ,BMI: " + us.getBmi(); 

      Log.d("Name: ", log); 

     } 


    } 


} 

回答

2

如果所有的EditText的內線對話dialogue_userinfo layout然後使用對話框實例初始化按鈕上點擊的EditText的實例。這樣做:

EditText textName = (EditText)dialog.findViewById(R.id.txtName); 
    stringName = textName.getText().toString(); 

    EditText textAge = (EditText)dialog.findViewById(R.id.txtAge); 
    intAge = Integer.parseInt(textAge.getText().toString()); 
    //....same for others... 
+0

謝謝你的工作! – 2013-05-07 02:36:02

+0

@NiraliPatel:很高興能幫到你 – 2013-05-07 02:42:09

0

的getText返回Editable對象。如果你的edittext沒有任何字符,它可能會返回null Object。所以你必須檢查getText()是否返回一個空對象。

if(youreditView.getText() != null) { 
    String content = youreditView.getText().toString(); 
} 
+0

如果您的edittexts在您的對話框中。 K提供了正確的答案,不理我的。 – buptcoder 2013-05-07 02:32:06