谷歌地图(谷歌地图Android v2)活动在切换USB模式时崩溃
我陷入了一个奇怪的境地,这是偶然发生的。我目前正在使用Android v2的谷歌地图API来显示地图片段。除了用户在USB模式之间切换时应用程序崩溃外,其他一切都正常运行 问题是,我无法保存某些对象,因为在这种情况下我无法覆盖onSaveInstanceState()。这是谷歌地图API中的一个应用程序。我收到空指针异常。完整的logcat如下所示:谷歌地图(谷歌地图Android v2)活动在切换USB模式时崩溃,android,usb,google-maps-android-api-2,Android,Usb,Google Maps Android Api 2,我陷入了一个奇怪的境地,这是偶然发生的。我目前正在使用Android v2的谷歌地图API来显示地图片段。除了用户在USB模式之间切换时应用程序崩溃外,其他一切都正常运行 问题是,我无法保存某些对象,因为在这种情况下我无法覆盖onSaveInstanceState()。这是谷歌地图API中的一个应用程序。我收到空指针异常。完整的logcat如下所示: 04-27 18:45:10.289: E/AndroidRuntime(19982): FATAL EXCEPTION: main 04-27
04-27 18:45:10.289: E/AndroidRuntime(19982): FATAL EXCEPTION: main
04-27 18:45:10.289: E/AndroidRuntime(19982): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.octanetech.cortes/com.octanetech.cortes.MapActivity}: java.lang.NullPointerException
04-27 18:45:10.289: E/AndroidRuntime(19982): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-27 18:45:10.289: E/AndroidRuntime(19982): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-27 18:45:10.289: E/AndroidRuntime(19982): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-27 18:45:10.289: E/AndroidRuntime(19982): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-27 18:45:10.289: E/AndroidRuntime(19982): at android.os.Handler.dispatchMessage(Handler.java:99)
04-27 18:45:10.289: E/AndroidRuntime(19982): at android.os.Looper.loop(Looper.java:123)
04-27 18:45:10.289: E/AndroidRuntime(19982): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-27 18:45:10.289: E/AndroidRuntime(19982): at java.lang.reflect.Method.invokeNative(Native Method)
04-27 18:45:10.289: E/AndroidRuntime(19982): at java.lang.reflect.Method.invoke(Method.java:521)
04-27 18:45:10.289: E/AndroidRuntime(19982): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:871)
04-27 18:45:10.289: E/AndroidRuntime(19982): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)
04-27 18:45:10.289: E/AndroidRuntime(19982): at dalvik.system.NativeStart.main(Native Method)
04-27 18:45:10.289: E/AndroidRuntime(19982): Caused by: java.lang.NullPointerException
04-27 18:45:10.289: E/AndroidRuntime(19982): at com.octanetech.cortes.MapActivity.displayPlaces(MapActivity.java:644)
04-27 18:45:10.289: E/AndroidRuntime(19982): at com.octanetech.cortes.MapActivity.onCreate(MapActivity.java:158)
04-27 18:45:10.289: E/AndroidRuntime(19982): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-27 18:45:10.289: E/AndroidRuntime(19982): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
04-27 18:45:10.289: E/AndroidRuntime(19982): ... 11 more
04-27 18:45:10.309: W/ActivityManager(2466): Force finishing activity com.octanetech.cortes/.MapActivity
只有当我恢复映射活动时,应用程序才会崩溃,否则应用程序的其余部分工作正常。我们将非常感谢您的帮助
编辑添加活动类:
public class MapActivity extends SherlockFragmentActivity implements LocationListener, OnInfoWindowClickListener, SearchView.OnQueryTextListener,
OnMarkerClickListener {
// private final BitmapDescriptor markerBitmap = null;
private GoogleMap mMap;
private CameraPosition cameraPosition;
Location currentLocation;
MapView mapView;
Bitmap beaconImage;
Geocoder geocoder;
private Handler mHandler;
protected Marker marker;
ActionBar actionBar;
private Menu menu;
private boolean menuCreated;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
actionBar = getSupportActionBar();
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
navigate = new NavigateActivity(this);
context = this;
currentLocation = new Location("asd");
currentLocation.setLatitude(0);
currentLocation.setLongitude(0);
setContentView(R.layout.activity_map);
mMap = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
SupportMapFragment f = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map));
f.setRetainInstance(true);
mMap.setMyLocationEnabled(true);
mMap.setOnInfoWindowClickListener(this);
mMap.setOnMarkerClickListener(this);
cameraPosition = mMap.getCameraPosition();
geocoder = new Geocoder(this, Locale.getDefault());
displayPlaces();
}
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
this.menu = menu;
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.mapview_menu, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
mSearchView = (SearchView) searchItem.getActionView();
setupSearchView(searchItem);
menuCreated = true;
return true;
}
private void setupSearchView(MenuItem searchItem) {
if (isAlwaysExpanded()) {
mSearchView.setIconifiedByDefault(false);
} else {
searchItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_IF_ROOM
| MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
}
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
if (searchManager != null) {
}
mSearchView.setOnQueryTextListener(this);
}
public boolean onQueryTextChange(String newText) {
// mStatusView.setText("Query = " + newText);
return false;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.list:
// app icon in action bar clicked; go home
Intent intent = new Intent(this, SearchActivity.class);
intent.removeExtra("search");
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
return true;
case R.id.bookmark:
intent = new Intent(adasd.this, BookmarkActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public boolean onQueryTextSubmit(String query) {
// Extras.createActualDialogueBox(context, "error", url.length()+"");
// getPlaces(url);
Intent intent = new Intent(this, LoadingActivityForPlaces.class);
mBeaconOptionsPoppedUp = false;
intent.putExtra("key", query);
startActivity(intent);
Extras.searched = true;
return false;
}
public boolean onClose() {
return false;
}
protected boolean isAlwaysExpanded() {
return false;
}
@Override
public void onBackPressed() {
// do nothing.
if (mBeaconOptionsPoppedUp) {
mBeaconOptionsPoppedUp = false;
}
}
@Override
public void onResume() {
super.onResume();
displayPlaces();
}
@Override
public void onPause() {
super.onPause();
Extras.activityPaused();
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
}
@Override
public void onSaveInstanceState(Bundle bundle) {
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
}
private void displayPlaces() {
if (!Extras.trialMode) {
// TODO Auto-generated method stub
if (placesMarkers != null) {
removeMarkers(placesMarkers);
}
placesMarkers = new ArrayList<Marker>();
for (GooglePlace i : Extras.googlePlaces) {
Marker m = mMap.addMarker(new MarkerOptions()
.position(i.getPosition()).title(i.getTitle())
.snippet(i.getDescription()));
if (i.equals(Extras.currentGooglePlace)) {
m.showInfoWindow();
}
placesMarkers.add(m);
}
for (GooglePlace i : Extras.cortesPlaces) {
Marker m = mMap.addMarker(new MarkerOptions()
.position(i.getPosition()).title(i.getTitle())
.snippet(i.getDescription()));
if (i.equals(Extras.currentGooglePlace)) {
m.showInfoWindow();
}
placesMarkers.add(m);
}
}
}
公共类MapActivity扩展SherlockFragmentActivity实现LocationListener、OnInfo窗口ClickListener、SearchView.OnQueryTextListener、,
市场上的监听器{
//私有最终位图描述符markerBitmap=null;
私有谷歌地图;
私人摄像位置摄像位置;
位置当前位置;
地图视图;
位图信标图像;
地理编码器;
私人经理人;
保护标记;
ActionBar ActionBar;
私人菜单;
创建私有布尔菜单;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
actionBar=getSupportActionBar();
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
导航=新的导航活动(此);
上下文=这个;
当前位置=新位置(“asd”);
currentLocation.setLatitude(0);
currentLocation.setLongitude(0);
setContentView(R.layout.activity_map);
mMap=((SupportMapFragment)getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
SupportMapFragment f=((SupportMapFragment)getSupportFragmentManager()
.findFragmentById(R.id.map));
f、 setRetainInstance(真);
mMap.setMyLocationEnabled(真);
mMap.setOnInfoWindowClickListener(此);
mMap.setOnMarkerClickListener(此);
cameraPosition=mMap.getCameraPosition();
geocoder=新的geocoder(这个,Locale.getDefault());
显示位置();
}
公共布尔onCreateOptions菜单(菜单){
super.onCreateOptions菜单(菜单);
this.menu=菜单;
MenuInflater充气器=getSupportMenuInflater();
充气器。充气(R.menu.mapview_菜单,菜单);
MenuItem searchItem=menu.findItem(R.id.action\u search);
mSearchView=(SearchView)searchItem.getActionView();
setupSearchView(searchItem);
menuCreated=true;
返回true;
}
私有void setupSearchView(MenuItem searchItem){
如果(isAlwaysExpanded()){
mSearchView.setIconifiedByDefault(false);
}否则{
searchItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_如果房间
|菜单项。显示为(操作、折叠、操作、视图);
}
SearchManager SearchManager=(SearchManager)getSystemService(Context.SEARCH\u服务);
if(searchManager!=null){
}
mSearchView.setOnQueryTextListener(此);
}
公共布尔onQueryTextChange(字符串newText){
//mStatusView.setText(“Query=“+newText”);
返回false;
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
开关(item.getItemId()){
案例R.id.列表:
//已单击操作栏中的应用图标;返回主页
意向意向=新意向(此为SearchActivity.class);
意图。removeExtra(“搜索”);
intent.addFlags(intent.FLAG\u ACTIVITY\u REORDER\u TO\u FRONT);
星触觉(意向);
返回true;
案例R.id.书签:
intent=新的intent(adasd.this、BookmarkActivity.class);
intent.setFlags(intent.FLAG\u ACTIVITY\u REORDER\u TO\u FRONT);
星触觉(意向);
返回true;
违约:
返回super.onOptionsItemSelected(项目);
}
}
公共布尔值onQueryTextSubmit(字符串查询){
//createActualDialogueBox(上下文,“error”,url.length()+);
//getPlaces(url);
意向意向=新意向(此,LoadingActivityForPlaces.class);
mBeaconOptionsPoppedUp=false;
intent.putExtra(“键”,查询);
星触觉(意向);
Extras.seared=true;
返回false;
}
公共布尔onClose(){
返回false;
}
受保护的布尔值isAlwaysExpanded(){
返回false;
}
@凌驾
public void onBackPressed(){
//什么也不做。
如果(mBeaconOptionsPoppedUp){
mBeaconOptionsPoppedUp=false;
}
}
@凌驾
恢复时公开作废(){
super.onResume();
显示位置();
}
@凌驾
公共无效暂停(){
super.onPause();
额外活动暂停();
}
@凌驾
受保护的void onNewIntent(意图){
super.onNewIntent(意向);
}
@凌驾
SaveInstanceState上的公共无效(捆绑){
}
@凌驾
RestoreInstanceState上的受保护无效(Bundle savedInstanceState){
}
专用void displayPlaces(){
如果(!Extras.trialMode){
//TODO自动生成的方法存根
if(placesMarkers!=null){
移除标记(放置标记);
}
placesMarkers=newarraylist();
用于(Google Place i:额外服务,Google Places){
Marker m=mMap.addMarker(新MarkerOptions()
.position(i.getPosition()).title(i.getTitle())
.snippet(i.getDescription());
如果(i.equals(Extras.currentGooglePlace)){
m、 showInfoWindow();
}
地点标记。添加(m);
}
用于(Google Place i:额外服务cortesPlaces){
Marker m=mMap.addMarker(新MarkerOptions()
.position(i.getPosition()).title(i.getTitle())
.snippet(i.getDescription());
如果(i.equals(Extras.currentGooglePlace)){
m、 showInfoWindow();
}
地点标记。添加(m);
}
}
}
}
这里Extras是一个带有静态对象的类。美联社