当我第二次尝试充气时,当我使用我的标签时,应用程序崩溃,在一个小设备上它正常工作。我试图在谷歌地图片段上膨胀。片段在里面。我试图在我复发之前杀死我的地图片段,但它没有工作当我尝试从onList_ItemClickListener转到地图片段时,应用程序崩溃
我的竞争者xml
<FrameLayout
android:id="@+id/tablet_left"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="40"
android:background="#eeeeee" >
</FrameLayout>
<FrameLayout
android:id="@+id/tablet_rigth"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="60" />
</LinearLayout>
我的主要课程:
public class MainActivity extends FragmentActivity
implements Search_Categories.onCategoryClickListener,
First_Page.onSearchTypeListener,
Search_Fragment.OnListResultClickListener,
Search_Fragment.onGetAllPlacesClickListener
{
boolean isTablet;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.container);
isTablet = isTablet(getApplicationContext());
if (findViewById(R.id.fragment_container) != null){
if (savedInstanceState != null) {
return;
}
First_Page first = new First_Page();
first.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, first).commit();
}else{
First_Page first = new First_Page();
first.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction().add(R.id.tablet_left, first).commit();
Map_fragment map = new Map_fragment();
map.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction().add(R.id.tablet_rigth, map).commit();
}
}
@Override
public void onList_ItemClickListener(String lat, String lng, String placeName,String placeRating, String placeAdress) {
isTablet = isTablet(getApplicationContext());
if (isTablet==true){
Map_fragment map_frag2 = new Map_fragment();
Bundle bundel = new Bundle();
bundel.putString("lat", lat);
bundel.putString("lng", lng);
bundel.putString("name", placeName);
bundel.putString("rating", placeRating);
bundel.putString("adress", placeAdress);
map_frag2.setArguments(bundel);
FragmentTransaction transaction1 = getSupportFragmentManager().beginTransaction();
transaction1.replace(R.id.tablet_rigth, map_frag2);
transaction1.addToBackStack(null);
transaction1.commit();
}else{
// phone
//need to change view bundel
Map_fragment map_frag = new Map_fragment();
//Search_Fragment search = new Search_Fragment();
Bundle bundel = new Bundle();
bundel.putString("lat", lat);
bundel.putString("lng", lng);
bundel.putString("name", placeName);
bundel.putString("rating", placeRating);
bundel.putString("adress", placeAdress);
map_frag.setArguments(bundel);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, map_frag);
transaction.addToBackStack(null);
transaction.commit();
}
public boolean isTablet(Context context) {
boolean xlarge = ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == 4);
boolean large = ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE);
return (xlarge || large);
}
}
我的地图片段
public class Map_fragment extends Fragment{
private GoogleMap googleMap;
DBHandler placesDB;
FavoritDBHandler favoritsDB;
GPSTracker gps;
double myLat;
double myLng;
String placeRating;
String placeAdress="no adress in the system";
String placename="No name";
double latitude = 32.0833;
double longitude = 34.8000;
String lat;
String lng;
boolean isTablet=false;
boolean flag=true;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
favoritsDB = new FavoritDBHandler(getActivity());
return inflater.inflate(R.layout.map, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
boolean get_All_Places_Flag = false;
try {
// Loading map
initilizeMap();
Bundle bundel = getArguments();
lat = bundel.getString("lat");
lng= bundel.getString("lng");
placename = bundel.getString("name");
placeAdress= bundel.getString("adress");
placeRating= bundel.getString("rating");
get_All_Places_Flag=bundel.getBoolean("flag");
latitude = Double.valueOf(lat);
longitude = Double.valueOf(lng);
gps= new GPSTracker(getActivity());
myLat = gps.getLatitude();
myLng = gps.getLongitude();
} catch (Exception e) {
e.printStackTrace();
}
ImageButton navigate = (ImageButton)view.findViewById(R.id.navigate);
navigate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LatLng start=new LatLng(myLat, myLng);
LatLng end=new LatLng(latitude, longitude);
Navigator navy = new Navigator(googleMap,start,end);
navy.findDirections(true);
}
});
boolean isTablet=false;
gps= new GPSTracker(getActivity());
myLat = gps.latitude;
myLng = gps.longitude;
isTablet= isTablet(getActivity());
if (isTablet==true){
if(flag==true){
CameraPosition cameraPosition = new CameraPosition.Builder().target(
new LatLng(myLat, myLng)).zoom(13).build();
setGoogleMap(cameraPosition);
flag=false;
googleMap.addMarker(new MarkerOptions().position(new LatLng(myLat,myLng)).title("You Are Here")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
Toast.makeText(getActivity(), "may lat" +myLat, Toast.LENGTH_SHORT).show();
}else{
// ADD marker to map
googleMap.addMarker(new MarkerOptions().position(new LatLng(latitude,longitude)).title(placename));
googleMap.addMarker(new MarkerOptions().position(new LatLng(myLat,myLng)).title("You Are Here")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
}
}else{
CameraPosition cameraPosition = new CameraPosition.Builder().target(
new LatLng(latitude, longitude)).zoom(15).build();
setGoogleMap(cameraPosition);
// ADD marker to map
googleMap.addMarker(new MarkerOptions().position(new LatLng(latitude,longitude)).title(placename));
googleMap.addMarker(new MarkerOptions().position(new LatLng(myLat,myLng)).title("You Are Here")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
}
}
private void setGoogleMap(CameraPosition cameraPosition) {
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
googleMap.getUiSettings().setCompassEnabled(true);
googleMap.setMyLocationEnabled(true); // false to disable
googleMap.getUiSettings().setRotateGesturesEnabled(true);
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
}
private void initilizeMap() {
if (googleMap == null) {
googleMap = ((SupportMapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();
// check if map is created successfully or not
if (googleMap == null) {
Toast.makeText(getActivity(), "Sorry! unable to create maps", Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onPause() {
super.onPause();
SupportMapFragment map1 = (SupportMapFragment)getFragmentManager().findFragmentById(R.id.map);
if(map1 != null)
getFragmentManager().beginTransaction().remove(map1).commitAllowingStateLoss();
}
@Override
public void onDestroy() {
super.onDestroy();
SupportMapFragment map = (SupportMapFragment)getFragmentManager().findFragmentById(R.id.map);
if (map != null){
getFragmentManager().beginTransaction().remove(map).commit();
}
}
public boolean isTablet(Context context) {
boolean xlarge = ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == 4);
boolean large = ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE);
return (xlarge || large);
}
}
代码更改 @覆盖 public void onDestroyView(){ super.onDestroyView();
SupportMapFragment map = (SupportMapFragment)getFragmentManager().findFragmentByTag("google_map");
if (map != null){
getFragmentManager().beginTransaction().remove(map).commitAllowingStateLoss();
}
/*Map_fragment map = (Map_fragment)getFragmentManager().findFragmentByTag("google_map");
if (map != null){
getFragmentManager().beginTransaction().remove(map).commitAllowingStateLoss();
}*/
}
我的MAP CLASS XML:
<fragment
android:tag="google_map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"
android:id="@+id/map"
>
</fragment>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#99000000"
android:orientation="vertical" >
<ImageButton
android:id="@+id/navigate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/location_50" />
<ImageButton
android:id="@+id/add_to_favorits"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/location_star_50" />
<ImageButton
android:id="@+id/share"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/location_speech_bubble_50" />
<ImageButton
android:id="@+id/place_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/location_info_50" />
</LinearLayout>
</FrameLayout>