我的应用程序崩溃问题

时间:2014-12-18 05:22:04

标签: java android nullpointerexception android-logcat

我正在编写一个应用程序,向您显示一个有公交车站的路线,您可以知道哪一个最接近您的位置。 这只是MapActivity1的测试,但Android Studio给我一个错误,它说Caused by: java.lang.NullPointerException.

package com.example.unobtainium.map1;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;

import android.app.Activity;

import android.util.Log;

import com.google.android.gms.maps.CameraUpdateFactory;

import com.google.android.gms.maps.MapFragment;

import com.google.android.gms.maps.model.PolylineOptions;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;

import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

import java.util.ArrayList;
import java.util.List;


public class MapsActivity1 extends FragmentActivity {

private GoogleMap mMap; // Might be null if Google Play services APK is not available.


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps_activity1);
    setUpMapIfNeeded();
}

@Override
protected void onResume() {
    super.onResume();
    setUpMapIfNeeded();
}


 /*
 * Sets up the map if it is possible to do so (i.e., the Google Play services APK is correctly
 * installed) and the map has not already been instantiated.. This will ensure that we only ever
 * call {@link #setUpMap()} once when {@link #mMap} is not null.
 * <p/>
 * If it isn't installed {@link SupportMapFragment} (and
 * {@link com.google.android.gms.maps.MapView MapView}) will show a prompt for the user to
 * install/update the Google Play services APK on their device.
 * <p/>
 * A user can return to this FragmentActivity after following the prompt and correctly
 * installing/updating/enabling the Google Play services. Since the FragmentActivity may not
 * have been completely destroyed during this process (it is likely that it would only be
 * stopped or paused), {@link #onCreate(Bundle)} may not be called again so we should call this
 * method in {@link #onResume()} to guarantee that it will be called.
 */

private void setUpMapIfNeeded() {
    // Do a null check to confirm that we have not already instantiated the map.
    if (mMap == null) {
        // Try to obtain the map from the SupportMapFragment.
        mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                .getMap();
        // Check if we were successful in obtaining the map.
        if (mMap != null) {
            mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
            mMap.setMyLocationEnabled(true);
            setUpMap();
        }
    }
}

/*
 * This is where we can add markers or lines, add listeners or move the camera. In this case, we
 * just add a marker near Africa.
 * <p/>
 * This should only be called once and when we are sure that {@link #mMap} is not null.
 */





private List<LatLng> positions = new ArrayList<>();
{
    positions.add(0,new LatLng(0,0));
    positions.add(1,new LatLng(1,-32));
    positions.add(2,new LatLng(-12,12));
    positions.add(3,new LatLng(-13,73));
    positions.add(4,new LatLng(4,4));
}


public static double nearest(double lat1, double lat2, double lon1, double lon2) {
    double earthRadius = 6371; //kilometers
    double dLat = Math.toRadians(lat2-lat1);
    double dLng = Math.toRadians(lon2-lon1);
    double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
            Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
                    Math.sin(dLng/2) * Math.sin(dLng/2);
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));

    return (float) (earthRadius * c);

}



private void setUpMap() {
    mMap.addMarker(new MarkerOptions()
            .position(new LatLng(-12.138837,-77.017262))
            .title("Paradero Inicial")
            .snippet("Cruce de las avenidas El Sol y Pedro Heraud") );
    mMap.addMarker(new MarkerOptions()
            .position(new LatLng(-12.141921,-77.013807))
            .title("Segundo paradero")
            .snippet("Cruce entre las avenidas Pedro Heraud y Lima"));





    double currentLat=0;

    double currentLng=0;

    double mindist= 0;
    double latit=0;
    double longit=0;
    double distance=0;
    for (int i = 0; i < positions.size(); i++) {

        mMap.addMarker(new MarkerOptions()
                .position(positions.get(i)));
        currentLat = mMap.getMyLocation().getLatitude();
        currentLng = mMap.getMyLocation().getLongitude();
        latit = positions.get(i).latitude;
        longit = positions.get(i).longitude;


        distance= nearest(currentLat,latit,currentLng,longit);
        if (distance<mindist){
            mindist=distance;

        }

    }

  //  CameraUpdate yourNewLocation = CameraUpdateFactory.newLatLngZoom(new LatLng(newLat,newLon), 12);
   // mMap.animateCamera(yourNewLocation);

   LatLng coord= new LatLng(latit,longit);

   new AlertDialog.Builder(this)
            .setTitle("Nearest Found ")
            .setMessage("Este es el paradero más cercano a tu posición" + "   " + coord)
            .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    // continue with delete
                }
            })
            .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    // do nothing
                }
            })
            .setIcon(android.R.drawable.ic_dialog_alert)
            .show();


    mMap.addPolyline(new PolylineOptions()
             .addAll(positions)
             .color(-16711681)
             .width(8.2f));

    mMap.addPolyline(new PolylineOptions()

                    .add(new LatLng(-12.143568, -77.015278))
                    .add(new LatLng(-12.141911, -77.013808))
                    .add(new LatLng(-12.138848, -77.017273))
                    .add(new LatLng(-12.139309, -77.018754))
                    .add(new LatLng(-12.139519, -77.022262))
                    .add(new LatLng(-12.137914, -77.022434))
                    .add(new LatLng(-12.135774, -77.022713))
                    .add(new LatLng(-12.13492, -77.023039))
                    .add(new LatLng(-12.134185, -77.02327))
                    .add(new LatLng(-12.132313, -77.023452))
                    .add(new LatLng(-12.134054, -77.023393))
                    .add(new LatLng(-12.134563, -77.023447))
                    .add(new LatLng(-12.135156, -77.025206))
                    .add(new LatLng(-12.131458,-77.029814))
                    .add(new LatLng(-12.127818,-77.029519))
                    .add(new LatLng(-12.12453,-77.029385))
                    .add(new LatLng(-12.122406,-77.029106))
                    .add(new LatLng(-12.11906,-77.028865))
                    .add(new LatLng(-12.103508,-77.03151))
                    .add(new LatLng(-12.064172,-77.037464))
                    .add(new LatLng(-12.053066,-77.038432))
                    .add(new LatLng(-12.048764,-77.039076))
                    .add(new LatLng(-12.032563,-77.028218))
                    .add(new LatLng(-12.031965,-77.027605))
                    .add(new LatLng(-12.026309,-77.034042))
                    //.add(new LatLng())
                    .color(-16711681)
                    .width(8.2f)
            // .visible(true)
    );

    mMap.addPolyline(new PolylineOptions()
                    .add(new LatLng(-12.126901,-77.029463))
                    .add(new LatLng(-12.124551,-77.029291))
                    .add(new LatLng(-12.119348,-77.028882))
                    .color(-16711681)
                    .width(12.2f)
    );
    LatLng myCoordinates = new LatLng(-12.096889,-77.024376);
    CameraUpdate yourLocation = CameraUpdateFactory.newLatLngZoom(myCoordinates, 12);
    mMap.animateCamera(yourLocation);
}



}

正如你所看到的,我已经创建了一个位置数组,这使我可以在所有位置放置标记并制作折线(这是一个独立的测试,除了主坐标周围〜 -12,-77〜)。

这是logcat:

12-17 23:39:18.085    1847-1847/com.example.unobtainium.map1 D/dalvikvm﹕ GC_FOR_ALLOC freed 254K, 7% free 11860K/12652K, paused 4ms, total 4ms
12-17 23:39:18.089    1847-1847/com.example.unobtainium.map1 I/dalvikvm-heap﹕ Grow heap (frag case) to 12.031MB for 444705-byte allocation
12-17 23:39:18.093    1847-1847/com.example.unobtainium.map1 D/dalvikvm﹕ GC_FOR_ALLOC freed 510K, 10% free 11784K/13088K, paused 6ms, total 6ms
12-17 23:39:18.105    1847-1847/com.example.unobtainium.map1 D/dalvikvm﹕ GC_FOR_ALLOC freed 244K, 8% free 12040K/13088K, paused 5ms, total 5ms
12-17 23:39:18.105    1847-1847/com.example.unobtainium.map1 I/dalvikvm-heap﹕ Grow heap (frag case) to 12.279MB for 520618-byte allocation
12-17 23:39:18.113    1847-1847/com.example.unobtainium.map1 D/dalvikvm﹕ GC_FOR_ALLOC freed 254K, 10% free 12295K/13600K, paused 6ms, total 6ms
12-17 23:39:18.125    1847-1847/com.example.unobtainium.map1 D/dalvikvm﹕ GC_FOR_ALLOC freed 750K, 9% free 12475K/13600K, paused 6ms, total 6ms
12-17 23:39:18.137    1847-1847/com.example.unobtainium.map1 D/dalvikvm﹕ GC_FOR_ALLOC freed 254K, 7% free 12729K/13600K, paused 9ms, total 10ms
12-17 23:39:18.189    1847-1851/com.example.unobtainium.map1 D/dalvikvm﹕ GC_CONCURRENT freed 663K, 6% free 13255K/14036K, paused 1ms+1ms, total 7ms
12-17 23:39:18.297    1847-1847/com.example.unobtainium.map1 D/dalvikvm﹕ GC_FOR_ALLOC freed 296K, 3% free 14450K/14772K, paused 8ms, total 8ms
12-17 23:39:18.381    1847-1847/com.example.unobtainium.map1 D/dalvikvm﹕ GC_FOR_ALLOC freed 1248K, 8% free 15089K/16364K, paused 7ms, total 7ms
12-17 23:39:18.473    1847-1847/com.example.unobtainium.map1 D/dalvikvm﹕ GC_FOR_ALLOC freed 451K, 3% free 16738K/17216K, paused 8ms, total 8ms
12-17 23:39:18.541    1847-1863/com.example.unobtainium.map1 W/ActivityThread﹕ ClassLoader.loadClass: The class loader returned by Thread.getContextClassLoader() may fail for processes that host multiple applications. You should explicitly specify a context class loader. For example: Thread.setContextClassLoader(getClass().getClassLoader());
12-17 23:39:18.597    1847-1847/com.example.unobtainium.map1 D/AndroidRuntime﹕ Shutting down VM
12-17 23:39:18.597    1847-1847/com.example.unobtainium.map1 W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xa4c2a648)
12-17 23:39:18.597    1847-1847/com.example.unobtainium.map1 E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.unobtainium.map1/com.example.unobtainium.map1.MapsActivity1}:     java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.unobtainium.map1.MapsActivity1.setUpMap(MapsActivity1.java:148)
at com.example.unobtainium.map1.MapsActivity1.setUpMapIfNeeded(MapsActivity1.java:78)
at com.example.unobtainium.map1.MapsActivity1.onCreate(MapsActivity1.java:42)
at android.app.Activity.performCreate(Activity.java:5133)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
12-17 23:39:18.785    1847-1863/com.example.unobtainium.map1 D/dalvikvm﹕ GC_FOR_ALLOC freed 2207K, 12% free 17182K/19416K, paused 9ms, total 10ms
12-17 23:39:18.189    1847-1851/com.example.unobtainium.map1 D/dalvikvm﹕ GC_CONCURRENT freed 663K, 6% free 13255K/14036K, paused 1ms+1ms, total 7ms

我对Android开发很新,所以我很确定我的代码是无效的,请告诉我。 对不起,我的英语不好。 谢谢你的回答。

0 个答案:

没有答案