我的目標是按照指示here來製作Google地圖的主要活動。未知分類:googleMap和2個以上
我粘貼了網站的代碼(MainActivity.java的代碼如下)並導入了所有必需的包(alt + enter)。儘管如此,仍然存在一些錯誤(現在,我專注於一個問題,在不久的將來可能會問及其他問題)。
線if (googleMap == null) {
得到一條紅線,並說三件事情:
Unknown class googleMap
(此錯誤是隻有在這個特定線)
Unexpected token
(當鼠標移到null
)&
Unexpected token
(當鼠標經過if
時)
如果有幫助,我正在使用android studio。感謝和代碼如下─
import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
public class MainActivity extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
public GoogleMap googleMap; // Might be null if Google Play services APK is not available.
if (googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMapAsync(this);
// check if map is created successfully or not
if (googleMap == null) {
Toast.makeText(getApplicationContext(),
"Could not create Maps", Toast.LENGTH_SHORT).show();
}
else {
// Changing map type
//TODO
}
}
public GpsLocation(Context mContext, TextView gpsStatusTextView) {
this.mContext = mContext;
this.gpsStatusTextView = gpsStatusTextView;
getLocation();
}
public Location getLocation() {
try {
locationManager = (LocationManager) mContext
.getSystemService(Context.LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
} else {
this.canGetLocation = true;
// First get location from Network Provider
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
gpsLocation = new GpsLocation(this, gpsStatusTextView);
if (gpsLocation.canGetLocation()){
double longitude = gpsLocation.getLongitude();
double latitude = gpsLocation.getLatitude();
}
}
很抱歉,如果這是一個總的的n00b問題
你好!我有一個API密鑰和工作,以及什麼是地圖活動? –
沒有更多未知的類googleMap?很高興聽到 –
aahhh,我想你誤會了我!我仍然得到這個錯誤,但我的API密鑰正在工作,我非常肯定這回合 –