将Mapview保存为位图

时间:2011-03-03 11:04:05

标签: android android-mapview

我正在尝试将mapview保存为位图图片,但是我得到空指针异常我已经检查了所有帖子但是找不到问题的解决方案,我的代码如下: / p>

public class Customview extends MapActivity{
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        MapView mapview = new MapView(this, "*************MapApikey****");

        MapController mc = mapview.getController();
        GeoPoint p = new GeoPoint((int) (36 * 1E6),(int) (36 * 1E6));
        mc.animateTo(p);
        mc.setZoom(20);
        mapview.layout(0,0,400,400);
        Bitmap screenshot;
        mapview.setDrawingCacheEnabled(true);
        screenshot = Bitmap.createBitmap(mapview.getDrawingCache()); //this line gives error 
        mapview.setDrawingCacheEnabled(false);
    }
//Other map functions automatically included here
}

我想要的是我应该能够在手机上保存该位图并将其用于其他活动。

错误日志:

03-03 10:57:01.948: ERROR/AndroidRuntime(8303): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.abc.myproject.Customview}: java.lang.NullPointerException
03-03 10:57:01.948: ERROR/AndroidRuntime(8303):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2781)
03-03 10:57:01.948: ERROR/AndroidRuntime(8303):     at android.app.ActivityThread.startActivityNow(ActivityThread.java:2621)
03-03 10:57:01.948: ERROR/AndroidRuntime(8303):     at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
03-03 10:57:01.948: ERROR/AndroidRuntime(8303):     at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
03-03 10:57:01.948: ERROR/AndroidRuntime(8303):     at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:651)
03-03 10:57:01.948: ERROR/AndroidRuntime(8303):     at android.widget.TabHost.setCurrentTab(TabHost.java:323)
03-03 10:57:01.948: ERROR/AndroidRuntime(8303):     at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:129)
03-03 10:57:01.948: ERROR/AndroidRuntime(8303):     at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:453)
03-03 10:57:01.948: ERROR/AndroidRuntime(8303):     at android.view.View.performClick(View.java:2408)
03-03 10:57:01.948: ERROR/AndroidRuntime(8303):     at android.view.View$PerformClick.run(View.java:8817)
03-03 10:57:01.948: ERROR/AndroidRuntime(8303):     at android.os.Handler.handleCallback(Handler.java:587)
03-03 10:57:01.948: ERROR/AndroidRuntime(8303):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-03 10:57:01.948: ERROR/AndroidRuntime(8303):     at android.os.Looper.loop(Looper.java:143)
03-03 10:57:01.948: ERROR/AndroidRuntime(8303):     at android.app.ActivityThread.main(ActivityThread.java:4914)
03-03 10:57:01.948: ERROR/AndroidRuntime(8303):     at java.lang.reflect.Method.invokeNative(Native Method)
03-03 10:57:01.948: ERROR/AndroidRuntime(8303):     at java.lang.reflect.Method.invoke(Method.java:521)
03-03 10:57:01.948: ERROR/AndroidRuntime(8303):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
03-03 10:57:01.948: ERROR/AndroidRuntime(8303):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
03-03 10:57:01.948: ERROR/AndroidRuntime(8303):     at dalvik.system.NativeStart.main(Native Method)
03-03 10:57:01.948: ERROR/AndroidRuntime(8303): Caused by: java.lang.NullPointerException
03-03 10:57:01.948: ERROR/AndroidRuntime(8303):     at android.graphics.Bitmap.createBitmap(Bitmap.java:450)

0 个答案:

没有答案