我正在以编程方式将SupportMapFragment添加到我的主要活动中。 我可以添加它,但我需要一个编辑文本和一个按钮也显示在地图的顶部。 我为我的地图片段创建了一个单独的布局,我在主要活动中以编程方式添加。但我得到一份NPE说“无法恢复活动”
请帮帮我。 这是我的代码。 MainActivity.java
public class MainActivity extends FragmentActivity{
protected LocationManager locationManager;
protected LocationListener locationListener;
protected Context context;
GoogleMap googleMap;
String lat;
String provider;
protected double latitude,longitude;
protected boolean gps_enabled,network_enabled;
private ImageView mImage;
List<Marker> markerslist = new ArrayList<Marker>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get an instance of FragmentTransaction from your Activity
final FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
//add a fragment
MapFragmentActivity mapFragment = new MapFragmentActivity();
fragmentTransaction.replace(R.id.appfragment, mapFragment);
fragmentTransaction.commit();
mImage = (ImageView)findViewById(R.id.eemaView);
mImage.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
System.out.println("inside on click eema");
EnterpriseFragment entFragment = new EnterpriseFragment();
FragmentTransaction fragmentTrans = fragmentManager.beginTransaction();
fragmentTrans.replace(R.id.appfragment, entFragment);
fragmentTrans.commit();
}
});
mImage = (ImageView)findViewById(R.id.deviationView);
mImage.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
System.out.println("inside on click deviation");
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
//add a fragment
MapFragmentActivity mapFragment = new MapFragmentActivity();
fragmentTransaction.replace(R.id.appfragment, mapFragment);
fragmentTransaction.commit();
}
});
}
这是我的片段类
public class MapFragmentActivity extends SupportMapFragment implements LocationListener{
protected LocationManager locationManager;
protected LocationListener locationListener;
protected Context context;
GoogleMap googleMap;
String lat;
String provider;
protected double latitude,longitude;
protected boolean gps_enabled,network_enabled;
List<Marker> markerslist = new ArrayList<Marker>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.mapfragmentlayout, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
googleMap = this.getMap();
if (googleMap != null) {
//Your initialization code goes here
googleMap.setMyLocationEnabled(true);
LocationManager locationManager = (LocationManager) getActivity().getSystemService(getActivity().LOCATION_SERVICE);
Criteria criteria = new Criteria();
String bestProvider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(bestProvider);
if (location != null) {
onLocationChanged(location);
}
locationManager.requestLocationUpdates(bestProvider, 20000, 0, this);
// Getting reference to btn_find of the layout activity_main
//Button btn_find = (Button) getActivity().findViewById(R.id.btn_find);
// Defining button click event listener for the find button
View.OnClickListener findClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
// Getting reference to EditText to get the user input location
EditText etLocation = (EditText) getActivity().findViewById(R.id.et_location);
// Getting user input location
String location = etLocation.getText().toString();
if(location!=null && !location.equals("")){
for(Marker m : markerslist) {
System.out.println("Marker size is "+markerslist.size());
System.out.println("Title is "+m.getSnippet());
System.out.println("Position is "+((LatLng)(m.getPosition())).latitude);
if(location.equals(m.getSnippet())) {
// do something with the marker
LatLng latLng = m.getPosition();
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
break; // stop the loop
}
}
}
}
};
// Setting button click event listener for the find button
//btn_find.setOnClickListener(findClickListener);
googleMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
@Override
public View getInfoWindow(Marker marker) {
return null;
}
@Override
public View getInfoContents(Marker marker) {
// Getting view from the layout file info_window_layout
View v = getActivity().getLayoutInflater().inflate(R.layout.site_info_window, null);
List<String> qfList = JSONReader.readQuickFixData(getActivity());
ListView devLst = (ListView)v.findViewById(R.id.deviationList);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),R.layout.simple_list_item_deviation,android.R.id.text1,qfList);
devLst.setAdapter(adapter);
return v;
}
});
}
}
这是我的片段布局xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/btn_find"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Find"
android:layout_alignParentRight="true" />
<EditText
android:id="@+id/et_location"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:hint="Enter Site ID"
android:layout_toLeftOf="@id/btn_find" />
</RelativeLayout>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"/>
</LinearLayout>
我的主要布局xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="WEM App"
android:id="@+id/textView"
android:textStyle="bold"
android:textSize="16sp"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:layout_margin="3dp"
android:textColor="@color/abc_input_method_navigation_guard"
android:background="@android:color/holo_green_light"/>
<LinearLayout
android:id="@+id/appfragment"
android:layout_width="match_parent"
android:layout_weight="4"
android:layout_height="0dip"
android:orientation="horizontal">
</LinearLayout>
<HorizontalScrollView
android:id="@+id/hsview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginBottom="3dp"
android:background="@android:color/holo_green_light">
<LinearLayout android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/deviationView"
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@drawable/sema_icon" />
<ImageView
android:id="@+id/eemaView"
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@drawable/eema_icon" />
<ImageView
android:id="@+id/siteView"
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@drawable/sema_icon" />
<ImageView
android:id="@+id/alertsView"
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@drawable/eema_icon" />
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
答案 0 :(得分:1)
import android.support.v4.app.Fragment;
和import android.support.v4.app.FragmentTransaction;
进行如下更改 -
更改public class MapFragmentActivity extends SupportMapFragment
到public class MapFragmentActivity extends Fragment
保持原状。