为什么Google地图无法显示?
Why Google Maps can not be displayed?
我在 logcat 遇到过这样的问题:
01-27 18:27:12.403: E/AndroidRuntime(3938): FATAL EXCEPTION: main
01-27 18:27:12.403: E/AndroidRuntime(3938): java.lang.RuntimeException: Unable to start activity ComponentInfo{in.wptrafficanalyzer.locationmarkermysql/in.wptrafficanalyzer.locationmarkermysql.MainActivity}: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
01-27 18:27:12.403: E/AndroidRuntime(3938): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
01-27 18:27:12.403: E/AndroidRuntime(3938): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-27 18:27:12.403: E/AndroidRuntime(3938): at android.app.ActivityThread.access0(ActivityThread.java:141)
01-27 18:27:12.403: E/AndroidRuntime(3938): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-27 18:27:12.403: E/AndroidRuntime(3938): at android.os.Handler.dispatchMessage(Handler.java:99)
01-27 18:27:12.403: E/AndroidRuntime(3938): at android.os.Looper.loop(Looper.java:137)
01-27 18:27:12.403: E/AndroidRuntime(3938): at android.app.ActivityThread.main(ActivityThread.java:5041)
01-27 18:27:12.403: E/AndroidRuntime(3938): at java.lang.reflect.Method.invokeNative(Native Method)
01-27 18:27:12.403: E/AndroidRuntime(3938): at java.lang.reflect.Method.invoke(Method.java:511)
01-27 18:27:12.403: E/AndroidRuntime(3938): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-27 18:27:12.403: E/AndroidRuntime(3938): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-27 18:27:12.403: E/AndroidRuntime(3938): at dalvik.system.NativeStart.main(Native Method)
01-27 18:27:12.403: E/AndroidRuntime(3938): Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
01-27 18:27:12.403: E/AndroidRuntime(3938): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
01-27 18:27:12.403: E/AndroidRuntime(3938): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
01-27 18:27:12.403: E/AndroidRuntime(3938): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
01-27 18:27:12.403: E/AndroidRuntime(3938): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
01-27 18:27:12.403: E/AndroidRuntime(3938): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
01-27 18:27:12.403: E/AndroidRuntime(3938): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
01-27 18:27:12.403: E/AndroidRuntime(3938): at android.app.Activity.setContentView(Activity.java:1881)
01-27 18:27:12.403: E/AndroidRuntime(3938): at in.wptrafficanalyzer.locationmarkermysql.MainActivity.onCreate(MainActivity.java:35)
01-27 18:27:12.403: E/AndroidRuntime(3938): at android.app.Activity.performCreate(Activity.java:5104)
01-27 18:27:12.403: E/AndroidRuntime(3938): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
01-27 18:27:12.403: E/AndroidRuntime(3938): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
01-27 18:27:12.403: E/AndroidRuntime(3938): ... 11 more
01-27 18:27:12.403: E/AndroidRuntime(3938): Caused by: java.lang.SecurityException: The Maps API requires the additional following permissions to be set in the AndroidManifest.xml to ensure a correct behavior:
01-27 18:27:12.403: E/AndroidRuntime(3938): <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
01-27 18:27:12.403: E/AndroidRuntime(3938): at com.google.maps.api.android.lib6.c.ck.a(Unknown Source)
01-27 18:27:12.403: E/AndroidRuntime(3938): at com.google.maps.api.android.lib6.c.i.a(Unknown Source)
01-27 18:27:12.403: E/AndroidRuntime(3938): at com.google.maps.api.android.lib6.c.el.a(Unknown Source)
01-27 18:27:12.403: E/AndroidRuntime(3938): at com.google.maps.api.android.lib6.c.ab.a(Unknown Source)
01-27 18:27:12.403: E/AndroidRuntime(3938): at com.google.maps.api.android.lib6.c.aa.a(Unknown Source)
01-27 18:27:12.403: E/AndroidRuntime(3938): at com.google.android.gms.maps.internal.x.onTransact(SourceFile:107)
01-27 18:27:12.403: E/AndroidRuntime(3938): at android.os.Binder.transact(Binder.java:310)
01-27 18:27:12.403: E/AndroidRuntime(3938): at com.google.android.gms.maps.internal.IMapFragmentDelegate$a$a.onCreateView(Unknown Source)
01-27 18:27:12.403: E/AndroidRuntime(3938): at com.google.android.gms.maps.SupportMapFragment$a.onCreateView(Unknown Source)
01-27 18:27:12.403: E/AndroidRuntime(3938): at com.google.android.gms.dynamic.a.b(Unknown Source)
01-27 18:27:12.403: E/AndroidRuntime(3938): at com.google.android.gms.dynamic.a.a(Unknown Source)
01-27 18:27:12.403: E/AndroidRuntime(3938): at com.google.android.gms.dynamic.a.onCreateView(Unknown Source)
01-27 18:27:12.403: E/AndroidRuntime(3938): at com.google.android.gms.maps.SupportMapFragment.onCreateView(Unknown Source)
01-27 18:27:12.403: E/AndroidRuntime(3938): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
01-27 18:27:12.403: E/AndroidRuntime(3938): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:900)
01-27 18:27:12.403: E/AndroidRuntime(3938): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1082)
01-27 18:27:12.403: E/AndroidRuntime(3938): at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1184)
01-27 18:27:12.403: E/AndroidRuntime(3938): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:285)
01-27 18:27:12.403: E/AndroidRuntime(3938): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
我想通过使用 Google API 来显示 google 地图,但发生这种情况应用程序自行停止,这是我的 MainActivity
MainActivity.java
public class MainActivity extends FragmentActivity {
GoogleMap mGoogleMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Getting reference to SupportMapFragment
SupportMapFragment fragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
// Creating GoogleMap from SupportMapFragment
mGoogleMap = fragment.getMap();
// Enabling MyLocation button for the Google Map
mGoogleMap.setMyLocationEnabled(true);
// Setting OnClickEvent listener for the GoogleMap
mGoogleMap.setOnMapClickListener(new OnMapClickListener() {
@Override
public void onMapClick(LatLng latlng) {
addMarker(latlng);
sendToServer(latlng);
}
});
// Starting locations retrieve task
new RetrieveTask().execute();
}
// Adding marker on the GoogleMaps
private void addMarker(LatLng latlng) {
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(latlng);
markerOptions.title(latlng.latitude + "," + latlng.longitude);
mGoogleMap.addMarker(markerOptions);
}
// Invoking background thread to store the touched location in Remove MySQL server
private void sendToServer(LatLng latlng) {
new SaveTask().execute(latlng);
}
// Background thread to save the location in remove MySQL server
private class SaveTask extends AsyncTask<LatLng, Void, Void> {
@Override
protected Void doInBackground(LatLng... params) {
String lat = Double.toString(params[0].latitude);
String lng = Double.toString(params[0].longitude);
String strUrl = "http://192.168.191.1/location_marker_mysql/save.php";
URL url = null;
try {
url = new URL(strUrl);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(
connection.getOutputStream());
outputStreamWriter.write("lat=" + lat + "&lng="+lng);
outputStreamWriter.flush();
outputStreamWriter.close();
InputStream iStream = connection.getInputStream();
BufferedReader reader = new BufferedReader(new
InputStreamReader(iStream));
StringBuffer sb = new StringBuffer();
String line = "";
while( (line = reader.readLine()) != null){
sb.append(line);
}
reader.close();
iStream.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
// Background task to retrieve locations from remote mysql server
private class RetrieveTask extends AsyncTask<Void, Void, String>{
@Override
protected String doInBackground(Void... params) {
String strUrl = "http://192.168.191.1/location_marker_mysql/retrieve.php";
URL url = null;
StringBuffer sb = new StringBuffer();
try {
url = new URL(strUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream iStream = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(iStream));
String line = "";
while( (line = reader.readLine()) != null){
sb.append(line);
}
reader.close();
iStream.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
new ParserTask().execute(result);
}
}
// Background thread to parse the JSON data retrieved from MySQL server
private class ParserTask extends AsyncTask<String, Void, List<HashMap<String, String>>>{
@Override
protected List<HashMap<String,String>> doInBackground(String... params) {
MarkerJSONParser markerParser = new MarkerJSONParser();
JSONObject json = null;
try {
json = new JSONObject(params[0]);
} catch (JSONException e) {
e.printStackTrace();
}
List<HashMap<String, String>> markersList = markerParser.parse(json);
return markersList;
}
@Override
protected void onPostExecute(List<HashMap<String, String>> result) {
for(int i=0; i<result.size();i++){
HashMap<String, String> marker = result.get(i);
LatLng latlng = new LatLng(Double.parseDouble(marker.get("lat")), Double.parseDouble(marker.get("lng")));
addMarker(latlng);
}
}
}
@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;
}
}
我正在使用MySQL数据库存储当纬度经度作为标记将显示在地图中,但仍然不能因为有错误。我的清单有问题吗?这是我的清单
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="in.wptrafficanalyzer.locationmarkermysql"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<!-- Protect the map component of the application using application signature -->
<permission
android:name="in.wptrafficanalyzer.locationmarkermysql.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<!-- Allows to receive map -->
<uses-permission android:name="in.wptrafficanalyzer.locationmarkermysql.permission.MAPS_RECEIVE" />
<!-- Used by the Google Maps Android API V2 to download map tiles from Google Maps servers -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- Allows the Google Maps Android API V2 to cache map tile data in the device's external storage area -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- Allows the Google Maps Android API V2 to use WiFi or mobile cell data (or both) to determine the device's location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<!-- Allows the Google Maps Android API V2 to use the Global Positioning System (GPS)
to determine the device's location to within a very small area -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- Allows to contact Google Serves -->
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<!-- Google Maps Android API V2 requires OpenGL ES version 2 -->
<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" >
<activity
android:name="in.wptrafficanalyzer.locationmarkermysql.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>
<!-- Specifies the Android API Key, which is obtained from Google API Console -->
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyDDoOiE8El5PyA3HQn2uu1qA90vcm3htQo" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</application>
</manifest>
朋友们可以帮帮我,我是新手Android,求解释,谢谢
你的logcat说的很清楚
Caused by: java.lang.SecurityException: The Maps API requires the
additional following permissions to be set in the AndroidManifest.xml
to ensure a correct behavior: 01-27 18:27:12.403:
AndroidRuntime(3938): <uses-permission
android:name="android.permission.ACCESS_NETWORK_STATE"/> 01-27
18:27:12.403: E/AndroidRuntime(3938)
加入你的manifest.xml
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
此权限允许 API
检查 网络连接 状态以确定是否可以下载数据。
每次遇到错误时,请检查您的 logcat 并在该检查中由您的 logcat 引起: 原因:java.lang.SecurityException:地图 API 需要在 AndroidManifest.xml 中设置额外的以下权限以确保正确的行为:
01-27 18:27:12.403: E/AndroidRuntime(3938):
只需将 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
添加到您的清单文件中。
我在 logcat 遇到过这样的问题:
01-27 18:27:12.403: E/AndroidRuntime(3938): FATAL EXCEPTION: main
01-27 18:27:12.403: E/AndroidRuntime(3938): java.lang.RuntimeException: Unable to start activity ComponentInfo{in.wptrafficanalyzer.locationmarkermysql/in.wptrafficanalyzer.locationmarkermysql.MainActivity}: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
01-27 18:27:12.403: E/AndroidRuntime(3938): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
01-27 18:27:12.403: E/AndroidRuntime(3938): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-27 18:27:12.403: E/AndroidRuntime(3938): at android.app.ActivityThread.access0(ActivityThread.java:141)
01-27 18:27:12.403: E/AndroidRuntime(3938): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-27 18:27:12.403: E/AndroidRuntime(3938): at android.os.Handler.dispatchMessage(Handler.java:99)
01-27 18:27:12.403: E/AndroidRuntime(3938): at android.os.Looper.loop(Looper.java:137)
01-27 18:27:12.403: E/AndroidRuntime(3938): at android.app.ActivityThread.main(ActivityThread.java:5041)
01-27 18:27:12.403: E/AndroidRuntime(3938): at java.lang.reflect.Method.invokeNative(Native Method)
01-27 18:27:12.403: E/AndroidRuntime(3938): at java.lang.reflect.Method.invoke(Method.java:511)
01-27 18:27:12.403: E/AndroidRuntime(3938): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-27 18:27:12.403: E/AndroidRuntime(3938): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-27 18:27:12.403: E/AndroidRuntime(3938): at dalvik.system.NativeStart.main(Native Method)
01-27 18:27:12.403: E/AndroidRuntime(3938): Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
01-27 18:27:12.403: E/AndroidRuntime(3938): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
01-27 18:27:12.403: E/AndroidRuntime(3938): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
01-27 18:27:12.403: E/AndroidRuntime(3938): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
01-27 18:27:12.403: E/AndroidRuntime(3938): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
01-27 18:27:12.403: E/AndroidRuntime(3938): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
01-27 18:27:12.403: E/AndroidRuntime(3938): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
01-27 18:27:12.403: E/AndroidRuntime(3938): at android.app.Activity.setContentView(Activity.java:1881)
01-27 18:27:12.403: E/AndroidRuntime(3938): at in.wptrafficanalyzer.locationmarkermysql.MainActivity.onCreate(MainActivity.java:35)
01-27 18:27:12.403: E/AndroidRuntime(3938): at android.app.Activity.performCreate(Activity.java:5104)
01-27 18:27:12.403: E/AndroidRuntime(3938): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
01-27 18:27:12.403: E/AndroidRuntime(3938): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
01-27 18:27:12.403: E/AndroidRuntime(3938): ... 11 more
01-27 18:27:12.403: E/AndroidRuntime(3938): Caused by: java.lang.SecurityException: The Maps API requires the additional following permissions to be set in the AndroidManifest.xml to ensure a correct behavior:
01-27 18:27:12.403: E/AndroidRuntime(3938): <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
01-27 18:27:12.403: E/AndroidRuntime(3938): at com.google.maps.api.android.lib6.c.ck.a(Unknown Source)
01-27 18:27:12.403: E/AndroidRuntime(3938): at com.google.maps.api.android.lib6.c.i.a(Unknown Source)
01-27 18:27:12.403: E/AndroidRuntime(3938): at com.google.maps.api.android.lib6.c.el.a(Unknown Source)
01-27 18:27:12.403: E/AndroidRuntime(3938): at com.google.maps.api.android.lib6.c.ab.a(Unknown Source)
01-27 18:27:12.403: E/AndroidRuntime(3938): at com.google.maps.api.android.lib6.c.aa.a(Unknown Source)
01-27 18:27:12.403: E/AndroidRuntime(3938): at com.google.android.gms.maps.internal.x.onTransact(SourceFile:107)
01-27 18:27:12.403: E/AndroidRuntime(3938): at android.os.Binder.transact(Binder.java:310)
01-27 18:27:12.403: E/AndroidRuntime(3938): at com.google.android.gms.maps.internal.IMapFragmentDelegate$a$a.onCreateView(Unknown Source)
01-27 18:27:12.403: E/AndroidRuntime(3938): at com.google.android.gms.maps.SupportMapFragment$a.onCreateView(Unknown Source)
01-27 18:27:12.403: E/AndroidRuntime(3938): at com.google.android.gms.dynamic.a.b(Unknown Source)
01-27 18:27:12.403: E/AndroidRuntime(3938): at com.google.android.gms.dynamic.a.a(Unknown Source)
01-27 18:27:12.403: E/AndroidRuntime(3938): at com.google.android.gms.dynamic.a.onCreateView(Unknown Source)
01-27 18:27:12.403: E/AndroidRuntime(3938): at com.google.android.gms.maps.SupportMapFragment.onCreateView(Unknown Source)
01-27 18:27:12.403: E/AndroidRuntime(3938): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
01-27 18:27:12.403: E/AndroidRuntime(3938): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:900)
01-27 18:27:12.403: E/AndroidRuntime(3938): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1082)
01-27 18:27:12.403: E/AndroidRuntime(3938): at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1184)
01-27 18:27:12.403: E/AndroidRuntime(3938): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:285)
01-27 18:27:12.403: E/AndroidRuntime(3938): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
我想通过使用 Google API 来显示 google 地图,但发生这种情况应用程序自行停止,这是我的 MainActivity
MainActivity.java
public class MainActivity extends FragmentActivity {
GoogleMap mGoogleMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Getting reference to SupportMapFragment
SupportMapFragment fragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
// Creating GoogleMap from SupportMapFragment
mGoogleMap = fragment.getMap();
// Enabling MyLocation button for the Google Map
mGoogleMap.setMyLocationEnabled(true);
// Setting OnClickEvent listener for the GoogleMap
mGoogleMap.setOnMapClickListener(new OnMapClickListener() {
@Override
public void onMapClick(LatLng latlng) {
addMarker(latlng);
sendToServer(latlng);
}
});
// Starting locations retrieve task
new RetrieveTask().execute();
}
// Adding marker on the GoogleMaps
private void addMarker(LatLng latlng) {
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(latlng);
markerOptions.title(latlng.latitude + "," + latlng.longitude);
mGoogleMap.addMarker(markerOptions);
}
// Invoking background thread to store the touched location in Remove MySQL server
private void sendToServer(LatLng latlng) {
new SaveTask().execute(latlng);
}
// Background thread to save the location in remove MySQL server
private class SaveTask extends AsyncTask<LatLng, Void, Void> {
@Override
protected Void doInBackground(LatLng... params) {
String lat = Double.toString(params[0].latitude);
String lng = Double.toString(params[0].longitude);
String strUrl = "http://192.168.191.1/location_marker_mysql/save.php";
URL url = null;
try {
url = new URL(strUrl);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(
connection.getOutputStream());
outputStreamWriter.write("lat=" + lat + "&lng="+lng);
outputStreamWriter.flush();
outputStreamWriter.close();
InputStream iStream = connection.getInputStream();
BufferedReader reader = new BufferedReader(new
InputStreamReader(iStream));
StringBuffer sb = new StringBuffer();
String line = "";
while( (line = reader.readLine()) != null){
sb.append(line);
}
reader.close();
iStream.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
// Background task to retrieve locations from remote mysql server
private class RetrieveTask extends AsyncTask<Void, Void, String>{
@Override
protected String doInBackground(Void... params) {
String strUrl = "http://192.168.191.1/location_marker_mysql/retrieve.php";
URL url = null;
StringBuffer sb = new StringBuffer();
try {
url = new URL(strUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream iStream = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(iStream));
String line = "";
while( (line = reader.readLine()) != null){
sb.append(line);
}
reader.close();
iStream.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
new ParserTask().execute(result);
}
}
// Background thread to parse the JSON data retrieved from MySQL server
private class ParserTask extends AsyncTask<String, Void, List<HashMap<String, String>>>{
@Override
protected List<HashMap<String,String>> doInBackground(String... params) {
MarkerJSONParser markerParser = new MarkerJSONParser();
JSONObject json = null;
try {
json = new JSONObject(params[0]);
} catch (JSONException e) {
e.printStackTrace();
}
List<HashMap<String, String>> markersList = markerParser.parse(json);
return markersList;
}
@Override
protected void onPostExecute(List<HashMap<String, String>> result) {
for(int i=0; i<result.size();i++){
HashMap<String, String> marker = result.get(i);
LatLng latlng = new LatLng(Double.parseDouble(marker.get("lat")), Double.parseDouble(marker.get("lng")));
addMarker(latlng);
}
}
}
@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;
}
}
我正在使用MySQL数据库存储当纬度经度作为标记将显示在地图中,但仍然不能因为有错误。我的清单有问题吗?这是我的清单
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="in.wptrafficanalyzer.locationmarkermysql"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<!-- Protect the map component of the application using application signature -->
<permission
android:name="in.wptrafficanalyzer.locationmarkermysql.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<!-- Allows to receive map -->
<uses-permission android:name="in.wptrafficanalyzer.locationmarkermysql.permission.MAPS_RECEIVE" />
<!-- Used by the Google Maps Android API V2 to download map tiles from Google Maps servers -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- Allows the Google Maps Android API V2 to cache map tile data in the device's external storage area -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- Allows the Google Maps Android API V2 to use WiFi or mobile cell data (or both) to determine the device's location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<!-- Allows the Google Maps Android API V2 to use the Global Positioning System (GPS)
to determine the device's location to within a very small area -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- Allows to contact Google Serves -->
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<!-- Google Maps Android API V2 requires OpenGL ES version 2 -->
<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" >
<activity
android:name="in.wptrafficanalyzer.locationmarkermysql.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>
<!-- Specifies the Android API Key, which is obtained from Google API Console -->
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyDDoOiE8El5PyA3HQn2uu1qA90vcm3htQo" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</application>
</manifest>
朋友们可以帮帮我,我是新手Android,求解释,谢谢
你的logcat说的很清楚
Caused by: java.lang.SecurityException: The Maps API requires the
additional following permissions to be set in the AndroidManifest.xml
to ensure a correct behavior: 01-27 18:27:12.403:
AndroidRuntime(3938): <uses-permission
android:name="android.permission.ACCESS_NETWORK_STATE"/> 01-27
18:27:12.403: E/AndroidRuntime(3938)
加入你的manifest.xml
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
此权限允许 API
检查 网络连接 状态以确定是否可以下载数据。
每次遇到错误时,请检查您的 logcat 并在该检查中由您的 logcat 引起: 原因:java.lang.SecurityException:地图 API 需要在 AndroidManifest.xml 中设置额外的以下权限以确保正确的行为: 01-27 18:27:12.403: E/AndroidRuntime(3938):
只需将 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
添加到您的清单文件中。