簡體   English   中英

調用另一個類時,Android無法啟動活動componentinfo錯誤

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

我正在編寫一個調用另一個類的應用程序,但是每次我這樣做時,我總是收到“無法啟動activity componentinfo”錯誤。

代碼如下:

  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):致命異常:主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):在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):位於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個

在我看來,某些代碼中的NullPointerException並非代碼段的一部分。

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM