因此,我正在關注使用Google Maps V2構建移動應用程序的Lynda.com教程,並且我正在配合您創建地圖的一部分應用程序正在尋找位置編程,而不是依靠setMyLocationEnabled(true)
方法。所以,我的代碼,每次我去運行它崩潰的應用程序,並給我下面的錯誤在logcat的時候跟着一起:構造函數LocationClient(MainActivity,MainActivity,MainActivity)未定義
05-15 12:18:35.139: E/AndroidRuntime(22270): FATAL EXCEPTION: main
05-15 12:18:35.139: E/AndroidRuntime(22270): Process: com.example.gmapsapp, PID: 22270
05-15 12:18:35.139: E/AndroidRuntime(22270): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gmapsapp/com.example.gmapsapp.MainActivity}: java.lang.ClassCastException: com.example.gmapsapp.MainActivity cannot be cast to com.google.android.gms.common.GooglePlayServicesClient$ConnectionCallbacks
05-15 12:18:35.139: E/AndroidRuntime(22270): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
05-15 12:18:35.139: E/AndroidRuntime(22270): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
05-15 12:18:35.139: E/AndroidRuntime(22270): at android.app.ActivityThread.access$900(ActivityThread.java:161)
05-15 12:18:35.139: E/AndroidRuntime(22270): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
05-15 12:18:35.139: E/AndroidRuntime(22270): at android.os.Handler.dispatchMessage(Handler.java:102)
05-15 12:18:35.139: E/AndroidRuntime(22270): at android.os.Looper.loop(Looper.java:157)
05-15 12:18:35.139: E/AndroidRuntime(22270): at android.app.ActivityThread.main(ActivityThread.java:5356)
05-15 12:18:35.139: E/AndroidRuntime(22270): at java.lang.reflect.Method.invokeNative(Native Method)
05-15 12:18:35.139: E/AndroidRuntime(22270): at java.lang.reflect.Method.invoke(Method.java:515)
05-15 12:18:35.139: E/AndroidRuntime(22270): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
05-15 12:18:35.139: E/AndroidRuntime(22270): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
05-15 12:18:35.139: E/AndroidRuntime(22270): at dalvik.system.NativeStart.main(Native Method)
05-15 12:18:35.139: E/AndroidRuntime(22270): Caused by: java.lang.ClassCastException: com.example.gmapsapp.MainActivity cannot be cast to com.google.android.gms.common.GooglePlayServicesClient$ConnectionCallbacks
05-15 12:18:35.139: E/AndroidRuntime(22270): at com.example.gmapsapp.MainActivity.onCreate(MainActivity.java:60)
05-15 12:18:35.139: E/AndroidRuntime(22270): at android.app.Activity.performCreate(Activity.java:5426)
05-15 12:18:35.139: E/AndroidRuntime(22270): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
05-15 12:18:35.139: E/AndroidRuntime(22270): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
05-15 12:18:35.139: E/AndroidRuntime(22270): ... 11 more
下面是我MainActivity.java的代碼,讓我知道如果你需要的任何其他文件,看看是否可以用問題幫助的感謝:
package com.example.gmapsapp;
import java.io.IOException;
import java.util.List;
import android.app.Dialog;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationClient;
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.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
public class MainActivity extends FragmentActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener{
private static final int GPS_ERRORDIALOG_REQUEST = 9001;
@SuppressWarnings("unused")
private static final int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9002;
GoogleMap mMap;
@SuppressWarnings("unused")
private static final double SEATTLE_LAT = 47.60621,
SEATTLE_LNG =-122.33207,
SYDNEY_LAT = -33.867487,
SYDNEY_LNG = 151.20699,
NEWYORK_LAT = 40.714353,
NEWYORK_LNG = -74.005973;
private static final float DEFAULTZOOM = 15;
@SuppressWarnings("unused")
private static final String LOGTAG = "Maps";
LocationClient mLocationClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (servicesOK()) {
setContentView(R.layout.activity_map);
if (initMap()) {
// mMap.setMyLocationEnabled(true);
mLocationClient = new LocationClient(this, (ConnectionCallbacks) this, this);
mLocationClient.connect();
}
else {
Toast.makeText(this, "Map not available!", Toast.LENGTH_SHORT).show();
}
}
else {
setContentView(R.layout.activity_main);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public boolean servicesOK() {
int isAvailable = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (isAvailable == ConnectionResult.SUCCESS) {
return true;
}
else if (GooglePlayServicesUtil.isUserRecoverableError(isAvailable)) {
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(isAvailable, this, GPS_ERRORDIALOG_REQUEST);
dialog.show();
}
else {
Toast.makeText(this, "Can't connect to Google Play services", Toast.LENGTH_SHORT).show();
}
return false;
}
private boolean initMap() {
if (mMap == null) {
SupportMapFragment mapFrag =
(SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mMap = mapFrag.getMap();
}
return (mMap != null);
}
@SuppressWarnings("unused")
private void gotoLocation(double lat, double lng) {
LatLng ll = new LatLng(lat, lng);
CameraUpdate update = CameraUpdateFactory.newLatLng(ll);
mMap.moveCamera(update);
}
private void gotoLocation(double lat, double lng,
float zoom) {
LatLng ll = new LatLng(lat, lng);
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(ll, zoom);
mMap.moveCamera(update);
}
public void geoLocate(View v) throws IOException {
hideSoftKeyboard(v);
EditText et = (EditText) findViewById(R.id.editText1);
String location = et.getText().toString();
Geocoder gc = new Geocoder(this);
List<Address> list = gc.getFromLocationName(location, 1);
Address add = list.get(0);
String locality = add.getLocality();
Toast.makeText(this, locality, Toast.LENGTH_LONG).show();
double lat = add.getLatitude();
double lng = add.getLongitude();
gotoLocation(lat, lng, DEFAULTZOOM);
}
private void hideSoftKeyboard(View v) {
InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.mapTypeNone:
mMap.setMapType(GoogleMap.MAP_TYPE_NONE);
break;
case R.id.mapTypeNormal:
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
break;
case R.id.mapTypeSatellite:
mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
break;
case R.id.mapTypeTerrain:
mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
break;
case R.id.mapTypeHybrid:
mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
break;
case R.id.gotoCurrentLocation:
gotoCurrentLocation();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onStop() {
super.onStop();
MapStateManager mgr = new MapStateManager(this);
mgr.saveMapState(mMap);
}
@Override
protected void onResume() {
super.onResume();
MapStateManager mgr = new MapStateManager(this);
CameraPosition position = mgr.getSavedCameraPosition();
if (position != null) {
CameraUpdate update = CameraUpdateFactory.newCameraPosition(position);
mMap.moveCamera(update);
// This is part of the answer to the code challenge
mMap.setMapType(mgr.getSavedMapType());
}
}
protected void gotoCurrentLocation() {
}
@Override
public void onConnectionFailed(ConnectionResult arg0) {
// TODO Auto-generated method stub
}
@Override
public void onConnected(Bundle arg0) {
Toast.makeText(this, "Connected to location service", Toast.LENGTH_SHORT).show();
}
@Override
public void onConnectionSuspended(int arg0) {
// TODO Auto-generated method stub
}
}
構造函數的參數是錯誤的。閱讀http://developer.android.com/reference/com/google/android/gms/location/LocationClient.html#LocationClient(android.content.Context,com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks,com .google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener) – Raghunandan
謝謝我會給你一個機會! :) – user2196650