2017-04-19 51 views
-3

我从设备和服务器获取经度和纬度。它也显示。但是当我尝试计算距离时,应用程序崩溃。计算距离时应用程序崩溃

这是我MainActivity.java

import android.Manifest; 
import android.app.ProgressDialog; 
import android.content.Context; 
import android.content.Intent; 
import android.content.pm.PackageManager; 
import android.location.Criteria; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.os.Parcelable; 
import android.support.v4.app.ActivityCompat; 
import android.support.v7.app.AlertDialog; 
import android.support.v7.app.AppCompatActivity; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.Toast; 

import com.android.volley.RequestQueue; 
import com.android.volley.Response; 
import com.android.volley.VolleyError; 
import com.android.volley.toolbox.StringRequest; 
import com.android.volley.toolbox.Volley; 

import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import static com.android.volley.Request.Method.GET; 
import static com.example.john.bustrackclient.R.id.textView4; 

public class MainActivity extends AppCompatActivity implements View.OnClickListener,LocationListener { 

    LocationManager locationManager; 
    String mprovider; 
    String latitude, longitude,latitude1,longitude1; 

    private EditText editTextId; 
    private Button buttonGet; 
    private TextView textViewResult,textView6; 

    private ProgressDialog loading; 

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

     editTextId = (EditText) findViewById(R.id.editTextId); 
     buttonGet = (Button) findViewById(R.id.buttonGet); 
     textViewResult = (TextView) findViewById(R.id.textViewResult); 

     buttonGet.setOnClickListener(this); 

     locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     Criteria criteria = new Criteria(); 

     mprovider = locationManager.getBestProvider(criteria, false); 

     if (mprovider != null && !mprovider.equals("")) { 
      if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
       return; 
      } 
      Location location = locationManager.getLastKnownLocation(mprovider); 
      locationManager.requestLocationUpdates(mprovider, 15000, 1, this); 

      if (location != null) 
       onLocationChanged(location); 
      else 
       Toast.makeText(getBaseContext(), "No Location Provider Found Check Your Code", Toast.LENGTH_SHORT).show(); 
     } 
    } 

    public void onLocationChanged(Location location) { 
     TextView latitude = (TextView) findViewById(R.id.textView2); 
     TextView longitude = (TextView) findViewById(R.id.textView3); 

     latitude.setText("Latitude: " + location.getLatitude()); 
     longitude.setText("Longitude: " + location.getLongitude()); 

    } 

    public void onStatusChanged(String s, int i, Bundle bundle) { 
    } 

    public void onProviderEnabled(String s) { 
    } 

    public void onProviderDisabled(String s) { 
    } 


    private void getData() { 
     String destination = editTextId.getText().toString().trim(); 
     if (destination.equals("")) { 
      Toast.makeText(this, "Please enter your destination", Toast.LENGTH_LONG).show(); 
      return; 
     } 
     loading = ProgressDialog.show(this, "Please wait...", "Fetching...", false, false); 

     String url = Config.DATA_URL + editTextId.getText().toString().trim(); 

     StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() { 
      @Override 
      public void onResponse(String response) { 
       loading.dismiss(); 
       showJSON(response); 
      } 
     }, 
       new Response.ErrorListener() { 
        @Override 
        public void onErrorResponse(VolleyError error) { 
         Toast.makeText(MainActivity.this, error.getMessage().toString(), Toast.LENGTH_LONG).show(); 
        } 
       }); 

     RequestQueue requestQueue = Volley.newRequestQueue(this); 
     requestQueue.add(stringRequest); 
    } 

    private void showJSON(String response){ 
     latitude1=""; 
     longitude1=""; 
     try { 
      JSONObject jsonObject = new JSONObject(response); 
      JSONArray result = jsonObject.getJSONArray(Config.JSON_ARRAY); 
      JSONObject updata = result.getJSONObject(0); 
      latitude1 = updata.getString(Config.KEY_LATITUDE); 
      longitude1 = updata.getString(Config.KEY_LONGITUDE); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
     textViewResult.setText("Latitude: "+latitude1+"\nLongitude: "+longitude1); 
    } 



    @Override 
    public void onClick(View v) 
    { 

     getData(); 
     int MILLION = 1000000; 
     int EARTH_RADIUS_KM = 6371; 

     double lati1=Double.parseDouble(latitude); 
     double long1=Double.parseDouble(longitude); 
     double lati2=Double.parseDouble(latitude1); 
     double long2=Double.parseDouble(longitude1); 

     double lat1 = lati1/MILLION; 
     double lon1 = long1/MILLION; 
     double lat2 = lati2/MILLION; 
     double lon2 = long2/MILLION; 

     double lat1Rad = Math.toRadians(lat1); 
     double lat2Rad = Math.toRadians(lat2); 
     double deltaLonRad = Math.toRadians(lon2 - lon1); 

     double dist = Math 
       .acos(Math.sin(lat1Rad) * Math.sin(lat2Rad) + Math.cos(lat1Rad) 
         * Math.cos(lat2Rad) * Math.cos(deltaLonRad)) 
       * EARTH_RADIUS_KM; 

     String distance=Double.toString(dist); 
     textView6.setText(distance); 
    } 
} 

这是我的logcat

04-19 15:27:27.717 28686-28686/? D/dalvikvm: Late-enabling CheckJNI 
04-19 15:27:27.785 28686-28686/com.example.john.bustrackclient I/InstantRun: Instant Run Runtime started. Android package is com.example.john.bustrackclient, real application class is null. 
04-19 15:27:27.785 28686-28686/com.example.john.bustrackclient W/InstantRun: No instant run dex files added to classpath 
04-19 15:27:27.789 28686-28686/com.example.john.bustrackclient I/dalvikvm: Could not find method android.view.Window$Callback.onProvideKeyboardShortcuts, referenced from method android.support.v7.view.WindowCallbackWrapper.onProvideKeyboardShortcuts 
04-19 15:27:27.789 28686-28686/com.example.john.bustrackclient W/dalvikvm: VFY: unable to resolve interface method 16119: Landroid/view/Window$Callback;.onProvideKeyboardShortcuts (Ljava/util/List;Landroid/view/Menu;I)V 
04-19 15:27:27.789 28686-28686/com.example.john.bustrackclient D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002 
04-19 15:27:27.789 28686-28686/com.example.john.bustrackclient W/dalvikvm: VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;) 
04-19 15:27:27.789 28686-28686/com.example.john.bustrackclient I/dalvikvm: Could not find method android.view.Window$Callback.onSearchRequested, referenced from method android.support.v7.view.WindowCallbackWrapper.onSearchRequested 
04-19 15:27:27.789 28686-28686/com.example.john.bustrackclient W/dalvikvm: VFY: unable to resolve interface method 16121: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z 
04-19 15:27:27.789 28686-28686/com.example.john.bustrackclient D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002 
04-19 15:27:27.793 28686-28686/com.example.john.bustrackclient I/dalvikvm: Could not find method android.view.Window$Callback.onWindowStartingActionMode, referenced from method android.support.v7.view.WindowCallbackWrapper.onWindowStartingActionMode 
04-19 15:27:27.793 28686-28686/com.example.john.bustrackclient W/dalvikvm: VFY: unable to resolve interface method 16125: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode; 
04-19 15:27:27.793 28686-28686/com.example.john.bustrackclient D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002 
04-19 15:27:27.793 28686-28686/com.example.john.bustrackclient I/dalvikvm: Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.widget.TintTypedArray.getChangingConfigurations 
04-19 15:27:27.793 28686-28686/com.example.john.bustrackclient W/dalvikvm: VFY: unable to resolve virtual method 697: Landroid/content/res/TypedArray;.getChangingConfigurations()I 
04-19 15:27:27.793 28686-28686/com.example.john.bustrackclient D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002 
04-19 15:27:27.797 28686-28686/com.example.john.bustrackclient I/dalvikvm: Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.widget.TintTypedArray.getType 
04-19 15:27:27.797 28686-28686/com.example.john.bustrackclient W/dalvikvm: VFY: unable to resolve virtual method 719: Landroid/content/res/TypedArray;.getType (I)I 
04-19 15:27:27.797 28686-28686/com.example.john.bustrackclient D/dalvikvm: VFY: replacing opcode 0x6e at 0x0008 
04-19 15:27:27.833 28686-28686/com.example.john.bustrackclient I/dalvikvm: Could not find method android.widget.FrameLayout.startActionModeForChild, referenced from method android.support.v7.widget.ActionBarContainer.startActionModeForChild 
04-19 15:27:27.833 28686-28686/com.example.john.bustrackclient W/dalvikvm: VFY: unable to resolve virtual method 16553: Landroid/widget/FrameLayout;.startActionModeForChild (Landroid/view/View;Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode; 
04-19 15:27:27.837 28686-28686/com.example.john.bustrackclient D/dalvikvm: VFY: replacing opcode 0x6f at 0x0002 
04-19 15:27:27.881 28686-28686/com.example.john.bustrackclient I/dalvikvm: Could not find method android.content.Context.getColorStateList, referenced from method android.support.v7.content.res.AppCompatResources.getColorStateList 
04-19 15:27:27.881 28686-28686/com.example.john.bustrackclient W/dalvikvm: VFY: unable to resolve virtual method 430: Landroid/content/Context;.getColorStateList (I)Landroid/content/res/ColorStateList; 
04-19 15:27:27.881 28686-28686/com.example.john.bustrackclient D/dalvikvm: VFY: replacing opcode 0x6e at 0x0006 
04-19 15:27:27.881 28686-28686/com.example.john.bustrackclient I/dalvikvm: Could not find method android.content.res.Resources.getDrawable, referenced from method android.support.v7.widget.ResourcesWrapper.getDrawable 
04-19 15:27:27.889 28686-28686/com.example.john.bustrackclient W/dalvikvm: VFY: unable to resolve virtual method 660: Landroid/content/res/Resources;.getDrawable (ILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable; 
04-19 15:27:27.889 28686-28686/com.example.john.bustrackclient D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002 
04-19 15:27:27.889 28686-28686/com.example.john.bustrackclient I/dalvikvm: Could not find method android.content.res.Resources.getDrawableForDensity, referenced from method android.support.v7.widget.ResourcesWrapper.getDrawableForDensity 
04-19 15:27:27.889 28686-28686/com.example.john.bustrackclient W/dalvikvm: VFY: unable to resolve virtual method 662: Landroid/content/res/Resources;.getDrawableForDensity (IILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable; 
04-19 15:27:27.889 28686-28686/com.example.john.bustrackclient D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002 
04-19 15:27:27.909 28686-28686/com.example.john.bustrackclient E/dalvikvm: Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering 
04-19 15:27:27.909 28686-28686/com.example.john.bustrackclient W/dalvikvm: VFY: unable to resolve instanceof 157 (Landroid/graphics/drawable/RippleDrawable;) in Landroid/support/v7/widget/AppCompatImageHelper; 
04-19 15:27:27.909 28686-28686/com.example.john.bustrackclient D/dalvikvm: VFY: replacing opcode 0x20 at 0x000c 
04-19 15:27:28.093 28686-28686/com.example.john.bustrackclient D/libEGL: loaded /system/lib/egl/libEGL_genymotion.so 

                     [ 04-19 15:27:28.105 28686:28686 D/   ] 
                     HostConnection::get() New Host Connection established 0xb85a4440, tid 28686 
04-19 15:27:28.125 28686-28686/com.example.john.bustrackclient D/libEGL: loaded /system/lib/egl/libGLESv1_CM_genymotion.so 
04-19 15:27:28.129 28686-28686/com.example.john.bustrackclient D/libEGL: loaded /system/lib/egl/libGLESv2_genymotion.so 
04-19 15:27:28.237 28686-28686/com.example.john.bustrackclient W/EGL_genymotion: eglSurfaceAttrib not implemented 
04-19 15:27:28.325 28686-28686/com.example.john.bustrackclient E/OpenGLRenderer: Getting MAX_TEXTURE_SIZE from GradienCache 
04-19 15:27:28.381 28686-28686/com.example.john.bustrackclient E/OpenGLRenderer: MAX_TEXTURE_SIZE: 8192 
04-19 15:27:28.413 28686-28686/com.example.john.bustrackclient E/OpenGLRenderer: Getting MAX_TEXTURE_SIZE from Caches::initConstraints() 
04-19 15:27:28.421 28686-28686/com.example.john.bustrackclient E/OpenGLRenderer: MAX_TEXTURE_SIZE: 8192 
04-19 15:27:28.421 28686-28686/com.example.john.bustrackclient D/OpenGLRenderer: Enabling debug mode 0 
04-19 15:27:28.465 28686-28686/com.example.john.bustrackclient D/dalvikvm: GC_FOR_ALLOC freed 262K, 10% free 3093K/3412K, paused 7ms, total 9ms 
04-19 15:27:35.709 28686-28686/com.example.john.bustrackclient D/dalvikvm: GC_FOR_ALLOC freed 77K, 8% free 3317K/3592K, paused 3ms, total 3ms 
04-19 15:27:35.709 28686-28686/com.example.john.bustrackclient I/dalvikvm-heap: Grow heap (frag case) to 4.369MB for 1127532-byte allocation 
04-19 15:27:35.713 28686-28695/com.example.john.bustrackclient D/dalvikvm: GC_FOR_ALLOC freed 1K, 6% free 4417K/4696K, paused 4ms, total 4ms 
04-19 15:27:35.737 28686-28686/com.example.john.bustrackclient D/AndroidRuntime: Shutting down VM 
04-19 15:27:35.737 28686-28686/com.example.john.bustrackclient W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xa4cebb20) 
04-19 15:27:35.741 28686-28686/com.example.john.bustrackclient E/AndroidRuntime: FATAL EXCEPTION: main 
                       Process: com.example.john.bustrackclient, PID: 28686 
                       java.lang.NullPointerException 
                        at java.lang.StringToReal.parseDouble(StringToReal.java:244) 
                        at java.lang.Double.parseDouble(Double.java:295) 
                        at com.example.john.bustrackclient.MainActivity.onClick(MainActivity.java:151) 
                        at android.view.View.performClick(View.java:4438) 
                        at android.view.View$PerformClick.run(View.java:18422) 
                        at android.os.Handler.handleCallback(Handler.java:733) 
                        at android.os.Handler.dispatchMessage(Handler.java:95) 
                        at android.os.Looper.loop(Looper.java:136) 
                        at android.app.ActivityThread.main(ActivityThread.java:5001) 
                        at java.lang.reflect.Method.invokeNative(Native Method) 
                        at java.lang.reflect.Method.invoke(Method.java:515) 
                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
                        at dalvik.system.NativeStart.main(Native Method) 
04-19 15:27:35.925 28686-28822/com.example.john.bustrackclient D/dalvikvm: GC_FOR_ALLOC freed 292K, 8% free 4644K/4996K, paused 11ms, total 11ms 
04-19 15:27:38.701 28686-28686/com.example.john.bustrackclient I/Process: Sending signal. PID: 28686 SIG: 9 

从logcat的,我认为这可能是一个转换错误...但我不知道。

任何帮助表示赞赏!

+1

所以基本上你的应用程序抛出一个NullPointerException?我想我们可能已经在一两次之内回答了这个问题。 – markspace

+0

[什么是NullPointerException,以及如何解决它?]可能重复(http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it ) – csmckelvey

+0

我怀疑行151. – markspace

回答

0

入住这里返回什么:

latitude1 = updata.getString(Config.KEY_LATITUDE); 
longitude1 = updata.getString(Config.KEY_LONGITUDE); 

好像你的JSON响应包含无效的数据,或者你期待不同的东西,它实际上是。在这种情况下,似乎没有字段,其名称的值由Config.KEY_LATITUDE定义。

+0

当我打印它们时,我得到存储在服务器中的经度和纬度。只有当我添加计算部分时,该应用才会崩溃。 – John

+0

我建议运行调试器,在第131行放置断点latitude1 = updata.getString(Config.KEY_LATITUDE);检查你在'updata'中存储了什么,并检查它是否是你期望的。 – snikolau