android对话框 - edittext始终为NULL

时间:2014-07-06 20:19:27

标签: android android-activity android-dialog

我花了很多时间在这上面,我在这个网站上阅读了很多类似问题的消息,但我无法解决= [请帮我解决问题

    public void button() {

    button = (Button) findViewById(R.id.hadash);
    button.setOnClickListener(new OnClickListener() {

        private String newweight2;

        @Override
        public void onClick(View view) {
            final Dialog dialog = new Dialog(Activity3.this);
            LayoutInflater inflater = (LayoutInflater) getLayoutInflater();
            View content = inflater.inflate(R.layout.dialog, null);
            dialog.setContentView(content);
            dialog.setTitle("הוספת מעקב משקל");

            Button dialogButton = (Button) content.findViewById(R.id.button1);
            EditText newweight  = (EditText) content.findViewById(R.id.editText1);
            newweight2 = newweight.getText().toString();


            dialogButton.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {

              //   int newweight2=Integer.parseInt(newweight.getText().toString());

              //      SQLiteDatabase db;
             //       db = openOrCreateDatabase( "weight.db" , SQLiteDatabase.CREATE_IF_NECESSARY,null);

              //      String sql = "INSERT INTO weight_listview (weight,date) VALUES(" + newweight + ",'03/04/2014')" ;       
              //      db.execSQL(sql);

                   Toast.makeText(Activity3.this,"המשקל " + newweight2 + " נקלט בהצלחה", Toast.LENGTH_LONG).show();
                    dialog.dismiss();
                }
            });
            dialog.show();
        }

    });
}

请帮助= [我真的很累

1 个答案:

答案 0 :(得分:1)

移动此行:

newweight2 = newweight.getText().toString();


从目前的位置到:

   dialogButton.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {

                   newweight2 = newweight.getText().toString();  //<---------------------------

                   Toast.makeText(Activity3.this,"המשקל " + newweight2 + " נקלט בהצלחה", Toast.LENGTH_LONG).show();
                   dialog.dismiss();
                }