지도위에 그림을 그리고 이미지로 저장을 하려고 하는데요
밑에서 save메뉴를 클릭했을때 capture()가 호출되고
최상위의 view가 save변수에 저장이 되는 방식인데 오류가 나네요
소스코드 자체에는 오류가 없구요 save메뉴를 클릭했을 때 오류가 납니다.
package com.Messenger;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import com.Messenger.Gallery1;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;
public class Map_edit extends MapActivity implements OnClickListener {
private MapController mapController;
private MyPositionOverlay2 positionOverlay;
private MapView myMapView;
private FrameLayout frame;
private View view;
private static final int sMenuExampleResources[] = { R.menu.submenu_edit };
private static final String sMenuExampleNames[] = { "Submenu2" };
private Spinner mSpinner;
private TextView mInstructionsText;
private Menu mMenu;
private Bitmap save;
private FileOutputStream foutput;
private int cnt = 0;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.map_edit);
frame = (FrameLayout) findViewById(R.id.frame);
myMapView = (MapView) findViewById(R.id.mapview);
positionOverlay = new MyPositionOverlay2();
mapController = myMapView.getController();
// Configure the map display options
myMapView.setSatellite(false);
myMapView.setStreetView(true);
// Zoom in
mapController.setZoom(20);
initZoomControls();
initMyLocation();
// Add the MyPositionOverlay
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
String provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
updateWithNewLocation(location);
locationManager.requestLocationUpdates(provider, 2000, 10,
locationListener);
View drawButton = findViewById(R.id.draw_button);
drawButton.setOnClickListener(this);
View endButton = findViewById(R.id.end_button);
endButton.setOnClickListener(this);
// Create a simple layout
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
// Create the spinner to allow the user to choose a menu XML
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, sMenuExampleNames);
adapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinner = new Spinner(this);
mSpinner.setAdapter(adapter);
}
public void capture() {
this.view.getRootView().setDrawingCacheEnabled(true);
save = this.view.getRootView().getDrawingCache();
}
public void onClick(View v) {
if (v.getId() == R.id.draw_button) {
List<Overlay> overlays = myMapView.getOverlays();
overlays.add(positionOverlay);
}
else if (v.getId() == R.id.end_button) {
positionOverlay.path_idx = 0;
List<Overlay> overlays = myMapView.getOverlays();
overlays.remove(positionOverlay);
}
}
private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
updateWithNewLocation(location);
}
public void onProviderDisabled(String provider) {
updateWithNewLocation(null);
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
};
/** Update the map with a new location */
private void updateWithNewLocation(Location location) {
String latLongString;
String addressString = "No address found";
if (location != null) {
// Update my location marker
positionOverlay.setLocation(location);
// Update the map location.
Double geoLat = location.getLatitude() * 1E6;
Double geoLng = location.getLongitude() * 1E6;
GeoPoint point = new GeoPoint(geoLat.intValue(), geoLng.intValue());
mapController.animateTo(point);
double lat = location.getLatitude();
double lng = location.getLongitude();
latLongString = "Lat:" + lat + "\nLong:" + lng;
Geocoder gc = new Geocoder(this, Locale.getDefault());
try {
List<Address> addresses = gc.getFromLocation(lat, lng, 1);
StringBuilder sb = new StringBuilder();
if (addresses.size() > 0) {
Address address = addresses.get(0);
for (int i = 0; i < address.getMaxAddressLineIndex(); i++)
sb.append(address.getAddressLine(i)).append("\n");
sb.append(address.getLocality()).append("\n");
sb.append(address.getPostalCode()).append("\n");
sb.append(address.getCountryName());
}
addressString = sb.toString();
} catch (IOException e) {
}
} else {
latLongString = "No location found";
}
}
/** Get the zoom controls and add them to the bottom of the map. */
private void initZoomControls() {
View zoomControls = myMapView.getZoomControls();
FrameLayout.LayoutParams p = new FrameLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
Gravity.BOTTOM + Gravity.CENTER_HORIZONTAL);
frame.addView(zoomControls, p);
}
/** Start tracking the position on the map. */
private void initMyLocation() {
final MyLocationOverlay overlay = new MyLocationOverlay(this, myMapView);
overlay.enableMyLocation();
// overlay.enableCompass(); // no effect in emulator
overlay.runOnFirstFix(new Runnable() {
public void run() {
// Zoom in to current location
mapController.setZoom(16);
mapController.animateTo(overlay.getMyLocation());
}
});
myMapView.getOverlays().add(overlay);
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Hold on to this
mMenu = menu;
// Inflate the currently selected menu XML resource.
MenuInflater inflater = getMenuInflater();
inflater.inflate(sMenuExampleResources[mSpinner
.getSelectedItemPosition()], menu);
// Disable the spinner since we've already created the menu and the user
// can no longer pick a different menu XML.
mSpinner.setEnabled(false);
// Change instructions
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.map1:
startActivity(new Intent(this, Map1_main.class));
return true;
case R.id.map2:
startActivity(new Intent(this, Map2_main.class));
return true;
case R.id.chat:
startActivity(new Intent(this, Messenger.class));
return true;
case R.id.search:
startActivity(new Intent(this, Search.class));
return true;
case R.id.save:
capture();
try {
foutput = this.openFileOutput(Integer.toString(cnt), 1);
save.compress(Bitmap.CompressFormat.JPEG, 100, foutput);
cnt++;
foutput.flush();
foutput.close();
} catch (IOException e) {
e.printStackTrace();
}
return true;
case R.id.gallery:
Intent gall = new Intent(this, Gallery1.class);
gall.putExtra("save", save);
gall.putExtra("cnt", cnt);
startActivity(gall);
return true;
case R.id.all:
positionOverlay.path_idx = 0;
List<Overlay> overlays = myMapView.getOverlays();
overlays.add(positionOverlay);
return true;
case R.id.prev:
positionOverlay.path_idx = positionOverlay.path_idx-1;
List<Overlay> overlays2 = myMapView.getOverlays();
overlays2.add(positionOverlay);
return true;
default:
// Don't toast text when a submenu2 is clicked
if (!item.hasSubMenu()) {
Toast.makeText(this, item.getTitle(), Toast.LENGTH_SHORT)
.show();
return true;
}
break;
}
return false;
}
}
로그캣의 오류메시지 입니다.
11-01 02:12:09.674: ERROR/AndroidRuntime(4528): Uncaught handler: thread main exiting due to uncaught exception
11-01 02:12:09.694: ERROR/AndroidRuntime(4528): java.lang.NullPointerException
11-01 02:12:09.694: ERROR/AndroidRuntime(4528): at com.Messenger.Map_edit.capture(Map_edit.java:121)
11-01 02:12:09.694: ERROR/AndroidRuntime(4528): at com.Messenger.Map_edit.onOptionsItemSelected(Map_edit.java:266)
11-01 02:12:09.694: ERROR/AndroidRuntime(4528): at android.app.Activity.onMenuItemSelected(Activity.java:2085)
11-01 02:12:09.694: ERROR/AndroidRuntime(4528): at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:820)
11-01 02:12:09.694: ERROR/AndroidRuntime(4528): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:139)
11-01 02:12:09.694: ERROR/AndroidRuntime(4528): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:813)
11-01 02:12:09.694: ERROR/AndroidRuntime(4528): at com.android.internal.view.menu.IconMenuView.invokeItem(IconMenuView.java:519)
11-01 02:12:09.694: ERROR/AndroidRuntime(4528): at com.android.internal.view.menu.IconMenuItemView.performClick(IconMenuItemView.java:122)
11-01 02:12:09.694: ERROR/AndroidRuntime(4528): at android.view.View.onTouchEvent(View.java:3828)
11-01 02:12:09.694: ERROR/AndroidRuntime(4528): at android.widget.TextView.onTouchEvent(TextView.java:6291)
11-01 02:12:09.694: ERROR/AndroidRuntime(4528): at android.view.View.dispatchTouchEvent(View.java:3368)
11-01 02:12:09.694: ERROR/AndroidRuntime(4528): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
11-01 02:12:09.694: ERROR/AndroidRuntime(4528): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
11-01 02:12:09.694: ERROR/AndroidRuntime(4528): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1691)
11-01 02:12:09.694: ERROR/AndroidRuntime(4528): at android.view.ViewRoot.handleMessage(ViewRoot.java:1525)
11-01 02:12:09.694: ERROR/AndroidRuntime(4528): at android.os.Handler.dispatchMessage(Handler.java:99)
11-01 02:12:09.694: ERROR/AndroidRuntime(4528): at android.os.Looper.loop(Looper.java:123)
11-01 02:12:09.694: ERROR/AndroidRuntime(4528): at android.app.ActivityThread.main(ActivityThread.java:3948)
11-01 02:12:09.694: ERROR/AndroidRuntime(4528): at java.lang.reflect.Method.invokeNative(Native Method)
11-01 02:12:09.694: ERROR/AndroidRuntime(4528): at java.lang.reflect.Method.invoke(Method.java:521)
11-01 02:12:09.694: ERROR/AndroidRuntime(4528): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
11-01 02:12:09.694: ERROR/AndroidRuntime(4528): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
11-01 02:12:09.694: ERROR/AndroidRuntime(4528): at dalvik.system.NativeStart.main(Native Method)