我在尝试从可绘制文件中获取图像并调整其大小并绘制到地图上时遇到了一些问题。这是代码:
final Drawable d = EventDrawableImage.resizeCurrentLocImage("current_loc",
context);
for (int iii = 0; iii < pathGeometries.size(); iii++) {
final int counter = iii ;
EventDetail.handler.postDelayed(new Runnable()
{
private long time = 0;
public void run()
{
time += 1000;
EventDetail.handler.postDelayed(this, 1000);
moveNext(pathGeometries.get(counter).getX(),
pathGeometries.get(counter).getY(), 0, d);
}
}, 1000);
}
在我的moveNext()方法中,我正在将标记绘制到地图上:
public static void moveNext(double coordx, double coordy, int k, Drawable d){
// Set center
Point p = new Point(coordx, coordy);
EventMain.mMapView.zoomToResolution(p, 1);
// Add marker
PictureMarkerSymbol graphicIcon;
graphicIcon = new PictureMarkerSymbol(d);
Symbol symbol = graphicIcon;
HashMap<String, Object> attrMap = new HashMap<String, Object>();
attrMap.put("currentLoc", "User Current location");
EventMain.graphicsLayer.addGraphic(new Graphic(p, symbol, attrMap));
}
我的resizeCurrentLoc()方法:
public static Drawable resizeCurrentLocImage(String locPic, Context context) {
String uri = "@drawable/" + locPic;
int imageResource = context.getResources().getIdentifier(uri, null,
context.getPackageName());
Drawable res = context.getResources().getDrawable(imageResource);
Bitmap bitmap = ((BitmapDrawable) res).getBitmap();
Drawable d = new BitmapDrawable(context.getResources(),
Bitmap.createScaledBitmap(bitmap, 40, 40, true));
return d;
}
我能够使用返回的drawable并通过其他函数将其绘制到地图上,但不是这样。我怀疑这是因为处理程序的东西。作为堆栈跟踪的错误消息:
01-17 19:44:27.568: E/AndroidRuntime(5886): FATAL EXCEPTION: AsyncTask #5
01-17 19:44:27.568: E/AndroidRuntime(5886): java.lang.RuntimeException: An error occured while executing doInBackground()
01-17 19:44:27.568: E/AndroidRuntime(5886): at android.os.AsyncTask$3.done(AsyncTask.java:278)
01-17 19:44:27.568: E/AndroidRuntime(5886): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
01-17 19:44:27.568: E/AndroidRuntime(5886): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
01-17 19:44:27.568: E/AndroidRuntime(5886): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
01-17 19:44:27.568: E/AndroidRuntime(5886): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
01-17 19:44:27.568: E/AndroidRuntime(5886): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
01-17 19:44:27.568: E/AndroidRuntime(5886): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
01-17 19:44:27.568: E/AndroidRuntime(5886): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
01-17 19:44:27.568: E/AndroidRuntime(5886): at java.lang.Thread.run(Thread.java:856)
01-17 19:44:27.568: E/AndroidRuntime(5886): Caused by: java.lang.NullPointerException
01-17 19:44:27.568: E/AndroidRuntime(5886): at Extra.EventDrawableImage.resizeCurrentLocImage(EventDrawableImage.java:37)
01-17 19:44:27.568: E/AndroidRuntime(5886): at Controller.EventController.getDirection(EventController.java:308)
01-17 19:44:27.568: E/AndroidRuntime(5886): at AsyncTask.GetEventDirectionAsyncTask.doInBackground(GetEventDirectionAsyncTask.java:36)
01-17 19:44:27.568: E/AndroidRuntime(5886): at AsyncTask.GetEventDirectionAsyncTask.doInBackground(GetEventDirectionAsyncTask.java:1)
01-17 19:44:27.568: E/AndroidRuntime(5886): at android.os.AsyncTask$2.call(AsyncTask.java:264)
01-17 19:44:27.568: E/AndroidRuntime(5886): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
01-17 19:44:27.568: E/AndroidRuntime
任何想法,因为它是为其他功能工作,但不是这个?提前谢谢。
答案 0 :(得分:1)
下面:
int imageResource = context.getResources().getIdentifier(uri, null,
context.getPackageName());
getIdentifier方法需要:
。资源名称作为第一个参数
。资源目录名称,如raw,drawable,..作为第二个参数
。应用程序包名称为第三个参数
因此,使用name as:
获取资源ID int imageResource = context.getResources().getIdentifier(locPic, "drawable",
context.getPackageName());
并确保context
不是null
。
答案 1 :(得分:1)
很有可能
context.getResources().getIdentifier(uri, null,
context.getPackageName());
返回null,它不能存储在int中(它可能在整数中),因此会导致NPE。
但null的返回值可能表示出现了问题 - 请参阅其他答案。