我从 String placeName = placeText.getText().toString(); 收到空指针异常
您好,想从编辑文本中获取地名并在地图上标记,这是我的代码,其中出现空指针异常,请帮助我应该做什么以及哪里出错了。
因为我从对话框中的编辑文本字段获取地名。
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 ;
}
Hi want to get the place name from the edit text and mark on map here is my code where i got null pointer exception please help me what i should do and where i am going wrong.
as I am getting the place name from the edit text field in dialog box.
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 ;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
NullPointerExceptions
是最容易查找和调试的异常。如果您在此行遇到
NullPointerException
:唯一可以为 null 的是 placeText 变量,或
placeText.getText()
您需要找出原因它为空。由于您的 placeText 处于警报状态,因此您应该使用layout.findViewById 从布局中获取它。当您从错误的位置获取它时,它将为 null,从而导致
NullPointerException
。理论上,
getText()
也可以返回null,但是Android SDK中当前的实现在没有填写任何内容时会返回一个空字符串,因此不能为null。NullPointerExceptions
are the most easy exceptions to find and debug.If you're getting a
NullPointerException
on this line :The only thing that can be null is the placeText variable, or the
placeText.getText()
You need to figure out why it is null. As your placeText is on the alert, so you should fetch it from the layout using layout.findViewById. As your fetching it from the wrong place, it will be null, causing the
NullPointerException
.in theory,
getText()
could also return null, but the current implementation in the Android SDK will return an empty string when nothing is filled in, so that can't be null.使用
EditText placeText = (EditText) layout.findViewById(R.id.strtplace);
而不是EditText placeText = (EditText) findViewById(R.id.strtplace);
use
EditText placeText = (EditText) layout.findViewById(R.id.strtplace);
intead ofEditText placeText = (EditText) findViewById(R.id.strtplace);
这是您需要做的更改:
//视图和editext都应该声明为final,并且findViewById方法需要一个布局来调用它
heres the changes that you need to do:
//both the view and the editext should be declared as final and the findViewById method needs a layout on which it is to be called