我有一个问题,我几天以来一直在处理,我尝试了很多,但我没有找到解决方案。
任何帮助将非常感激!
主要活动
package de.ok.locmarker;
import android.app.Dialog;
import android.content.ContentValues;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.view.Menu;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMapClickListener;
import com.google.android.gms.maps.GoogleMap.OnMapLongClickListener;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MainActivity extends FragmentActivity implements LoaderCallbacks<Cursor> {
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);
// Invoke LoaderCallbacks to retrieve and draw already saved locations in map
getSupportLoaderManager().initLoader(0, null, this);
}
googleMap.setOnMapClickListener(new OnMapClickListener() {
@Override
public void onMapClick(LatLng point) {
// Drawing marker on the map
drawMarker(point);
// Creating an instance of ContentValues
ContentValues contentValues = new ContentValues();
// Setting latitude in ContentValues
contentValues.put(LocationsDB.FIELD_LAT, point.latitude );
// Setting longitude in ContentValues
contentValues.put(LocationsDB.FIELD_LNG, point.longitude);
// Setting zoom in ContentValues
contentValues.put(LocationsDB.FIELD_ZOOM, googleMap.getCameraPosition().zoom);
// Creating an instance of LocationInsertTask
LocationInsertTask insertTask = new LocationInsertTask();
// Storing the latitude, longitude and zoom level to SQLite database
insertTask.execute(contentValues);
Toast.makeText(getBaseContext(), "Marker is added to the Map", Toast.LENGTH_SHORT).show();
}
});
googleMap.setOnMapLongClickListener(new OnMapLongClickListener() {
@Override
public void onMapLongClick(LatLng point) {
// Removing all markers from the Google Map
googleMap.clear();
// Creating an instance of LocationDeleteTask
LocationDeleteTask deleteTask = new LocationDeleteTask();
// Deleting all the rows from SQLite database table
deleteTask.execute();
Toast.makeText(getBaseContext(), "All markers are removed", Toast.LENGTH_LONG).show();
}
});
}
private void drawMarker(LatLng point){
// Creating an instance of MarkerOptions
MarkerOptions markerOptions = new MarkerOptions();
// Setting latitude and longitude for the marker
markerOptions.position(point);
// Adding marker on the Google Map
googleMap.addMarker(markerOptions);
}
private class LocationInsertTask extends AsyncTask<ContentValues, Void, Void>{
@Override
protected Void doInBackground(ContentValues... contentValues) {
/** Setting up values to insert the clicked location into SQLite database */
getContentResolver().insert(LocationsContentProvider.CONTENT_URI, contentValues[0]);
return null;
}
}
private class LocationDeleteTask extends AsyncTask<Void, Void, Void>{
@Override
protected Void doInBackground(Void... params) {
/** Deleting all the locations stored in SQLite database */
getContentResolver().delete(LocationsContentProvider.CONTENT_URI, null, null);
return null;
}
}
@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;
}
@Override
public Loader<Cursor> onCreateLoader(int arg0,
Bundle arg1) {
// Uri to the content provider LocationsContentProvider
Uri uri = LocationsContentProvider.CONTENT_URI;
// Fetches all the rows from locations table
return new CursorLoader(this, uri, null, null, null, null);
}
@Override
public void onLoadFinished(Loader<Cursor> arg0,
Cursor arg1) {
int locationCount = 0;
double lat=0;
double lng=0;
float zoom=0;
// Number of locations available in the SQLite database table
locationCount = arg1.getCount();
// Move the current record pointer to the first row of the table
arg1.moveToFirst();
for(int i=0;i<locationCount;i++){
// Get the latitude
lat = arg1.getDouble(arg1.getColumnIndex(LocationsDB.FIELD_LAT));
// Get the longitude
lng = arg1.getDouble(arg1.getColumnIndex(LocationsDB.FIELD_LNG));
// Get the zoom level
zoom = arg1.getFloat(arg1.getColumnIndex(LocationsDB.FIELD_ZOOM));
// Creating an instance of LatLng to plot the location in Google Maps
LatLng location = new LatLng(lat, lng);
// Drawing the marker in the Google Maps
drawMarker(location);
// Traverse the pointer to the next row
arg1.moveToNext();
}
if(locationCount>0){
// Moving CameraPosition to last clicked position
googleMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(lat,lng)));
// Setting the zoom level in the map on last position is clicked
googleMap.animateCamera(CameraUpdateFactory.zoomTo(zoom));
}
}
@Override
public void onLoaderReset(Loader<Cursor> arg0) {
// TODO Auto-generated method stub
}
}
清单.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.ok.locmarker"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="17" />
<permission
android:name="de.ok.locmarker.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="de.ok.locmarker.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" >
<activity
android:name=".LocMarker"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider
android:name="LocationsContentProvider"
android:authorities="de.ok.locmarker.locations"
android:exported="false" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="xxx" />
</application>
</manifest>
活动主
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
日志猫
06-07 01:28:37.365: E/AndroidRuntime(5664): FATAL EXCEPTION: main
06-07 01:28:37.365: E/AndroidRuntime(5664):
java.lang.RuntimeException: Unable to instantiate activity
ComponentInfo{de.ok.locmarker/de.ok.locmarker.LocMarker}:
java.lang.ClassNotFoundException: de.ok.locmarker.LocMarker 06-07
01:28:37.365: E/AndroidRuntime(5664): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2016)
06-07 01:28:37.365: E/AndroidRuntime(5664): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2117)
06-07 01:28:37.365: E/AndroidRuntime(5664): at
android.app.ActivityThread.access$700(ActivityThread.java:134) 06-07
01:28:37.365: E/AndroidRuntime(5664): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1218)
06-07 01:28:37.365: E/AndroidRuntime(5664): at
android.os.Handler.dispatchMessage(Handler.java:99) 06-07
01:28:37.365: E/AndroidRuntime(5664): at
android.os.Looper.loop(Looper.java:137)