简体   繁体   English

调用另一个类时,Android无法启动活动componentinfo错误

[英]Android unable to start activity componentinfo error when calling another class

Im writting an app which calls another class, but everytime I do, i keep getting the "unable to start activity componentinfo" error. 我正在编写一个调用另一个类的应用程序,但是每次我这样做时,我总是收到“无法启动activity componentinfo”错误。

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 05-30 15:33:33.161:E / AndroidRuntime(916):致命异常:主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): 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 and :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):在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(本机方法)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) :在和 roid.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 roid.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个

Looks to me like a NullPointerException in some of your code that isn't part of the snippet. 在我看来,某些代码中的NullPointerException并非代码段的一部分。

05-30 15:33:33.161: E/AndroidRuntime(916): Caused by: java.lang.NullPointerException 05-30 15:33:33.161:E / AndroidRuntime(916):原因: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):在com.google.android.maps.ItemizedOverlay.boundCenterBottom(ItemizedOverlay.java:158)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM