0

我正在尝试从编辑文本输入框中检索值并将它们存储在数据库中。我得到一个空指针异常,我不知道为什么。在此先感谢您的帮助 :)

这是我的代码,我在其中检索值并将它们作为要存储在数据库中的参数传递:

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

         }


    }


}
4

2 回答 2

2

如果所有 EditText 都在 Dialogdialogue_userinfo layout中,则使用 Dialog 实例在按钮单击时初始化 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...
于 2013-05-07T02:27:40.600 回答
0

getText 将返回Editable对象。如果您的 edittext 没有任何字符,它可能会返回 null 对象。所以你必须检查 getText() 是否返回一个空对象。

if(youreditView.getText() != null ) {
  String content = youreditView.getText().toString();
}
于 2013-05-07T02:27:34.087 回答