2016-06-12 1 views
-1

私はAndroid開発者にとっては少し新しく、ボタンをクリックした後、最後の既知の場所を取得してTextViewに表示することで、Googleの位置情報サービスをテストしたかったのです。どのように私はこのコードを変更する必要がありますので、それが動作するように教えてもらえますか?事前のおかげで:私は何が不足している/このコードを動作させるために修正する必要がありますか?

public class MainActivity extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener{ 

GoogleApiClient googleApiClient; 
String latitude, longitude; 
Button gpsButton; 
TextView displayGPS; 
AlertDialog.Builder dB; 
AlertDialog errorDialog; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    gpsButton.findViewById(R.id.gps_button); 
    displayGPS.findViewById(R.id.display_gps); 

    //create new google api client 
    googleApiClient = new GoogleApiClient.Builder(this).enableAutoManage(this, this) 
      .addApi(LocationServices.API).build(); 
    int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION); 
    if (permissionCheck == PackageManager.PERMISSION_GRANTED) { 
     Location lastLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient); 

     if (lastLocation != null) { 
      latitude = String.valueOf((lastLocation.getLatitude())); 
      longitude = String.valueOf((lastLocation.getLongitude())); 
      try { 
       gpsButton.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         Resources res = getResources(); 
         String gps = String.format(res.getString(R.string.lat_and_long), latitude, longitude); 
         displayGPS.setText(gps); 
        } 
       }); 
      }catch (Exception e){ 
       displayGPS.setText(R.string.location_unavailable); 
      } 
     } 
    } 
} 


@Override 
public void onConnectionFailed(ConnectionResult cr){ 
    dB = new AlertDialog.Builder(this).setTitle("Connection Failed") 
      .setMessage("Connection has failed.").setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        finish(); 
       } 
      }); 
    errorDialog = dB.create(); 
    errorDialog.show(); 
} 
} 

ここでは、のstrings.xmlファイルである:すべての

<resources> 
    <string name="app_name">Location Test</string> 
    <string name="find_gps">Find Your Last Location</string> 
    <string name="ok">OK</string> 
    <string name="lat_and_long">Latitude: %1s &amp; Longitude: %2$s</string> 
    <string name="location_unavailable">Location unavailable.</string> 
</resources> 
+0

まず、%1sを文字列リソースの%1 $ sに置き換えてください。それ以外の場合は、エラーメッセージがスローされますか? –

+0

は、ステップガイダンスのためのこの答えを見ているhttp://stackoverflow.com/a/38397092/5955362 –

+0

ありがとう@JaydeepPatel、コードは助けて、私のコードを変更して動作するように助けてくれました – Jaleel

答えて

0

まずlastLocationがnullであるかどうかをチェックできます。

if(lastLocation == null) { 
    Log.d("TEST", "TEST"); //Prints out to LogCat 
} 

it's場合したがって、あなたは現在の場所を要求する必要があります

LocationServices.FusedLocationApi.requestLocationUpdates(
     mGoogleApiClient, mLocationRequest, this); 

developer.android.com
最後に既知の場所がないためです。

アプリケーションがクラッシュした場合は、displayGPS.setText(gps);はUIスレッドから呼び出されません。 runOnUiThreadを使用し、そこにテキストを設定します。

編集:
UIスレッド上setOnClickListener実行中に私の悪い、すべてのもの。申し訳ありません。

+0

大丈夫、ありがとう、コードを修正しました。 – Jaleel