Java 我从字符串placeName=placeText.getText().toString()获得空指针异常;
嗨,想从地图上的编辑文本和标记中得到地名这里是我的代码,我在这里得到了空指针异常,请帮助我应该做什么,我哪里出错了 当我从对话框中的编辑文本字段中获取地名时Java 我从字符串placeName=placeText.getText().toString()获得空指针异常;,java,android,maps,Java,Android,Maps,嗨,想从地图上的编辑文本和标记中得到地名这里是我的代码,我在这里得到了空指针异常,请帮助我应该做什么,我哪里出错了 当我从对话框中的编辑文本字段中获取地名时 View layout = View.inflate(this, R.layout.alertbox, null); AlertDialog.Builder dialog = new AlertDialog.Builder(this); dialog.setTitle("Enter the
View layout = View.inflate(this, R.layout.alertbox, null);
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("Enter the places");
dialog.setView(layout);
dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
// TODO Auto-generated method stub
EditText placeText = (EditText) findViewById(R.id.strtplace);
String placeName = placeText.getText().toString();
//
Break from execution if the user has not entered anything in the field
if(placeName.compareTo("")==0)
numberOptions = 5;
String [] optionArray = new String[numberOptions];
Geocoder gcoder = new Geocoder(TravellogActivity.this);
try{
List<Address> results = gcoder.getFromLocationName(placeName,numberOptions);
Iterator<Address> locations = results.iterator();
String raw = "\nRaw String:\n";
String country;
int opCount = 0;
while(locations.hasNext()){
Address location = locations.next();
lat = location.getLatitude();
lon = location.getLongitude();
country = location.getCountryName();
if(country == null) {
country = "";
} else {
country = ", "+country;
}
raw += location+"\n";
optionArray[opCount] = location.getAddressLine(0)+", "+location.getAddressLine(1)+country+"\n";
opCount ++;
}
Log.i("Location-List", raw);
Log.i("Location-List","\nOptions:\n");
for(int i=0; i<opCount; i++){
Log.i("Location-List","("+(i+1)+") "+optionArray[i]);
}
} catch (IOException e){
Log.e("Geocoder", "I/O Failure; is network available?",e);
}
p = new GeoPoint(latE6, lonE6);
myMC.animateTo(p);
}
});
dialog.show();
break;
}
return false ;
}
View layout=View.充气(这个,R.layout.alertbox,空);
AlertDialog.Builder dialog=新建AlertDialog.Builder(此);
对话框.setTitle(“输入位置”);
对话框.setView(布局);
setPositiveButton(“确定”,新的DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface对话框,int-id){
//TODO自动生成的方法存根
EditText placeText=(EditText)findViewById(R.id.strtplace);
字符串placeName=placeText.getText().toString();
//
如果用户未在字段中输入任何内容,则中断执行
if(placeName.compareTo(“”==0)
次数=5;
String[]optionArray=新字符串[numberOptions];
地理编码器gcoder=新的地理编码器(TravellogActivity.this);
试一试{
列表结果=gcoder.getFromLocationName(地名、数字选项);
迭代器位置=results.Iterator();
String raw=“\n原始字符串:\n”;
弦国;
int optcount=0;
while(locations.hasNext()){
地址位置=位置。下一步();
lat=位置。getLatitude();
lon=location.getLongitude();
country=location.getCountryName();
如果(国家==null){
国家=”;
}否则{
country=“,”+国家;
}
原始+=位置+“\n”;
optionArray[opCount]=location.getAddressLine(0)+“,“+location.getAddressLine(1)+国家+”\n”;
opCount++;
}
Log.i(“位置列表”,原始);
Log.i(“位置列表”,“选项:\n”);
对于(inti=0;i使用EditText-placeText=(EditText)布局。findViewById(R.id.strtplace);
intead ofEditText-placeText=(EditText)findViewById(R.id.strtplace);
NullPointerException
是最容易查找和调试的异常
如果您在这一行上得到一个NullPointerException
:
String placeName = placeText.getText().toString();
唯一可以为null的是placeText变量或placeText.getText()
您需要找出它为空的原因。由于placeText处于警报状态,因此您应该使用layout.findViewById从版面中获取它。由于从错误的位置获取它,它将为空,从而导致NullPointerException
理论上,getText()
也可以返回null,但Android SDK中的当前实现在未填充任何内容时将返回空字符串,因此不能为null。以下是您需要做的更改:
//视图和editText都应该声明为final,findViewById方法需要一个要调用它的布局
- 最终视图布局=视图。充气(此,R.layout.alertbox,空);
- final EditText placeText=(EditText)layout.findViewById(R.id.strtplace)
如果答案对您有用,您应该接受它(单击答案旁边的V形复选图标)