2010-11-12 67 views
0

我想获取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(); 


    } 
+0

我假设您将文本值分配给源代码和目标代码中的其他位置? – 2010-11-12 04:21:03

+0

是的......我有..我编辑了上面的代码。请看一看。 – 2010-11-12 04:39:09

回答

1

我假设你在srcdest.xml布局中有这些编辑文本字段。如果是的话,那么你是以错误的方式初始化你的编辑文本。尝试以这种方式初始化它们:

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

啊谢谢...不知道我是如何错过了... – 2010-11-12 06:54:14