0
我知道這聽起來很愚蠢的問題,許多人之前已經問過,但我現在感到沮喪,因爲我看過的每個答案(更多的時間比我不在乎),關於那些錯誤不能讓我的代碼正確。 我觀看了YouTube上的代碼並下載了here。R無法解析爲一個變量(谷歌地圖)
反正這裏是我的示例代碼
package com.example.googlemapsdemo;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
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.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MainActivity extends Activity {
private final LatLng LOCATION_PALEMBANG = new LatLng(-2.938201, 104.6892742);
private final LatLng LOCATION_JOHOR = new LatLng(1.5618762, 103.6365218);
private GoogleMap map;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); <-----this line (R cannot be resolved to a variable)
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap(); <-----this line (R cannot be resolved to a variable)
map.addMarker(new MarkerOptions().position(LOCATION_PALEMBANG).title("Find me here!"));
}
@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); <--- this line (same error)
return true;
}
public void onClick_City(View v) {
// CameraUpdate update = CameraUpdateFactory.newLatLng(LOCATION_PALEMBANG);
map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(LOCATION_PALEMBANG, 9);
map.animateCamera(update);
}
public void onClick_Burnaby(View v) {
map.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(LOCATION_PALEMBANG, 14);
map.animateCamera(update);
}
public void onClick_Surrey(View v) {
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(LOCATION_JOHOR, 16);
map.animateCamera(update);
}
}
,這是我的Android清單
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.googlemapsdemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="17" />
<permission
android:name="com.example.googlemapsdemo.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.example.googlemapsdemo.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<!-- The following two permissions are not required to use
Google Maps Android API v2, but are recommended. -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<uses-library
android:name="com.example.googlemapsdemo" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyCravI-Yxyh1702ZX9s4O5cL_lWmoGz9qo"/>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity
android:name="com.example.googlemapsdemo.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
和活動的主要代碼:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/btnJohor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:onClick="onClick_Johor"
android:text="Johor" />
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_below="@+id/btnSurrey"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<Button
android:id="@+id/btnPalembang"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:onClick="onClick_Palembang"
android:text="Palembang" />
<Button
android:id="@+id/btnCity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:onClick="onClick_City"
android:text="City" />
另外我有一個問題與我的價值-14,它說:「錯誤檢索項目的父母:找不到匹配給定名稱的資源'android:Theme.Holo.Light.DarkActionBar'。」 請有人能幫助我嗎?