0

我正在尝试在模拟器中执行 MAP-API 程序

  • 我收到日志错误!
  • r 有任何逻辑错误

如何解决这些错误

MainActivity.java

public class MainActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

}

显现

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.learnmap"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="12"
        android:targetSdkVersion="16" />

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <permission
        android:name="net.learn2develop.googlemaps.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-permission android:name="net.learn2develop.googlemaps.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" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="net.learn2develop.googlemaps.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>

        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="---key---" />
    </application>

</manifest>

日志

10-18 12:33:13.299: D/AndroidRuntime(5516): Shutting down VM
10-18 12:33:13.348: W/dalvikvm(5516): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
10-18 12:33:13.389: E/AndroidRuntime(5516): FATAL EXCEPTION: main
10-18 12:33:13.389: E/AndroidRuntime(5516): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.learnmap/net.learn2develop.googlemaps.MainActivity}: 
4

1 回答 1

0

尝试扩展 Activity 而不是 fragmentActivity

.java 文件:

 public class MainActivity extends Activity implements LocationListener{
     LocationManager locationManager;
    private GoogleMap googleMap;
    double lat1=17.385044;
    double long=78.486671;
    private String provider;
 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
  LocationManager  service=(LocationManager)getSystemService(LOCATION_SERVICE);
boolean enableGPS= service.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean enableWifi=service.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
 // Check if enabled and if not send user to the GSP settings
   // Better solution would be to display a dialog and suggesting to 
  // go to the settings
if(!enableGPS){
  Toast.makeText(getApplicationContext(), "Gps signal not found",1).show();
    Intent intent=new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
    startActivity(intent);
}
locationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
 Criteria criteria = new Criteria();
 provider = locationManager.getBestProvider(criteria, false);
 Location location = locationManager.getLastKnownLocation(provider);

 // Initialize the location fields
 if (location != null) {
     Toast.makeText(this, "Selected Provider " + provider, 0).show();
     onLocationChanged(location);
 } else {

     //do something
 }


try {
    //loading map
    initilizeMap();
} catch (Exception e) {
    // TODO: handle exception
e.printStackTrace();
}
}
    private void initilizeMap(){
if(googleMap==null){
googleMap=(MapFragment)getSupportFragmentManager().findFragmentById(R.id.map)).getMap();    
    //check if map is created succesfully or not
    if(googleMap==null){
Toast.makeText(getApplicationContext(),"sorry unable to create map",1).show();
    }
}

MarkerOptions markerOptions=new MarkerOptions().position(new LatLng(lat1, long)); markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher)); CameraPosition cameraPosition=new CameraPosition.Builder().target(new LatLng(17.385044, 78.486671)).zoom(12).build(); googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); googleMap.setMyLocationEnabled(true); googleMap.setOnInfoWindowClickListener(null); }

@Override
 protected void onResume(){
super.onResume();
initilizeMap();
   }

    /* Remove the locationlistener updates when Activity is paused */
 @Override
protected void onPause() {
super.onPause();
locationManager.removeUpdates(this);
}

public void onLocationChanged(Location location) {
double lat =  location.getLatitude();
double lng = location.getLongitude();
Toast.makeText(this, "Location " + lat+","+lng,
        Toast.LENGTH_LONG).show();
LatLng coordinate = new LatLng(lat, lng);
Toast.makeText(this, "Location " + coordinate.latitude+","+coordinate.longitude,
        Toast.LENGTH_LONG).show();
Marker startPerc = googleMap.addMarker(new MarkerOptions()
.position(coordinate)
.title("Start")
.snippet("Inizio del percorso")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher)));
}


 public void onProviderDisabled(String provider) {
Toast.makeText(this, "Enabled new provider " + provider,0).show();

}


 public void onProviderEnabled(String provider) {
 Toast.makeText(this, "Disabled provider " + provider,0).show();

}

public void onStatusChanged(String provider, int status, Bundle extras) { // TODO 自动生成的方法存根

}

于 2013-10-18T08:35:21.480 回答