无法存储android设备的位置并将其存储在sqlite中

Posted

技术标签:

【中文标题】无法存储android设备的位置并将其存储在sqlite中【英文标题】:Cant store the location of a android device and store it in sqlite 【发布时间】:2014-08-14 20:30:42 【问题描述】:

我想存储位置并将其放入数据库中,我从这里获得了此代码。 但是应用程序停止找不到任何原因? 我还插入了我的 API 密钥。 link to the referencing site

MainActivity.java 文件

    package in.wptrafficanalyzer.locationmarkersqlite;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;


public class LocationsDB extends SQLiteOpenHelper

    /** Database name */
    private static String DBNAME = "locationmarkersqlite";

    /** Version number of the database */
    private static int VERSION = 1;

    /** Field 1 of the table locations, which is the primary key */
    public static final String FIELD_ROW_ID = "_id";

    /** Field 2 of the table locations, stores the latitude */
    public static final String FIELD_LAT = "lat";

    /** Field 3 of the table locations, stores the longitude*/
    public static final String FIELD_LNG = "lng";

    /** Field 4 of the table locations, stores the zoom level of map*/
    public static final String FIELD_ZOOM = "zom";

    /** A constant, stores the the table name */
    private static final String DATABASE_TABLE = "locations";

    /** An instance variable for SQLiteDatabase */
    private SQLiteDatabase mDB;  


    /** Constructor */
    public LocationsDB(Context context) 
        super(context, DBNAME, null, VERSION);  
        this.mDB = getWritableDatabase();
    


    /** This is a callback method, invoked when the method getReadableDatabase() / getWritableDatabase() is called 
      * provided the database does not exists 
    * */
    @Override
    public void onCreate(SQLiteDatabase db) 
        String sql =    "create table " + DATABASE_TABLE + " ( " +
                            FIELD_ROW_ID + " integer primary key autoincrement , " +
                            FIELD_LNG + " double , " +
                            FIELD_LAT + " double , " +
                            FIELD_ZOOM + " text " +
                        " ) ";

        db.execSQL(sql);
    

    /** Inserts a new location to the table locations */
    public long insert(ContentValues contentValues)
        long rowID = mDB.insert(DATABASE_TABLE, null, contentValues);
        return rowID;

       


    /** Deletes all locations from the table */
    public int del()
        int cnt = mDB.delete(DATABASE_TABLE, null , null);      
        return cnt;
    

    /** Returns all the locations from the table */
    public Cursor getAllLocations()
        return mDB.query(DATABASE_TABLE, new String[]  FIELD_ROW_ID,  FIELD_LAT , FIELD_LNG, FIELD_ZOOM  , null, null, null, null, null);
    

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 

    


locationscontentprovider.java

    package in.wptrafficanalyzer.locationmarkersqlite;

import java.sql.SQLException;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.net.Uri;

/** A custom Content Provider to do the database operations */
public class LocationsContentProvider extends ContentProvider

    public static final String PROVIDER_NAME = "in.wptrafficanalyzer.locationmarkersqlite.locations";

    /** A uri to do operations on locations table. A content provider is identified by its uri */
    public static final Uri CONTENT_URI = Uri.parse("content://" + PROVIDER_NAME + "/locations" );

    /** Constant to identify the requested operation */
    private static final int LOCATIONS = 1; 

    private static final UriMatcher uriMatcher ;

    static 
        uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        uriMatcher.addURI(PROVIDER_NAME, "locations", LOCATIONS);       
    

    /** This content provider does the database operations by this object */
    LocationsDB mLocationsDB;

    /** A callback method which is invoked when the content provider is starting up */
    @Override
    public boolean onCreate() 
        mLocationsDB = new LocationsDB(getContext());
        return true;
    

    /** A callback method which is invoked when insert operation is requested on this content provider */
    @Override
    public Uri insert(Uri uri, ContentValues values) 
        long rowID = mLocationsDB.insert(values);
        Uri _uri=null;
        if(rowID>0)
            _uri = ContentUris.withAppendedId(CONTENT_URI, rowID);
        else      
            try 
                throw new SQLException("Failed to insert : " + uri);
             catch (SQLException e) 
                e.printStackTrace();
            
        
        return _uri;    
    

    @Override
    public int update(Uri uri, ContentValues values, String selection,
            String[] selectionArgs) 
        // TODO Auto-generated method stub
        return 0;
    



    /** A callback method which is invoked when delete operation is requested on this content provider */
    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) 
        int cnt = 0;        
        cnt = mLocationsDB.del();   
        return cnt;
    

    /** A callback method which is invoked by default content uri */
    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)  

        if(uriMatcher.match(uri)==LOCATIONS)
            return mLocationsDB.getAllLocations();
        
        return null;
    


    @Override
    public String getType(Uri uri)         
        return null;
       

logcat 输出

    08-15 01:29:34.509: I/ActivityThread(543): Pub in.wptrafficanalyzer.locationmarkersqlite.locations: in.wptrafficanalyzer.locationmarkersqlite.LocationsContentProvider
08-15 01:29:34.818: D/AndroidRuntime(543): Shutting down VM
08-15 01:29:34.818: W/dalvikvm(543): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
08-15 01:29:34.828: E/AndroidRuntime(543): FATAL EXCEPTION: main
08-15 01:29:34.828: E/AndroidRuntime(543): java.lang.RuntimeException: Unable to start activity ComponentInfoin.wptrafficanalyzer.locationmarkersqlite/in.wptrafficanalyzer.locationmarkersqlite.MainActivity: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
08-15 01:29:34.828: E/AndroidRuntime(543):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
08-15 01:29:34.828: E/AndroidRuntime(543):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
08-15 01:29:34.828: E/AndroidRuntime(543):  at android.app.ActivityThread.access$600(ActivityThread.java:122)
08-15 01:29:34.828: E/AndroidRuntime(543):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
08-15 01:29:34.828: E/AndroidRuntime(543):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-15 01:29:34.828: E/AndroidRuntime(543):  at android.os.Looper.loop(Looper.java:137)
08-15 01:29:34.828: E/AndroidRuntime(543):  at android.app.ActivityThread.main(ActivityThread.java:4340)
08-15 01:29:34.828: E/AndroidRuntime(543):  at java.lang.reflect.Method.invokeNative(Native Method)
08-15 01:29:34.828: E/AndroidRuntime(543):  at java.lang.reflect.Method.invoke(Method.java:511)
08-15 01:29:34.828: E/AndroidRuntime(543):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-15 01:29:34.828: E/AndroidRuntime(543):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-15 01:29:34.828: E/AndroidRuntime(543):  at dalvik.system.NativeStart.main(Native Method)
08-15 01:29:34.828: E/AndroidRuntime(543): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
08-15 01:29:34.828: E/AndroidRuntime(543):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
08-15 01:29:34.828: E/AndroidRuntime(543):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
08-15 01:29:34.828: E/AndroidRuntime(543):  at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
08-15 01:29:34.828: E/AndroidRuntime(543):  at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
08-15 01:29:34.828: E/AndroidRuntime(543):  at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
08-15 01:29:34.828: E/AndroidRuntime(543):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251)
08-15 01:29:34.828: E/AndroidRuntime(543):  at android.app.Activity.setContentView(Activity.java:1835)
08-15 01:29:34.828: E/AndroidRuntime(543):  at in.wptrafficanalyzer.locationmarkersqlite.MainActivity.onCreate(MainActivity.java:33)
08-15 01:29:34.828: E/AndroidRuntime(543):  at android.app.Activity.performCreate(Activity.java:4465)
08-15 01:29:34.828: E/AndroidRuntime(543):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
08-15 01:29:34.828: E/AndroidRuntime(543):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
08-15 01:29:34.828: E/AndroidRuntime(543):  ... 11 more
08-15 01:29:34.828: E/AndroidRuntime(543): Caused by: java.lang.IllegalStateException: The meta-data tag in your app's AndroidManifest.xml does not have the right value.  Expected 4132500 but found 0.  You must have the following declaration within the <application> element:     <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
08-15 01:29:34.828: E/AndroidRuntime(543):  at com.google.android.gms.common.GooglePlayServicesUtil.n(Unknown Source)
08-15 01:29:34.828: E/AndroidRuntime(543):  at com.google.android.gms.common.GooglePlayServicesUtil.isGooglePlayServicesAvailable(Unknown Source)
08-15 01:29:34.828: E/AndroidRuntime(543):  at com.google.android.gms.maps.internal.q.v(Unknown Source)
08-15 01:29:34.828: E/AndroidRuntime(543):  at com.google.android.gms.maps.internal.q.u(Unknown Source)
08-15 01:29:34.828: E/AndroidRuntime(543):  at com.google.android.gms.maps.MapsInitializer.initialize(Unknown Source)
08-15 01:29:34.828: E/AndroidRuntime(543):  at com.google.android.gms.maps.SupportMapFragment$b.eb(Unknown Source)
08-15 01:29:34.828: E/AndroidRuntime(543):  at com.google.android.gms.maps.SupportMapFragment$b.a(Unknown Source)
08-15 01:29:34.828: E/AndroidRuntime(543):  at com.google.android.gms.dynamic.a.a(Unknown Source)
08-15 01:29:34.828: E/AndroidRuntime(543):  at com.google.android.gms.dynamic.a.onInflate(Unknown Source)
08-15 01:29:34.828: E/AndroidRuntime(543):  at com.google.android.gms.maps.SupportMapFragment.onInflate(Unknown Source)
08-15 01:29:34.828: E/AndroidRuntime(543):  at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:279)
08-15 01:29:34.828: E/AndroidRuntime(543):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:669)
08-15 01:29:34.828: E/AndroidRuntime(543):  ... 21 more
08-15 01:29:36.898: I/Process(543): Sending signal. PID: 543 SIG: 9

【问题讨论】:

错误信息告诉你你做错了什么。 【参考方案1】:

阅读最底部的“由”异常以获取有关问题及其修复的有用信息:

The meta-data tag in your app's AndroidManifest.xml does not have the right value.
Expected 4132500 but found 0.
You must have the following declaration within the <application> element:
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />

(我只是添加了一些换行符以使其更具可读性。)

【讨论】:

以上是关于无法存储android设备的位置并将其存储在sqlite中的主要内容,如果未能解决你的问题,请参考以下文章

php 文件以获取设备令牌并将其存储以供以后用户使用

获取 GPS 位置坐标并存储在数组中 [重复]

React Native:如何创建文本文件并将其保存到外部存储

谷歌地图中的当前位置并将接收到的位置存储在 TextView 上

你如何从android中的照片中提取地理位置信息?

如何从一个 android 设备检索 GPS 位置并将其实时提供给另一台 android 设备 [关闭]