android中ViewRoot.draw(boolean)中的空指针异常
这是我的代码 中的 ViewRoot.draw(boolean) 中获取空指针异常
在 android logcatView
Travellog [Android Application]
DalvikVM[localhost:8616]
Thread [<1> main] (Suspended (exception NullPointerException))
ViewRoot.draw(boolean) line: 1546
ViewRoot.performTraversals() line: 1258
ViewRoot.handleMessage(Message) line: 1859
ViewRoot(Handler).dispatchMessage(Message) line: 99
Looper.loop() line: 130
ActivityThread.main(String[]) line: 3683
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 507
ZygoteInit$MethodAndArgsCaller.run() line: 839
ZygoteInit.main(String[]) line: 597
NativeStart.main(String[]) line: not available [native method]
Thread [<8> Binder Thread #2] (Running)
Thread [<7> Binder Thread #1] (Running)
Daemon Thread [<10> [email protected]@405a4408] (Running)
类
LayoutInflater li= LayoutInflater.from(this);
View textEntryView = li.inflate(R.layout.alertbox, null);
final AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("Enter the places");
dialog.setView(textEntryView);
splc = (EditText) textEntryView.findViewById(R.id.strtplace);
dialog.setPositiveButton("Ok", new OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
pd = ProgressDialog.show(TravellogActivity.this, "Working..", "Searching your address", true, false);
//Show a progress dialog
searchAdress = new Thread(){
public void run(){
String addressInput = splc.getText().toString(); // Get input text
try {
foundAdresses = gc.getFromLocationName(addressInput, 5); // Search addresses
Thread.sleep(1500);
} catch (Exception e) {
// @todo: Show error message
}
showAdressResults.sendEmptyMessage(0);
}
};
searchAdress.start();
}
});
dialog.show();
private Handler showAdressResults = new Handler() {
@Override
public void handleMessage(Message msg) {
pd.dismiss();
if (foundAdresses.size() == 0) { // if no address found,
// display an error
//
// p = new GeoPoint((int) (foundAdresses.get(0).getLatitude() * 1E6),
// (int) (foundAdresses.get(0).getLongitude() * 1E6));
//
//
Dialog locationError = new AlertDialog.Builder(
TravellogActivity.this).setIcon(0).setTitle(
"Error").setPositiveButton(R.string.ok, null)
.setMessage(
"Sorry, your address doesn't exist.")
.create();
locationError.show();
} else { // else display address on map
for (int i = 0; i < foundAdresses.size(); ++i) {
// Save results as Longitude and Latitude
// @todo: if more than one result, then show a
// select-list
Address x = foundAdresses.get(i);
latit = x.getLatitude();
longi = x.getLongitude();
}
navigateToLocation((latit * 1000000), (longi * 1000000),myMapView); // display the found address
};
return ;
}
private void navigateToLocation(double latitude, double longitude,
MapView myMapView) {
// TODO Auto-generated method stub
p = new GeoPoint((int) (foundAdresses.get(0).getLatitude() * 1E6),
(int) (foundAdresses.get(0).getLongitude() * 1E6));
MC.animateTo(p);
myMapView.invalidate();
MyLocationOverlay 类
protected class MyLocationOverlay extends com.google.android.maps.Overlay {
@Override
public boolean draw(Canvas canvas, MapView myMapView, boolean shadow, long when) {
Paint paint = new Paint();
super.draw(canvas, myMapView, shadow);
// Converts lat/lng-Point to OUR coordinates on the screen.
Point myScreenCoords = new Point();
myMapView.getProjection().toPixels(geoPoint, myScreenCoords);
Point Screencoords = new Point();
myMapView.getProjection().toPixels(p, Screencoords);
paint.setStrokeWidth(1);
paint.setARGB(255, 255, 255, 255);
paint.setStyle(Paint.Style.STROKE);
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.marker);
canvas.drawBitmap(bmp, myScreenCoords.x, myScreenCoords.y, paint);
canvas.drawText("I am here...", myScreenCoords.x, myScreenCoords.y, paint);
here is my code
getting null pointer Exception in ViewRoot.draw(boolean) in android
the logcatView
Travellog [Android Application]
DalvikVM[localhost:8616]
Thread [<1> main] (Suspended (exception NullPointerException))
ViewRoot.draw(boolean) line: 1546
ViewRoot.performTraversals() line: 1258
ViewRoot.handleMessage(Message) line: 1859
ViewRoot(Handler).dispatchMessage(Message) line: 99
Looper.loop() line: 130
ActivityThread.main(String[]) line: 3683
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 507
ZygoteInit$MethodAndArgsCaller.run() line: 839
ZygoteInit.main(String[]) line: 597
NativeStart.main(String[]) line: not available [native method]
Thread [<8> Binder Thread #2] (Running)
Thread [<7> Binder Thread #1] (Running)
Daemon Thread [<10> [email protected]@405a4408] (Running)
Class
LayoutInflater li= LayoutInflater.from(this);
View textEntryView = li.inflate(R.layout.alertbox, null);
final AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("Enter the places");
dialog.setView(textEntryView);
splc = (EditText) textEntryView.findViewById(R.id.strtplace);
dialog.setPositiveButton("Ok", new OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
pd = ProgressDialog.show(TravellogActivity.this, "Working..", "Searching your address", true, false);
//Show a progress dialog
searchAdress = new Thread(){
public void run(){
String addressInput = splc.getText().toString(); // Get input text
try {
foundAdresses = gc.getFromLocationName(addressInput, 5); // Search addresses
Thread.sleep(1500);
} catch (Exception e) {
// @todo: Show error message
}
showAdressResults.sendEmptyMessage(0);
}
};
searchAdress.start();
}
});
dialog.show();
private Handler showAdressResults = new Handler() {
@Override
public void handleMessage(Message msg) {
pd.dismiss();
if (foundAdresses.size() == 0) { // if no address found,
// display an error
//
// p = new GeoPoint((int) (foundAdresses.get(0).getLatitude() * 1E6),
// (int) (foundAdresses.get(0).getLongitude() * 1E6));
//
//
Dialog locationError = new AlertDialog.Builder(
TravellogActivity.this).setIcon(0).setTitle(
"Error").setPositiveButton(R.string.ok, null)
.setMessage(
"Sorry, your address doesn't exist.")
.create();
locationError.show();
} else { // else display address on map
for (int i = 0; i < foundAdresses.size(); ++i) {
// Save results as Longitude and Latitude
// @todo: if more than one result, then show a
// select-list
Address x = foundAdresses.get(i);
latit = x.getLatitude();
longi = x.getLongitude();
}
navigateToLocation((latit * 1000000), (longi * 1000000),myMapView); // display the found address
};
return ;
}
private void navigateToLocation(double latitude, double longitude,
MapView myMapView) {
// TODO Auto-generated method stub
p = new GeoPoint((int) (foundAdresses.get(0).getLatitude() * 1E6),
(int) (foundAdresses.get(0).getLongitude() * 1E6));
MC.animateTo(p);
myMapView.invalidate();
MyLocationOverlay class
protected class MyLocationOverlay extends com.google.android.maps.Overlay {
@Override
public boolean draw(Canvas canvas, MapView myMapView, boolean shadow, long when) {
Paint paint = new Paint();
super.draw(canvas, myMapView, shadow);
// Converts lat/lng-Point to OUR coordinates on the screen.
Point myScreenCoords = new Point();
myMapView.getProjection().toPixels(geoPoint, myScreenCoords);
Point Screencoords = new Point();
myMapView.getProjection().toPixels(p, Screencoords);
paint.setStrokeWidth(1);
paint.setARGB(255, 255, 255, 255);
paint.setStyle(Paint.Style.STROKE);
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.marker);
canvas.drawBitmap(bmp, myScreenCoords.x, myScreenCoords.y, paint);
canvas.drawText("I am here...", myScreenCoords.x, myScreenCoords.y, paint);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从您的代码中我没有看到任何直接关系,但是,我遇到了类似的问题。我发现如果我在 MapView 上应用一个空的 ItemizedOverlay ,它会崩溃,删除此代码并添加:“setLastFocusedIndex(-1);”我的自定义 ItemizedOverlay“addOverlay()”和“clear()”方法解决了这个问题。
希望这有帮助。
From your code I am not seeing any direct relation, however, I had a similar issue. I found that if I was applying an empty ItemizedOverlay on the MapView it would crash, removing this code and adding: "setLastFocusedIndex(-1);" to my custom ItemizedOverlay "addOverlay()" and "clear()" methods solved the issue.
Hopefully this helps.