невозможно получить значение edittext - PullRequest
0 голосов
/ 12 ноября 2010

Я пытаюсь получить значение в src и dest, когда пользователь нажимает кнопку навигации, но оно всегда равно нулю. Может ли кто-нибудь PLZ помочь

static String source  = "currentLocation"; 
    static String destination  = null;

private void getSrcDest(){
        LayoutInflater factory = LayoutInflater.from(this);
        final View textEntryView = factory.inflate(R.layout.srcdest, null);
        final AlertDialog.Builder alert = new AlertDialog.Builder(this);
        alert.setIcon(R.drawable.srcdest_icon);
        alert.setTitle("Enter Source & Destination");
        alert.setView(textEntryView);
        final EditText sourceText = (EditText) findViewById(R.id.sourcetext);
        final EditText destinationText = (EditText) findViewById(R.id.desttext);

        alert.setPositiveButton("Navigate", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int whichButton) {
            try{
                Toast.makeText(getApplicationContext(), sourceText.getText().toString()+destinationText.getText().toString(),
                        Toast.LENGTH_SHORT).show();
                source = sourceText.getText().toString();
                destination = destinationText.getText().toString();
                System.out.println("----------------Source:"+source);
                System.out.println("-----------Destination:"+destination);
            }
            catch(Exception e){
                System.out.println("----------------Source:"+source);
                System.out.println("-----------Destination:"+destination);
                //if(destination.equals(null)){
                    final AlertDialog.Builder wrongAddressAlert = new AlertDialog.Builder(MainActivity.this);
                    wrongAddressAlert.setTitle("Destination Address Cannot be Null");
                    wrongAddressAlert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            getSrcDest();
                        }
                    });
                    wrongAddressAlert.show();


            //}
                }   
            }


            });
        alert.setNegativeButton("Cancel",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        dialog.cancel();
                    }
                });
        alert.show();


    }

1 Ответ

1 голос
/ 12 ноября 2010

Я предполагаю, что у вас есть эти текстовые поля для редактирования в srcdest.xml Layout. Если да, то вы неправильно инициализируете ваши тексты редактирования. Попробуйте инициализировать их следующим образом:

LayoutInflater factory = LayoutInflater.from(this);
    final View textEntryView = factory.inflate(R.layout.srcdest, null);

    final EditText sourceText = (EditText) textEntryView.findViewById(R.id.sourcetext);
    final EditText destinationText = (EditText) textEntryView.findViewById(R.id.desttext);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...