0

Im writting an app which calls another class, but everytime I do, i keep getting the "unable to start activity componentinfo" error.

the code is below:

  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);
    }




}

and from this class im trying to call this class:

    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

please help

UPDATED*

05-30 15:33:33.161: E/AndroidRuntime(916): FATAL EXCEPTION: main 05-30 15:33:33.161: E/AndroidRuntime(916): java.lang.RuntimeException: Unable to start activity ComponentInfo{android.Maps.Google2/android.Maps.HomeScreen}: java.lang.NullPointerException 05-30 15:33:33.161: E/AndroidRuntime(916): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 05-30 15:33:33.161: E/AndroidRuntime(916): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 05-30 15:33:33.161: E/AndroidRuntime(916): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 05-30 15:33:33.161: E/AndroidRuntime(916): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 05-30 15:33:33.161: E/AndroidRuntime(916): at android.os.Handler.dispatchMessage(Handler.java:99) 05-30 15:33:33.161: E/AndroidRuntime(916): at android.os.Looper.loop(Looper.java:123) 05-30 15:33:33.161: E/AndroidRuntime(916): at android.app.ActivityThread.main(ActivityThread.java:4627) 05-30 15:33:33.161: E/AndroidRuntime(916): at java.lang.reflect.Method.invokeNative(Native Method) 05-30 15:33:33.161: E/AndroidRuntime(916): at java.lang.reflect.Method.invoke(Method.java:521) 05-30 15:33:33.161: E/AndroidRuntime(916): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 05-30 15:33:33.161: E/AndroidRuntime(916): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 05-30 15:33:33.161: E/AndroidRuntime(916): at dalvik.system.NativeStart.main(Native Method) 05-30 15:33:33.161: E/AndroidRuntime(916): Caused by: java.lang.NullPointerException 05-30 15:33:33.161: E/AndroidRuntime(916): at com.google.android.maps.ItemizedOverlay.boundCenterBottom(ItemizedOverlay.java:158) 05-30 15:33:33.161: E/AndroidRuntime(916): at android.Maps.MyPositionOverlay.(MyPositionOverlay.java:28) 05-30 15:33:33.161: E/AndroidRuntime(916): at android.Maps.GeneticAlgorithm2.(GeneticAlgorithm2.java:51) 05-30 15:33:33.161: E/AndroidRuntime(916): at android.Maps.HomeScreen.onCreate(HomeScreen.java:76) 05-30 15:33:33.161: E/AndroidRuntime(916): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 05-30 15:33:33.161: E/AndroidRuntime(916): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 05-30 15:33:33.161: E/AndroidRuntime(916): ... 11 more

MurphyApps
  • 13
  • 2
  • 11
  • 2
    Show us the complete stack trace please. And please remove some code. Posting hundreds of LoC will not help in any way. – WarrenFaith May 30 '12 at 15:46
  • Sorry for the excessive amounts of LoC. – MurphyApps May 30 '12 at 15:54
  • Perhaps its how you call the class? instead of `GA2.test()` replace it with `new GeneticAlgorithm2()`. See if that does anything – wdziemia May 30 '12 at 16:04
  • Nothing special going on here, you are just getting a NullPointerException in "ItemizedOverlay", which is called by "MyPositionOverlay", which is called on line 51 of GeneticAlgorithm2. The reason you are seeing the "unable to start activity" error is that it is all being called from the onCreate method, so it isn't able to create the Activity due to the exception. – pents90 May 30 '12 at 16:09
  • So test() is a method i want to run, and further up the code I do have GA2 = new GeneticAlgorithm2();, I have also tried GeneticAlgorithm2 GA2 = new GeneticAlgorithm2(); to no such luck – MurphyApps May 30 '12 at 16:10

1 Answers1

0

Looks to me like a NullPointerException in some of your code that isn't part of the snippet.

05-30 15:33:33.161: E/AndroidRuntime(916): Caused by: java.lang.NullPointerException

05-30 15:33:33.161: E/AndroidRuntime(916): at com.google.android.maps.ItemizedOverlay.boundCenterBottom(ItemizedOverlay.java:158)

Community
  • 1
  • 1
Tyler
  • 19,113
  • 19
  • 94
  • 151