我正在编写一个调用另一个类的应用程序,但每次我这样做时,我都会收到“无法启动活动组件信息”错误。
代码如下:
package android.Maps;
public class HomeScreen extends MapActivity {
@Override
protected boolean isRouteDisplayed() {
return false;
}
MapController mapController;
MyPositionOverlay positionOverlay;
GeneticAlgorithm2 GA2;
//private MyPositionOverlay popListHome;
//public static int populationSizeHome;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//popListHome = new MyPositionOverlay(null, null);
//populationSizeHome = popListHome.overlayItemList();
//positionOverlay.overlayItemList
MapView myMapView = (MapView)findViewById(R.id.myMapView);
//myMapView.setBuiltInZoomControls(true);
mapController = myMapView.getController();
myMapView.setSatellite(true);
myMapView.setStreetView(true);
myMapView.displayZoomControls(false);
mapController.setZoom(17);
Drawable marker=getResources().getDrawable(android.R.drawable.star_big_on);
int markerWidth = marker.getIntrinsicWidth();
int markerHeight = marker.getIntrinsicHeight();
marker.setBounds(0, markerHeight, markerWidth, 0);
//Call Genetic Algorithm
GA2 = new GeneticAlgorithm2();
// Add the MyPositionOverlay
positionOverlay = new MyPositionOverlay(marker, HomeScreen.this);
List<Overlay> overlays = myMapView.getOverlays();
overlays.add(positionOverlay);
LocationManager locationManager;
String context = Context.LOCATION_SERVICE;
locationManager = (LocationManager)getSystemService(context);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
String provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
updateWithNewLocation(location);
locationManager.requestLocationUpdates(provider, 2000, 10,
locationListener);
//GeneticAlgorithm2 GA2 = new GeneticAlgorithm2();
final Button buttonRun = (Button) findViewById(R.id.btnRun);
buttonRun.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Context context = getApplicationContext();
CharSequence text = "Running";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
//This statement works
System.out.println(positionOverlay.overlayItemList.size());
Iterator<OverlayItem> iter = positionOverlay.overlayItemList.iterator();
System.out.println(positionOverlay.overlayItemList);
while (iter.hasNext()){
Object element = iter.next();
System.out.println(element + "");
}
//GeneticAlgorithm2 GA2;
//GA2.start();
GA2.test();
}// End of onClick
});
}// End of onCreate
/*public HomeScreen (){
popListHome = new MyPositionOverlay(null, null);
populationSizeHome = popListHome.overlayItemList();
}*/
private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
updateWithNewLocation(location);
}
public void onProviderDisabled(String provider){
updateWithNewLocation(null);
}
public void onProviderEnabled(String provider){ }
public void onStatusChanged(String provider, int status,
Bundle extras){ }
};
private void updateWithNewLocation(Location location) {
String latLongString;
TextView myLocationText;
myLocationText = (TextView)findViewById(R.id.myLocationText);
String addressString = "No address found";
if (location != null) {
// Update my location marker
positionOverlay.setLocation(location);
// Update the map location.
Double geoLat = location.getLatitude()*1E6;
Double geoLng = location.getLongitude()*1E6;
GeoPoint point = new GeoPoint(geoLat.intValue(),
geoLng.intValue());
mapController.animateTo(point);
double lat = location.getLatitude();
double lng = location.getLongitude();
latLongString = "Lat:" + lat + "\nLong:" + lng;
double latitude = location.getLatitude();
double longitude = location.getLongitude();
Geocoder gc = new Geocoder(this, Locale.getDefault());
try {
List<Address> addresses = gc.getFromLocation(latitude,
longitude, 1);
StringBuilder sb = new StringBuilder();
if (addresses.size() > 0) {
Address address = addresses.get(0);
for (int i = 0; i < address.getMaxAddressLineIndex(); i++)
sb.append(address.getAddressLine(i)).append("\n");
sb.append(address.getLocality()).append("\n");
sb.append(address.getPostalCode()).append("\n");
sb.append(address.getCountryName());
}
addressString = sb.toString();
} catch (IOException e) {}
} else {
latLongString = "No location found";
}
myLocationText.setText("Your Current Position is:\n" +
latLongString + "\n" + addressString);
}
}
从这个类我试图调用这个类:
package android.Maps;
public class GeneticAlgorithm2 {
public static int populationSize;
private MyPositionOverlay popList;
public int route[] = new int[populationSize +1];
int pointLimit;
int iterations;
double mutationRate = 0.10;
//Canvas C = null;
//Dimension CD;
//Label L;
//Graphics GC;
//int Initial_population = 800;
int matingPopulation = populationSize/2;
int favPopulation = matingPopulation/2;
int numOfCities = 30;
int cutLength = numOfCities/5;
//double Mutation_probability = 0.10;
double loops=1.5;
int Epoch;
double xlow = 0.0;
double ylow = 0.0;
double xhigh = 100.0;
double yhigh = 100.0;
double xrange, yrange;
double minCost = 5.0;
double timeStart,timeEnd;
Thread T = null;
double costPerfect = 0.;
boolean started = false;
City [] cities;
Chromosome [] chromosomes;
public GeneticAlgorithm2() {
// Calling My Position Overlay class
popList = new MyPositionOverlay(null, null);
//Sets the population size to the number of markers
populationSize = popList.overlayItemList.size();
//start();
test();
}
public void test(){
System.out.println("test");
}
}//End of Genetic Algorithm 2 class
请帮忙
更新*
05-30 15:33:33.161: E/AndroidRuntime(916): 致命异常: main 05-30 15:33:33.161: E/AndroidRuntime(916): java.lang.RuntimeException: 无法启动活动 ComponentInfo{android. Maps.Google2/android.Maps.HomeScreen}:java.lang.NullPointerException 05-30 15:33:33.161:E/AndroidRuntime(916):在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 05-30 15:33:33.161: E/AndroidRuntime(916): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 05-30 15:33:33.161: E/AndroidRuntime(916): 在 android.app.ActivityThread .access$2300(ActivityThread.java:125) 05-30 15:33:33.161: E/AndroidRuntime(916): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 05-30 15:33: 33.161: E/AndroidRuntime(916): 在 android.os.Handler.dispatchMessage(Handler.java:99) 05-30 15:33:33.161:E/AndroidRuntime(916): 在 android.os.Looper.loop(Looper.java:123) 05-30 15:33:33.161: E/AndroidRuntime(916): 在 android.app.ActivityThread.main(ActivityThread.java :4627) 05-30 15:33:33.161: E/AndroidRuntime(916): 在 java.lang.reflect.Method.invokeNative(Native Method) 05-30 15:33:33.161: E/AndroidRuntime(916): 在java.lang.reflect.Method.invoke(Method.java:521) 05-30 15:33:33.161: E/AndroidRuntime(916): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java :868) 05-30 15:33:33.161: E/AndroidRuntime(916): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 05-30 15:33:33.161: E/AndroidRuntime (916): 在 dalvik.system.NativeStart.main(Native Method) 05-30 15:33:33.161: E/AndroidRuntime(916): 由: java.lang.NullPointerException 05-30 15:33:33.161: E /AndroidRuntime(916):在 com.google.android.maps。ItemizedOverlay.boundCenterBottom(ItemizedOverlay.java:158) 05-30 15:33:33.161: E/AndroidRuntime(916): 在 android.Maps.MyPositionOverlay.(MyPositionOverlay.java:28) 05-30 15:33:33.161: E /AndroidRuntime(916): 在 android.Maps.GeneticAlgorithm2.(GeneticAlgorithm2.java:51) 05-30 15:33:33.161: E/AndroidRuntime(916): 在 android.Maps.HomeScreen.onCreate(HomeScreen.java:76 ) 05-30 15:33:33.161: E/AndroidRuntime(916): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 05-30 15:33:33.161: E/AndroidRuntime(916): 在 android .app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 05-30 15:33:33.161: E/AndroidRuntime(916): ... 11 更多E/AndroidRuntime(916): 在 android.Maps.GeneticAlgorithm2.(GeneticAlgorithm2.java:51) 05-30 15:33:33.161: E/AndroidRuntime(916): 在 android.Maps.HomeScreen.onCreate(HomeScreen.java: 76) 05-30 15:33:33.161: E/AndroidRuntime(916): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 05-30 15:33:33.161: E/AndroidRuntime(916): 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 05-30 15:33:33.161: E/AndroidRuntime(916): ... 11 更多E/AndroidRuntime(916): 在 android.Maps.GeneticAlgorithm2.(GeneticAlgorithm2.java:51) 05-30 15:33:33.161: E/AndroidRuntime(916): 在 android.Maps.HomeScreen.onCreate(HomeScreen.java: 76) 05-30 15:33:33.161: E/AndroidRuntime(916): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 05-30 15:33:33.161: E/AndroidRuntime(916): 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 05-30 15:33:33.161: E/AndroidRuntime(916): ... 11 更多ActivityThread.performLaunchActivity(ActivityThread.java:2627) 05-30 15:33:33.161: E/AndroidRuntime(916): ... 11 更多ActivityThread.performLaunchActivity(ActivityThread.java:2627) 05-30 15:33:33.161: E/AndroidRuntime(916): ... 11 更多