如何使用EditText的setText方法?

时间:2011-11-07 10:16:29

标签: android

我在editText()上使用setText(),它在模拟器上正常工作但是Activity在Device上强制关闭。我想按字符串设置文本我从数据库中检索。任何想法我怎么能摆脱提前谢谢。

import co.a.utility.WS_Url;

public class MyAccountActivity extends Activity implements View.OnClickListener{

    Button myRequests,edit,backOnMyAccount;
    EditText name, phone;
    ToggleButton tracking;
    String myid,onOff,s1="A",s2="B",s3="True";
    Boolean MyTracking_onOff;

    @Override
    public void onCreate(Bundle savedInstanceState){

        super.onCreate(savedInstanceState);
        setContentView(R.layout.myaccount);
        /* Getting IMEI number of Android*/
        TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
        myid=telephonyManager.getDeviceId();

        name=(EditText)findViewById(R.id.myaccount_name);
        //name.setText("puneet",TextView.BufferType.EDITABLE);
        phone=(EditText)findViewById(R.id.myaccount_phone);
        edit=(Button)findViewById(R.id.Editmyaccount_button);
        myRequests=(Button)findViewById(R.id.myAccArrowBtn);
        backOnMyAccount=(Button)findViewById(R.id.button_backOnMyAccount);
        tracking=(ToggleButton)findViewById(R.id.myAccTracking_toggleButton);

        //getMap(myid);


        backOnMyAccount.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent i=new Intent();
                i.setClassName("co.a.activity","co.a.activity.FamilyFinderActivity");
                startActivity(i);
                finish();

            }
         });

        name.setText("puneet");
        phone.setText("123456789");

        //edit=new Button(getApplicationContext());

        //edit.setOnClickListener(this);





        myRequests.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent i1 = new Intent();
                i1.setClassName("co.a.activity", "co.a.activity.MyRequestsActivity");
                startActivity(i1);
                finish();

            }
        });



        //tracking.setClickable(false);
        /*String temp="True";
        if(s3.equals(temp)){

        MyTracking_onOff=true;
        tracking.setChecked(true);
        }
        else{
            MyTracking_onOff=false;
            tracking.setChecked(false);
        }*/
        tracking.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    AlertDialog aboutDialog = new AlertDialog.Builder(MyAccountActivity.this).create();
                    aboutDialog.setMessage("Your parents will still be able to track you");
                    aboutDialog.setButton("OK" ,new DialogInterface.OnClickListener() {
                           public void onClick(DialogInterface dialog, int which) {
                                  // here you can add functions
                               MyTracking_onOff=true;


                               }
                            });
                    aboutDialog.show();

                }else{
                    MyTracking_onOff=false;
                }

            }
         });

    }

    @Override
    public void onClick(View v) {
        /*if(v==myRequests){
            Intent i = new Intent();
            i.setClassName("co.a.activity", "co.a.activity.MyRequestsActivity");
            startActivity(i);
            finish();
        }*/
        if(v==edit){
            if(edit.getText().equals("Edit")){
                edit.setText("Done");
                name.setInputType(1);
                name.setEnabled(true);
                name.setClickable(true);
                name.setFocusable(true);
                phone.setEnabled(true);
                phone.setClickable(true);
                phone.setFocusable(true);
                tracking.setClickable(true);
            }
            else{
                edit.setText("Edit");
                name.setInputType(0);
                name.setEnabled(false);
                phone.setEnabled(false);
                tracking.setClickable(false);
                HttpClient httpClient = new DefaultHttpClient();
                HttpContext localContext = new BasicHttpContext();
                HttpGet httpGet = new HttpGet(WS_Url.editAccount1+name+WS_Url.editAccount2+myid+WS_Url.editAccount3+phone+WS_Url.editAccount4+MyTracking_onOff+WS_Url.editAccount5);
                //Hard Coded
                //HttpGet httpGet = new HttpGet(WS_Url.editAccount1+name.getText().toString()+WS_Url.editAccount2+"dj7o78pugxgjrsu657"+WS_Url.editAccount3+phone.getText().toString()+WS_Url.editAccount4+MyTracking_onOff+WS_Url.editAccount5);
                try {
                    HttpResponse response = httpClient.execute(httpGet, localContext);
                } catch (ClientProtocolException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } 


                Intent i2 = new Intent();
                i2.setClassName("co.a.activity", "co.a.activity.FamilyFinderActivity");
                startActivity(i2);
                finish();

            } 

        }
    }

    public void getMap(String mid){ 
        String xml = XMLfunctions.getXML(WS_Url.selectMember1+mid+WS_Url.selectMember2).substring(42).trim();

        /*Convert xml string to DOM*/
        Document doc = XMLfunctions.XMLfromString(xml);

        /*Getting elements where name is tblRequest*/
        NodeList nodes = doc.getElementsByTagName("tblMember");

        for (int i = 0; i < nodes.getLength(); i++) {                           

            Element e = (Element)nodes.item(i);
             s1= XMLfunctions.getValue(e, "Name");
             s2= XMLfunctions.getValue(e, "PhoneNumber");
             s3=XMLfunctions.getValue(e, "TrackingEnabled");


        }



    }




}

1 个答案:

答案 0 :(得分:0)

您的布局myaccount位于何处?考虑到不同的屏幕尺寸,在仿真器和设备上是否相同?

我会检查变量“name”是否为null,这意味着在myaccount中找不到EditTest myaccount_name。