0
我編寫了一些代碼,並在一個類中創建並填充了一個數組列表,並且我可以從另一個類中調用並打印出來,但是當我嘗試調用它時從第三課開始就導致移動應用程序關閉。下面是數組列表代碼:Android調用數組列表會導致程序崩潰
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)
從我可以看到它是說數組列表爲空,但就像我說我可以愉快地從另一個類輸出它。
請幫忙。
好的愚蠢的問題,我將如何初始化它? – MurphyApps
這是一個很難回答的問題,不理解你的用例,反正看看這個[教程](http://android-coding.blogspot.sg/2011/06/using-itemizedoverlay-to-add-marker- on.html) – mprabhat
好的,但在我的腦海裏應該是工作。我在頂部MyPositionOverlay positionOverlay;它將我鏈接到數組列表所在的類,然後在start方法中我有int populationSize3 = positionOverlay.size(); 。我對另一個班級使用了相同的流程,並且工作良好 – MurphyApps