2013-05-07 2 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

대화 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

고마워요! –

+0

@NiraliPatel : 다행히 도울 수있다. –

0

gettext에이 Editable 객체를 반환합니다 덕분에 도움 :)

에 대한 사전에이 매개 변수가 데이터베이스에 저장 될 나는이 값을 검색하고이를 통과하고 내 코드입니다. edittext에 문자가 없으면 null 객체를 반환 할 수 있습니다. 따라서 getText()가 null 객체를 반환하는지 여부를 확인해야합니다.

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

편집 텍스트가 대화 상자에있는 경우. ρяσѕρєя K 님이 정확한 답변을 제공했지만 무시하겠습니다. – buptcoder

관련 문제