我目前正在开发一个跟踪某个设备的Android应用程序,其中一个按钮(即button2)负责启动MapsActivity。我将MapsActivity复制到另一个应用程序中,在该应用程序中它是唯一的活动,并且它工作正常,但是当我尝试使用按钮(button2)访问它时,它在其他应用程序中崩溃。
MainActivity java代码片段(实现了所有需要的导入):
package com.example.finalprototype;
public class MainActivity extends ActionBarActivity {
private ProgressDialog progressDialog;
Button button1;
Button button3;
Button button2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
new LoadTracker().execute();
}
});
button2 = (Button)findViewById(R.id.button2);
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, MapsActivity.class));
finish();
}
});
button3 = (Button)findViewById(R.id.button3);
button3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, MainActivity2Activity.class));
finish();
}
});
}
MapsActivity Code(实现了所有需要的导入):
package com.example.finalprototype;
public class MapsActivity extends ActionBarActivity
implements OnMapClickListener, OnMapLongClickListener, OnMarkerClickListener{
final int RQS_GooglePlayServices = 1;
private GoogleMap myMap;
static final int POLYGON_POINTS=4;
ArrayList<LatLng> markers = new ArrayList<>();
Location myLocation;
TextView tvLocInfo;
boolean markerClicked;
PolygonOptions polygonOptions;
Polygon polygon;
Button setgeo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
setgeo =(Button)findViewById(R.id.setgeo);
setgeo.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
String Point1 = markers.get(0).toString();
String Point2 = markers.get(2).toString();
}
});
tvLocInfo = (TextView)findViewById(R.id.locinfo);
FragmentManager myFragmentManager = getFragmentManager();
MapFragment myMapFragment = (MapFragment)myFragmentManager.findFragmentById(R.id.map);
myMap = myMapFragment.getMap();
myMap.setMyLocationEnabled(true);
myMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
myMap.setOnMapClickListener(this);
myMap.setOnMapLongClickListener(this);
myMap.setOnMarkerClickListener(this);
markerClicked = false;
}
@Override
protected void onResume() {
super.onResume();
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
if (resultCode == ConnectionResult.SUCCESS){
Toast.makeText(getApplicationContext(),
"isGooglePlayServicesAvailable SUCCESS",
Toast.LENGTH_LONG).show();
}else{
GooglePlayServicesUtil.getErrorDialog(resultCode, this, RQS_GooglePlayServices);
}
}
@Override
public void onMapClick(LatLng point) {
tvLocInfo.setText(point.toString());
myMap.animateCamera(CameraUpdateFactory.newLatLng(point));
markerClicked = false;
}
@Override
public void onMapLongClick(LatLng point) {
tvLocInfo.setText("New marker added@" + point.toString());
myMap.addMarker(new MarkerOptions().position(point).title(point.toString()));
markers.add(point);
markerClicked = false;
}
@Override
public boolean onMarkerClick(Marker marker) {
if(markerClicked){
if(polygon != null){
polygon.remove();
polygon = null;
}
polygonOptions.add(marker.getPosition());
polygonOptions.strokeColor(Color.BLUE);
polygonOptions.fillColor(0x3300FF);
polygonOptions.strokeWidth(3);
polygon = myMap.addPolygon(polygonOptions);
}else{
if(polygon != null){
polygon.remove();
polygon = null;
}
polygonOptions = new PolygonOptions().add(marker.getPosition());
markerClicked = true;
}
return true;
}
}
activity_main xml:
<RelativeLayout 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:background="@drawable/bg"
tools:context="com.app.layout.ZeroActivity" >
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" >
</RelativeLayout>
<ToggleButton
android:id="@+id/toggleButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="GPS"
style="@style/btnStyleAcapulco"
/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:text="Child Tracking System"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/toggleButton1"
android:layout_centerHorizontal="true"
android:text="E-Locator"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/toggleButton1"
android:layout_alignBottom="@+id/toggleButton1"
android:layout_toLeftOf="@+id/toggleButton1"
android:text="GPS :"
android:textAppearance="?android:attr/textAppearanceSmall" />
<Button
android:id="@+id/button1"
style="@style/btnStyleAcapulco"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="54dp"
android:text="Track Your Child"
android:layout_below="@+id/textView2"
android:layout_toLeftOf="@+id/textView3"
android:layout_toStartOf="@+id/textView3" />
<Button
android:id="@+id/button2"
style="@style/btnStyleAcapulco"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Set GeoFence"
android:layout_below="@+id/button1"
android:layout_marginTop="59dp"
android:layout_alignLeft="@+id/button1"
android:layout_alignStart="@+id/button1"
android:layout_alignRight="@+id/button1"
android:layout_alignEnd="@+id/button1" />
<Button
android:id="@+id/button3"
style="@style/btnStyleAcapulco"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Set Speed Limit"
android:layout_below="@+id/button2"
android:layout_alignLeft="@+id/button2"
android:layout_alignStart="@+id/button2"
android:layout_marginTop="54dp"
android:layout_alignRight="@+id/button2"
android:layout_alignEnd="@+id/button2" />
</RelativeLayout>
activity_maps 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=".MapsActivity"
android:weightSum="1">
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="379dp"
class="com.google.android.gms.maps.MapFragment"
android:layout_weight="0.92" />
<TextView
android:id="@+id/locinfo"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:layout_width="145dp"
android:layout_height="wrap_content"
android:text="Set GeoFence"
android:id="@+id/setgeo"
style="@style/btnStyleAcapulco"
android:layout_gravity="center_horizontal" />
</LinearLayout>
Android Manifest:
`
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"
android:maxSdkVersion="22" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity2Activity"
android:label="@string/title_activity_main_activity2" >
</activity>
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="AIzaSyCmRNOGr-q6m0p-Qq3QlVmrHkgEr8F39go" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="@string/google_maps_key" />
<activity
android:name=".MapsActivity"
android:label="@string/title_activity_maps" >
</activity>
</application>
</manifest>