我正在创建一个应用程序,使用SQLite数据库在地图上显示附近的地方。当我运行代码时,它将在 MainActivity.java:33 上提供 Java.lang.NoClassDefFoundError 。这是setContentView(R.layout.activity_main);
我检查了我的清单,然后我意识到了
` <meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />`
此行未添加到清单中且targetsdk版本为8.当我添加此行并将targetsdk版本更改为16时,将显示错误“错误:错误:找不到与给定匹配的资源name(在'value'处,值为'@ integer / google_play_services_version')。“但是当我将android值更改为4030500时,它会运行但崩溃。现在我不明白它崩溃的原因。请帮忙。这是我的清单代码,mainActivity.java类和log cat。
logcat的
> 05-12 00:39:10.705: D/jdwp(31381): sendBufferedRequest : len=0x6D
05-12 00:39:10.707: W/asset(31381): AssetManager-->addDefaultAssets CIP path not exsit!
05-12 00:39:10.738: D/ActivityThread(31381): installProvider: context=android.app.Application@4184c140holder=nullnoisy=falsenoReleaseNeeded=truestable=true
05-12 00:39:10.738: D/ActivityThread(31381): installProvider: context.getPackageName()=in.wptrafficanalyzer.locationmarkersqliteai.packageName=in.wptrafficanalyzer.locationmarkersqlite
05-12 00:39:10.929: W/dalvikvm(31381): VFY: unable to resolve static field 982 (MapAttrs) in Lcom/google/android/gms/R$styleable;
05-12 00:39:10.929: D/dalvikvm(31381): VFY: replacing opcode 0x62 at 0x000e
05-12 00:39:10.936: D/AndroidRuntime(31381): Shutting down VM
05-12 00:39:10.936: W/dalvikvm(31381): threadid=1: thread exiting with uncaught exception (group=0x412899a8)
05-12 00:39:10.951: E/AndroidRuntime(31381): FATAL EXCEPTION: main
05-12 00:39:10.951: E/AndroidRuntime(31381): java.lang.NoClassDefFoundError: com.google.android.gms.R$styleable
05-12 00:39:10.951: E/AndroidRuntime(31381): at com.google.android.gms.maps.GoogleMapOptions.createFromAttributes(Unknown Source)
05-12 00:39:10.951: E/AndroidRuntime(31381): at com.google.android.gms.maps.SupportMapFragment.onInflate(Unknown Source)
05-12 00:39:10.951: E/AndroidRuntime(31381): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:279)
05-12 00:39:10.951: E/AndroidRuntime(31381): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
05-12 00:39:10.951: E/AndroidRuntime(31381): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
05-12 00:39:10.951: E/AndroidRuntime(31381): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
05-12 00:39:10.951: E/AndroidRuntime(31381): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
05-12 00:39:10.951: E/AndroidRuntime(31381): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
05-12 00:39:10.951: E/AndroidRuntime(31381): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:281)
05-12 00:39:10.951: E/AndroidRuntime(31381): at android.app.Activity.setContentView(Activity.java:1881)
05-12 00:39:10.951: E/AndroidRuntime(31381): at in.wptrafficanalyzer.locationmarkersqlite.MainActivity.onCreate(MainActivity.java:33)
05-12 00:39:10.951: E/AndroidRuntime(31381): at android.app.Activity.performCreate(Activity.java:5122)
05-12 00:39:10.951: E/AndroidRuntime(31381): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
05-12 00:39:10.951: E/AndroidRuntime(31381): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
05-12 00:39:10.951: E/AndroidRuntime(31381): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
05-12 00:39:10.951: E/AndroidRuntime(31381): at android.app.ActivityThread.access$600(ActivityThread.java:162)
05-12 00:39:10.951: E/AndroidRuntime(31381): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
05-12 00:39:10.951: E/AndroidRuntime(31381): at android.os.Handler.dispatchMessage(Handler.java:107)
05-12 00:39:10.951: E/AndroidRuntime(31381): at android.os.Looper.loop(Looper.java:194)
05-12 00:39:10.951: E/AndroidRuntime(31381): at android.app.ActivityThread.main(ActivityThread.java:5371)
05-12 00:39:10.951: E/AndroidRuntime(31381): at java.lang.reflect.Method.invokeNative(Native Method)
05-12 00:39:10.951: E/AndroidRuntime(31381): at java.lang.reflect.Method.invoke(Method.java:525)
05-12 00:39:10.951: E/AndroidRuntime(31381): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
05-12 00:39:10.951: E/AndroidRuntime(31381): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
05-12 00:39:10.951: E/AndroidRuntime(31381): at dalvik.system.NativeStart.main(Native Method)
05-12 00:39:28.242: D/jdwp(31463): sendBufferedRequest : len=0x6D
05-12 00:39:28.243: D/jdwp(31463): processIncoming
05-12 00:39:28.243: D/jdwp(31463): handlePacket : cmd=0x1, cmdSet=0xC7, len=0x13, id=0x400001C0, flags=0x0, dataLen=0x8
05-12 00:39:28.245: D/jdwp(31463): processIncoming
05-12 00:39:28.245: D/jdwp(31463): handlePacket : cmd=0x1, cmdSet=0xC7, len=0x17, id=0x400001C1, flags=0x0, dataLen=0xC
05-12 00:39:28.245: W/asset(31463): AssetManager-->addDefaultAssets CIP path not exsit!
05-12 00:39:28.246: D/jdwp(31463): processIncoming
05-12 00:39:28.246: D/jdwp(31463): handlePacket : cmd=0x1, cmdSet=0xC7, len=0x13, id=0x400001C2, flags=0x0, dataLen=0x8
05-12 00:39:28.246: D/jdwp(31463): processIncoming
05-12 00:39:28.247: D/jdwp(31463): handlePacket : cmd=0x1, cmdSet=0xC7, len=0x13, id=0x400001C3, flags=0x0, dataLen=0x8
05-12 00:39:28.253: D/ActivityThread(31463): installProvider: context=android.app.Application@4184e080holder=nullnoisy=falsenoReleaseNeeded=truestable=true
05-12 00:39:28.253: D/ActivityThread(31463): installProvider: context.getPackageName()=in.wptrafficanalyzer.locationmarkersqliteai.packageName=in.wptrafficanalyzer.locationmarkersqlite
05-12 00:39:28.316: W/dalvikvm(31463): VFY: unable to resolve static field 982 (MapAttrs) in Lcom/google/android/gms/R$styleable;
05-12 00:39:28.316: D/dalvikvm(31463): VFY: replacing opcode 0x62 at 0x000e
05-12 00:39:28.318: D/AndroidRuntime(31463): Shutting down VM
05-12 00:39:28.319: W/dalvikvm(31463): threadid=1: thread exiting with uncaught exception (group=0x412899a8)
05-12 00:39:28.321: E/AndroidRuntime(31463): FATAL EXCEPTION: main
05-12 00:39:28.321: E/AndroidRuntime(31463): java.lang.NoClassDefFoundError: com.google.android.gms.R$styleable
05-12 00:39:28.321: E/AndroidRuntime(31463): at com.google.android.gms.maps.GoogleMapOptions.createFromAttributes(Unknown Source)
05-12 00:39:28.321: E/AndroidRuntime(31463): at com.google.android.gms.maps.SupportMapFragment.onInflate(Unknown Source)
05-12 00:39:28.321: E/AndroidRuntime(31463): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:279)
05-12 00:39:28.321: E/AndroidRuntime(31463): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
05-12 00:39:28.321: E/AndroidRuntime(31463): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
05-12 00:39:28.321: E/AndroidRuntime(31463): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
05-12 00:39:28.321: E/AndroidRuntime(31463): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
05-12 00:39:28.321: E/AndroidRuntime(31463): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
05-12 00:39:28.321: E/AndroidRuntime(31463): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:281)
05-12 00:39:28.321: E/AndroidRuntime(31463): at android.app.Activity.setContentView(Activity.java:1881)
05-12 00:39:28.321: E/AndroidRuntime(31463): at in.wptrafficanalyzer.locationmarkersqlite.MainActivity.onCreate(MainActivity.java:33)
05-12 00:39:28.321: E/AndroidRuntime(31463): at android.app.Activity.performCreate(Activity.java:5122)
05-12 00:39:28.321: E/AndroidRuntime(31463): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
05-12 00:39:28.321: E/AndroidRuntime(31463): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
05-12 00:39:28.321: E/AndroidRuntime(31463): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
05-12 00:39:28.321: E/AndroidRuntime(31463): at android.app.ActivityThread.access$600(ActivityThread.java:162)
05-12 00:39:28.321: E/AndroidRuntime(31463): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
05-12 00:39:28.321: E/AndroidRuntime(31463): at android.os.Handler.dispatchMessage(Handler.java:107)
05-12 00:39:28.321: E/AndroidRuntime(31463): at android.os.Looper.loop(Looper.java:194)
05-12 00:39:28.321: E/AndroidRuntime(31463): at android.app.ActivityThread.main(ActivityThread.java:5371)
05-12 00:39:28.321: E/AndroidRuntime(31463): at java.lang.reflect.Method.invokeNative(Native Method)
05-12 00:39:28.321: E/AndroidRuntime(31463): at java.lang.reflect.Method.invoke(Method.java:525)
05-12 00:39:28.321: E/AndroidRuntime(31463): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
05-12 00:39:28.321: E/AndroidRuntime(31463): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
05-12 00:39:28.321: E/AndroidRuntime(31463): at dalvik.system.NativeStart.main(Native Method)
清单
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="16" />
<permission
android:name="in.wptrafficanalyzer.locationmarkersqlite.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="in.wptrafficanalyzer.locationmarkersqlite.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="in.wptrafficanalyzer.locationmarkersqlite.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>
<provider
android:name="LocationsContentProvider"
android:authorities="in.wptrafficanalyzer.locationmarkersqlite.locations"
android:exported="false" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyDWcQ-K232pqKKftNuiP_f-OIQ9YaqNQiM" />
<meta-data
android:name="com.google.android.gms.version"
android:value="4030500" />
</application>
</manifest>
MainActivity.java
package in.wptrafficanalyzer.locationmarkersqlite;
import android.app.Dialog;
import android.content.ContentValues;
import android.database.Cursor;
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.common.GooglePlayServicesUtil;
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
}
}
http://wptrafficanalyzer.in/blog/gps-and-google-map-in-android-applications-series/
这是我用过的教程。