我试图在用户单击导航按钮时获取src和dest中的值,但它始终为null。可以帮助一些人帮助
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 :(得分: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);