0

私はAndroidには新しく、多くのチュートリアルで作業して、ユーザーにautozoomを使って現在地を取得させ、その場所を緯度と経度をテキスト表示に保存します。これまでのところ、私は場所を取得し、連続的なエラーやクラッシュを得ることができません。私はgpsStatusとnetworkConnectionStatusのステータスを表示できません。私はtargetSDKlevel 23と一緒に働いています。Google Mapsの現在の位置と受信した場所をTextViewに保存

アンサーが飾られています。

相続コード

public class Maps extends FragmentActivity implements OnMapReadyCallback, LocationListener { 

private GoogleMap mMap; 

public TextView tvRescueLocation; 


void error() 
{ 
    Toast.makeText(getApplicationContext(), "Something went wrong!", Toast.LENGTH_LONG).show(); 
} 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.maps); 
    // Obtain the SupportMapFragment and get notified when the map is ready to be used. 
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 
      .findFragmentById(R.id.map); 
    mapFragment.getMapAsync(this); 

    tvRescueLocation = (TextView) findViewById(R.id.tvRescueLocation); 

} 

@Override 
public void onMapReady(GoogleMap googleMap) { 
    mMap = googleMap; 


    LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE); 
    if(!lm.isProviderEnabled(LocationManager.GPS_PROVIDER) || 
      !lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) { 

     AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     builder.setTitle("Location Services Not Active"); 
     builder.setMessage("Please enable Location Services and GPS"); 
     builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialogInterface, int i) { 

       Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
       startActivity(intent); 
      } 
     }); 
     Dialog alertDialog = builder.create(); 
     alertDialog.setCanceledOnTouchOutside(false); 
     alertDialog.show(); 
    } 

    setMap(); 
} 

public void setMap() { 
    try { 
     if (ActivityCompat.checkSelfPermission 
       (this, Manifest.permission.ACCESS_FINE_LOCATION) 
       == PackageManager.PERMISSION_GRANTED 
       && 
       ActivityCompat.checkSelfPermission 
         (this, Manifest.permission.ACCESS_COARSE_LOCATION) 
         == PackageManager.PERMISSION_GRANTED) { 

      mMap.setMyLocationEnabled(true); 

      //Set map type to TERRAIN 
      mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN); 

      //Remapping the position of Location button 
      mMap.setPadding(0, 800, 0, 0); 

      //Enable UI controls of Map 
      mMap.getUiSettings().setZoomControlsEnabled(true); 
      mMap.getUiSettings().setCompassEnabled(true); 

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

      String provider = locationManager.getBestProvider(criteria,true); 
      Location newLoc = locationManager.getLastKnownLocation(provider); 

      location(newLoc); 
     } 
     else 
     { 
      finish(); 
     } 
    } 
    catch (Exception e) 
    { 
     error(); 
    } 
} 


@Override 
public void onLocationChanged(Location location) { 
} 

@Override 
public void onStatusChanged(String provider, int status, Bundle extras) { 

} 

@Override 
public void onProviderEnabled(String provider) { 

} 

@Override 
public void onProviderDisabled(String provider) { 

} 

public void location(Location location) { 
    final double latitude = location.getLatitude(); 
    final double longitude = location.getLongitude(); 
    LatLng latLng = new LatLng(latitude, longitude); 

    mMap.addMarker(new MarkerOptions() 
      .position(latLng) 
      .draggable(true) 
      .icon(BitmapDescriptorFactory 
        .fromResource(R.drawable.ic_location_flag))) 
      .setSnippet("Rescue Location - CLICK HERE"); 


    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom 
      (latLng, 17)); 

    //Adding Custom Circle for Rescue Attention 
    CircleOptions circleOptions = new CircleOptions() 
      .center(latLng) 
      .radius(40) 
      .strokeWidth(3) 
      .strokeColor(Color.RED); 
    mMap.addCircle(circleOptions); 

} 

}

LogCat:

04-01 15:40:29.334 22356-22675/? 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()); 
04-01 15:40:29.490 22356-22356/? I/Choreographer: Skipped 154 frames! The  application may be doing too much work on its main thread. 
04-01 15:40:29.709 22655-22659/? D/dalvikvm: GC_CONCURRENT freed 732K, 22%  free 4617K/5912K, paused 5ms+10ms, total 43ms 
04-01 15:40:29.709 22655-22655/? D/dalvikvm: WAIT_FOR_CONCURRENT_GC blocked 38ms 
04-01 15:40:29.724 22356-22356/? D/AndroidRuntime: Shutting down VM 
04-01 15:40:29.724 22356-22356/? W/dalvikvm: threadid=1: thread exiting with  uncaught exception (group=0x41701ba8) 
04-01 15:40:29.771 22356-22356/? E/AndroidRuntime: FATAL EXCEPTION: main 
               Process: com.rescue,  PID: 22356 
                 java.lang.NullPointerException 
                at  com.rescue.Maps.location(Maps.java:159) 
                at  com.rescue.Maps.setMap(Maps.java:125) 
                at  com.rescue.Maps.onMapReady(Maps.java:95) 
                at  com.google.android.gms.maps.SupportMapFragment$zza$1.zza(Unknown Source) 
                at  com.google.android.gms.maps.internal.zzo$zza.onTransact(Unknown Source) 
                at  android.os.Binder.transact(Binder.java:361) 
                at  com.google.android.gms.maps.internal.v$a$a.a(:com.google.android.gms.alldynamite:82) 
                at  maps.ei.bu$6.run(Unknown Source) 
                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:5059) 
                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-01 15:40:29.795 435-707/? W/ActivityManager: Force finishing activity  com.rescue/.Maps 
+0

チェック 'logcat'出力を! –

+0

私はlogcatを通過するだけですが、そのようなエラーは表示されません。一度私は場所を取得することができましたが、私はGPSとネットワークから私は場所が例外エラーを取得していないその時間から私はテストオフ私はオフ。 –

+0

あなたがテストしているデバイス –

答えて

0

使用LocationManager

LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
double longitude = location.getLongitude(); 
double latitude = location.getLatitude(); 

//Now set the value of Double to String 
String Lat = String.valueOf(latitude); 
String Lon = String.valueOf(longitude); 

//Set value to textview 

YourTextView1.setText(Lat); 
YourTextView2.setText(Lon); 
+0

私はこれを試しましたが、NullPointExceptionを取得しています。詳細については前のコメントを参照してください。 –

+0

マニフェストファイルに適切な権限を与えましたか? – Sam

+0

はい

0

getLastKnownLocat ion()はヌルを返す可能性があり、NullPointerExceptionが発生してアプリがクラッシュする可能性があります。

また、あなたは、Androidが提供する場所戦略のドキュメント上に行くと、そこに提供される方法を使用することができます:クラッシュの詳細については

// Acquire a reference to the system Location Manager 
LocationManager locationManager = (LocationManager)   this.getSystemService(Context.LOCATION_SERVICE); 
// Define a listener that responds to location updates 
LocationListener locationListener = new LocationListener() { 
public void onLocationChanged(Location location) { 
    double lat = location.getLatitude(); 
    double lng = location.getLongitude(); 
    // Display a marker on the GoogleMap 
} 
public void onStatusChanged(String provider, int status, Bundle extras) {} 
public void onProviderEnabled(String provider) {} 
public void onProviderDisabled(String provider) {} 
}; 
// Register the listener with the Location Manager to receive location updates 
// NOTE : PLEASE ADJUST THE FREQUENCY OF UPDATES PROPERLY 
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); 
+0

setMapその – Sam

+0

のあなたのlocationmanagerコードは、ロケーションマネージャからsetMap()メソッドからあなたのコードを削除します。upto public void location(....) – Sam

+0

これを試しましたが、正しく動作しません。マップを開始しますが、 (オートズームなし、マーカーなし)。 –

関連する問題