我在我的应用程序下面发布了我的代码显示没有错误在调试菜单中,当我运行它时logcat显示我在下面发布的一个错误。当我尝试在手机上运行应用程序时,它会关闭并停止响应,我不知道为什么。我正在关注一个在线教程并完全按照它说的做了。请帮忙。
10-07 14:50:02.901: I/Process(11653): Sending signal. PID: 11653 SIG: 9
10-07 14:50:06.124: W/dalvikvm(11801): VFY: unable to resolve static field 1346 (MapAttrs) in Lcom/google/android/gms/R$styleable;
10-07 14:50:06.124: D/dalvikvm(11801): VFY: replacing opcode 0x62 at 0x000e
10-07 14:50:06.134: D/AndroidRuntime(11801): Shutting down VM
10-07 14:50:06.134: W/dalvikvm(11801): threadid=1: thread exiting with uncaught exception (group=0x4129bac8)
10-07 14:50:06.134: E/AndroidRuntime(11801): FATAL EXCEPTION: main
10-07 14:50:06.134: E/AndroidRuntime(11801): java.lang.NoClassDefFoundError: com.google.android.gms.R$styleable
10-07 14:50:06.134: E/AndroidRuntime(11801): at com.google.android.gms.maps.GoogleMapOptions.createFromAttributes(Unknown Source)
10-07 14:50:06.134: E/AndroidRuntime(11801): at com.google.android.gms.maps.SupportMapFragment.onInflate(Unknown Source)
10-07 14:50:06.134: E/AndroidRuntime(11801): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:284)
10-07 14:50:06.134: E/AndroidRuntime(11801): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:682)
10-07 14:50:06.134: E/AndroidRuntime(11801): at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
10-07 14:50:06.134: E/AndroidRuntime(11801): at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
10-07 14:50:06.134: E/AndroidRuntime(11801): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
10-07 14:50:06.134: E/AndroidRuntime(11801): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
10-07 14:50:06.134: E/AndroidRuntime(11801): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:327)
10-07 14:50:06.134: E/AndroidRuntime(11801): at android.app.Activity.setContentView(Activity.java:1928)
10-07 14:50:06.134: E/AndroidRuntime(11801): at com.example.gloc.MainActivity.onCreate(MainActivity.java:30)
10-07 14:50:06.134: E/AndroidRuntime(11801): at android.app.Activity.performCreate(Activity.java:5250)
10-07 14:50:06.134: E/AndroidRuntime(11801): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
10-07 14:50:06.134: E/AndroidRuntime(11801): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
10-07 14:50:06.134: E/AndroidRuntime(11801): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2297)
10-07 14:50:06.134: E/AndroidRuntime(11801): at android.app.ActivityThread.access$700(ActivityThread.java:152)
10-07 14:50:06.134: E/AndroidRuntime(11801): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
10-07 14:50:06.134: E/AndroidRuntime(11801): at android.os.Handler.dispatchMessage(Handler.java:99)
10-07 14:50:06.134: E/AndroidRuntime(11801): at android.os.Looper.loop(Looper.java:137)
10-07 14:50:06.134: E/AndroidRuntime(11801): at android.app.ActivityThread.main(ActivityThread.java:5328)
10-07 14:50:06.134: E/AndroidRuntime(11801): at java.lang.reflect.Method.invokeNative(Native Method)
10-07 14:50:06.134: E/AndroidRuntime(11801): at java.lang.reflect.Method.invoke(Method.java:511)
10-07 14:50:06.134: E/AndroidRuntime(11801): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
10-07 14:50:06.134: E/AndroidRuntime(11801): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
10-07 14:50:06.134: E/AndroidRuntime(11801): at dalvik.system.NativeStart.main(Native Method)
MainActivity.java
package com.example.gloc;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.widget.TextView;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
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.LatLng;
public class MainActivity extends FragmentActivity implements LocationListener {
GoogleMap googleMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Getting Google Play availability status
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());
// Showing status
if(status!=ConnectionResult.SUCCESS){ // Google Play Services are not available
int requestCode = 10;
//Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode);
//dialog.show();
}else { // Google Play Services are available
// Getting reference to the SupportMapFragment of activity_main.xml
SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
// Getting GoogleMap object from the fragment
googleMap = fm.getMap();
// Enabling MyLocation Layer of Google Map
googleMap.setMyLocationEnabled(true);
// Getting LocationManager object from System Service LOCATION_SERVICE
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
// Creating a criteria object to retrieve provider
Criteria criteria = new Criteria();
// Getting the name of the best provider
String provider = locationManager.getBestProvider(criteria, true);
// Getting Current Location
Location location = locationManager.getLastKnownLocation(provider);
if(location!=null){
onLocationChanged(location);
}
locationManager.requestLocationUpdates(provider, 20000, 0, this);
}
}
@Override
public void onLocationChanged(Location location) {
TextView tvLocation = (TextView) findViewById(R.id.tv_location);
// Getting latitude of the current location
double latitude = location.getLatitude();
// Getting longitude of the current location
double longitude = location.getLongitude();
// Creating a LatLng object for the current location
LatLng latLng = new LatLng(latitude, longitude);
// Showing the current location in Google Map
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
// Zoom in the Google Map
googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
// Setting latitude and longitude in the TextView tv_location
tvLocation.setText("Latitude:" + latitude + ", Longitude:"+ longitude );
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@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;
}
}
Layout
<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"
tools:context=".MainActivity" >
<TextView
android:id="@+id/tv_location"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/tv_location"
class="com.google.android.gms.maps.SupportMapFragment" />
</RelativeLayout>
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.gloc"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="12"
android:targetSdkVersion="18" />
<permission
android:name="in.wptrafficanalyzer.locationingooglemapv2.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="in.wptrafficanalyzer.locationingooglemapv2.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<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" >
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="IEARASEDTHSITOHIDEIT"/>
<activity
android:name="com.example.gloc.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>
</manifest>
答案 0 :(得分:0)
您无法更改onCreate中的地图选项,因为它尚未实例化。尝试将OnLocationChanged中的所有内容放在onResume()而不是onCreate()中的GoogleMap对象中。