问题解决了!
ISSUE: 我有问题,我找不到任何解决方案。我有Maps_Activity,它包含两个片段。两个片段都显示谷歌地图v2,由于有两个ActionBar标签,我在屏幕上显示它们。
当我想返回上一个活动时,问题就出现了。当我点击Android后退按钮时,我的整个应用程序关闭,显示pupup,我的应用程序已停止。
我应该在代码中添加什么来删除此错误?
解决方案:
Maps_Activity.java
import android.app.ActionBar;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import com.google.android.gms.maps.MapFragment;
public class Maps_Activity extends Activity {
public static FragmentManager manager;
Fragment fragmentTab1 = new Medical_Shops_Map();
Fragment fragmentTab2 = new Medical_Clicnics_Map();
Button Medical_Shops_Button, Medical_Clinics_Button;
ActionBar actionBar ;
@Override
public void onCreate(Bundle arg0) {
super.onCreate(arg0);
setContentView(R.layout.activity_maps_);
manager = getFragmentManager();
actionBar=getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab tab1=actionBar.newTab();
tab1.setText("Apteki 24H/7");
tab1.setTabListener(new MyTabsListener(fragmentTab1));
ActionBar.Tab tab2=actionBar.newTab();
tab2.setText("Przychodnie Lekarskie");
tab2.setTabListener(new MyTabsListener(fragmentTab2));
actionBar.addTab(tab1);
actionBar.addTab(tab2);
}
protected class MyTabsListener implements ActionBar.TabListener {
private Fragment fragment;
public MyTabsListener(Fragment fragment) {
this.fragment = fragment;
}
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
ft = manager.beginTransaction();
ft.replace(R.id.map_container, fragment);
ft.commit();
Log.e("Activity", "Fragment replace");
}
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
// some people needed this line as well to make it work:
MapFragment mMap2 = ((MapFragment) Maps_Activity.manager.findFragmentById(R.id.map));
ft = manager.beginTransaction();
if(mMap2!=null)
{
ft.remove(mMap2);
ft.commit();
Log.e("Activity", "Mapa_fragment destroy");
}
else
{
MapFragment mMap1 = ((MapFragment) Maps_Activity.manager.findFragmentById(R.id.map1));
ft.remove(mMap1);
ft.commit();
Log.e("Activity", "Mapa_fragment2 destroy");
}
}
}
}
其中一个片段:
import android.os.Bundle;
import android.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMapOptions;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class Medical_Clicnics_Map extends Fragment implements OnMapReadyCallback {
private static View view;
private static GoogleMap mMap2;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.medical__clicnics__map, container, false);
setUpMapIfNeeded();
return view;
}
public void setUpMapIfNeeded() {
MapFragment mMap2 = ((MapFragment) Maps_Activity.manager
.findFragmentById(R.id.map1));
mMap2.getMapAsync(Medical_Clicnics_Map.this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
Log.d("Activity", "Map ready");
googleMap.setMyLocationEnabled(true);
LatLng sydney = new LatLng(-33.867, 151.206);
googleMap.addMarker(new MarkerOptions()
.title("Sydney")
.snippet("The most populous city in Australia.")
.position(sydney));
}
}
也许对某人来说这段代码会有所帮助;)