我已经编写了一些代码,在一个类中我创建并填充了一个数组列表,我可以从另一个类调用并打印出来,但是当我尝试从第三个类调用它时会导致移动应用程序关。这是数组列表的代码:
public class MyPositionOverlay extends ItemizedOverlay<OverlayItem> {
public ArrayList<OverlayItem> overlayItemList = new ArrayList<OverlayItem>();
Context context;
public MyPositionOverlay(Drawable marker, Context c) {
super(boundCenterBottom(marker));
// TODO Auto-generated constructor stub
populate();
context = c;
}
private final int mRadius = 5;
Location location;
public Location getLocation() {
return location;
}
public void setLocation(Location location) {;
this.location = location;
}
//Method for user intputs - fill in
@Override
public boolean onTap(GeoPoint point, MapView mapView) {
if(super.onTap(point, mapView)){
return true;
}
String title = "Point:" + String.valueOf(overlayItemList.size()+ 1) + "\n" +
"Lat:" + "\n" + String.valueOf(point.getLatitudeE6() +
"Long:" + "\n" + String.valueOf(point.getLongitudeE6()));
String snippet = "geo:\n"
+ String.valueOf(point.getLatitudeE6()) + "\n"
+ String.valueOf(point.getLongitudeE6());
addItem(point, title, snippet);
//return true;
//if(hit test is true)
// perform task
// return true;
//if not handled
return false;
}
@Override
protected boolean onTap(int index) {
// TODO Auto-generated method stub
//return super.onTap(index);
Toast.makeText(context,
"Touch on marker: \n" + overlayItemList.get(index).getTitle(),
Toast.LENGTH_LONG).show();
return true;
}
public void addItem(GeoPoint point, String title, String snippet){
OverlayItem newItem = new OverlayItem(point, title, snippet);
overlayItemList.add(newItem);
populate();
}
@Override
protected OverlayItem createItem(int i) {
// TODO Auto-generated method stub
return overlayItemList.get(i);
}
@Override
public int size() {
// TODO Auto-generated method stub
return overlayItemList.size();
}
}
我只是尝试从下面的课程中调用它:
public class GeneticAlgorithm2 {
//public int populationSize;
//private MyPositionOverlay popList;
MyPositionOverlay popSize;
MyPositionOverlay positionOverlay;
HomeScreen homeScreenPop;
//MyPositionOverlay overlaySize = new MyPositionOverlay(null, null);
//int populationSize2 = popSize.size();
int populationSize = 800;
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;//populationSize2;
//int numOfCities = popSize.overlayItemList.size();
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 = popSize.overlayItemList.size();
//start();
//test();
//int populationSize3 = popSize.size();
}*/
public void test(){
System.out.println("test");
}
public void start() {
// Calling My Position Overlay class
//popSize = new MyPositionOverlay(null, null);
//Sets the population size to the number of markers
//populationSize = popSize.overlayItemList.size();
//popSize.size();
//int populationSize2 = popSize.size();
//int populationSize3 = positionOverlay.size();
System.out.println("Start ...");
// generate a set of cities
System.out.println(positionOverlay.size());
//System.out.println( homeScreenPop.positionOverlay.size());
}
}
以下是我收到的错误消息:
06-06 17:14:05.210: W/dalvikvm(1301): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
06-06 17:14:05.220: E/AndroidRuntime(1301): FATAL EXCEPTION: main
06-06 17:14:05.220: E/AndroidRuntime(1301): java.lang.NullPointerException
06-06 17:14:05.220: E/AndroidRuntime(1301): at android.Maps.GeneticAlgorithm2.start(GeneticAlgorithm2.java:97)
06-06 17:14:05.220: E/AndroidRuntime(1301): at android.Maps.HomeScreen$3.onClick(HomeScreen.java:152)
06-06 17:14:05.220: E/AndroidRuntime(1301): at android.view.View.performClick(View.java:2408)
06-06 17:14:05.220: E/AndroidRuntime(1301): at android.view.View$PerformClick.run(View.java:8816)
06-06 17:14:05.220: E/AndroidRuntime(1301): at android.os.Handler.handleCallback(Handler.java:587)
06-06 17:14:05.220: E/AndroidRuntime(1301): at android.os.Handler.dispatchMessage(Handler.java:92)
06-06 17:14:05.220: E/AndroidRuntime(1301): at android.os.Looper.loop(Looper.java:123)
06-06 17:14:05.220: E/AndroidRuntime(1301): at android.app.ActivityThread.main(ActivityThread.java:4627)
06-06 17:14:05.220: E/AndroidRuntime(1301): at java.lang.reflect.Method.invokeNative(Native Method)
06-06 17:14:05.220: E/AndroidRuntime(1301): at java.lang.reflect.Method.invoke(Method.java:521)
06-06 17:14:05.220: E/AndroidRuntime(1301): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-06 17:14:05.220: E/AndroidRuntime(1301): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-06 17:14:05.220: E/AndroidRuntime(1301): at dalvik.system.NativeStart.main(Native Method)
从我所看到的是,它表示数组列表为空,但就像我说我可以愉快地从另一个类输出它。
请帮助。
答案 0 :(得分:3)
在start
方法positionOverlay
为null时,您刚刚声明了它,但在访问此字段之前尚未初始化它,因此NPE。