简体   繁体   中英

getting null values from editext in android

I got a problem with edit text...here i will explain regarding my problem

  1. I declared a fragment in fragmentactivity.
  2. In that fragment i have an edittext.
  3. I have a button in fragmnetactivity.
  4. If i click on that button I need to get values from that edittext which is in fragment.
  5. So fro this I declared that edittext globally.
  6. And It asked me to declare with "static".
  7. Until this it is fine but while getting values it was giving null values.
  8. I think the problem is occurred by declaring it as static.

I tried with all other ways but didn't found good solution. Can i know what the fault i have done with... Hope I have given required details to clear my question.If you feel it as insufficient please let me know it..

In fragment....
edt = (EditText) rootView.findViewById(R.id.note);

In fragmentactivity

static EditText edt;
static String a;
button.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        a = edt.getText().toString();
        Log.v("a",""+a);
}
});

I got a good solution for this....in android if you declare editext in fragment and to get values in activity it will give null values but the best part of android is if you dont get it in plan A it will show remaing 25 alphabets...So you have to implement it in anothe manner I will sugest you one implementation remove static from the edittext and keep it in fragment...

edt.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
                Editable b=s;
                a=b.toString();//use this a where ever you want in activity


            }
        });

You defined EditText. But did you initialized it by calling findViewById or something?

or did you defined it twice?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM