【问题标题】:Access database with provider使用提供者访问数据库
【发布时间】:2014-02-11 10:49:46
【问题描述】:

不久前,我开始与数据库和内容提供商合作,并制作了一个简单的应用程序来学习如何使用它们。

现在,我正在开发另一个应用程序,它的功能与我所做的几乎相同。我正在尝试实现适用于新应用程序的相同代码,但我遇到了一个错误,我无法找到原因。

这是 LogCat 的输出:

02-11 10:45:59.014: E/ActivityThread(22665): Failed to find provider info for com.final.reversegeocoding
02-11 10:45:59.024: E/AndroidRuntime(22665): FATAL EXCEPTION: main
02-11 10:45:59.024: E/AndroidRuntime(22665): java.lang.IllegalArgumentException: Unknown URL content://com.final.reversegeocoding/places
02-11 10:45:59.024: E/AndroidRuntime(22665):    at android.content.ContentResolver.insert(ContentResolver.java:913)
02-11 10:45:59.024: E/AndroidRuntime(22665):    at com.final.reversegeocoding.GeocodingActivity.insertPlaces(GeocodingActivity.java:427)
02-11 10:45:59.024: E/AndroidRuntime(22665):    at com.final.reversegeocoding.GeocodingActivity.access$27(GeocodingActivity.java:416)
02-11 10:45:59.024: E/AndroidRuntime(22665):    at com.final.reversegeocoding.GeocodingActivity$6.onClick(GeocodingActivity.java:288)
02-11 10:45:59.024: E/AndroidRuntime(22665):    at android.view.View.performClick(View.java:4475)
02-11 10:45:59.024: E/AndroidRuntime(22665):    at android.view.View$PerformClick.run(View.java:18786)
02-11 10:45:59.024: E/AndroidRuntime(22665):    at android.os.Handler.handleCallback(Handler.java:730)
02-11 10:45:59.024: E/AndroidRuntime(22665):    at android.os.Handler.dispatchMessage(Handler.java:92)
02-11 10:45:59.024: E/AndroidRuntime(22665):    at android.os.Looper.loop(Looper.java:137)
02-11 10:45:59.024: E/AndroidRuntime(22665):    at android.app.ActivityThread.main(ActivityThread.java:5419)
02-11 10:45:59.024: E/AndroidRuntime(22665):    at java.lang.reflect.Method.invokeNative(Native Method)
02-11 10:45:59.024: E/AndroidRuntime(22665):    at java.lang.reflect.Method.invoke(Method.java:525)
02-11 10:45:59.024: E/AndroidRuntime(22665):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
02-11 10:45:59.024: E/AndroidRuntime(22665):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
02-11 10:45:59.024: E/AndroidRuntime(22665):    at dalvik.system.NativeStart.main(Native Method)

这是应用程序在数据库中保存值的路径:

MainActivity(地理编码活动)

private void insertPlaces(String number, String street, String city, String country1, String country2, String lat, String lng){
        ContentValues newValues = new ContentValues();
        newValues.put(Places.NUMBER, number);
        newValues.put(Places.STREET, street);
        newValues.put(Places.CITY, city);
        newValues.put(Places.COUNTRY1, country1);
        newValues.put(Places.COUNTRY2, country2);
        newValues.put(Places.LAT, lat);
        newValues.put(Places.LNG, lng);

        getContentResolver().insert(PlacesProvider.CONTENT_URI, newValues);
}

内容提供者(PlacesProvider)

public class PlacesProvider extends ContentProvider {

    /*CONTENT_URI*/
    private static final String AUTHORITY = "com.final.reversegeocoding";
    public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/places");

    private PlacesDatabaseHelper mDbHelper;
    private SQLiteDatabase database;

    /**UriMatcher*/
    private static final int URI_PLACES = 1;
    private static final int URI_PLACE_ITEM = 2;

    private static final UriMatcher mUriMatcher;
    static {
        mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        mUriMatcher.addURI(AUTHORITY, "places", URI_PLACES);
        mUriMatcher.addURI(AUTHORITY, "places/#", URI_PLACE_ITEM);
    }


    public class Places implements BaseColumns {

        public static final String NUMBER= "number";
        public static final String STREET= "street";
        public static final String CITY= "city";
        public static final String COUNTRY1= "country1";
        public static final String COUNTRY2= "pacountry2s";
        public static final String LAT = "latitude";
        public static final String LNG = "longitude";
    }


    @Override
    public boolean onCreate() {
        mDbHelper = new PlacesDatabaseHelper(getContext());
        return true;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {

        database = mDbHelper.getWritableDatabase();
        long id = database.insert(PlacesDatabaseHelper.TABLE_NAME, null, values);
        Uri result = null;

        if (id >= 0){
            result = ContentUris.withAppendedId(CONTENT_URI, id);
            getContext().getContentResolver().notifyChange(uri, null);
        }
        return result;
    }

【问题讨论】:

    标签: android sql database provider


    【解决方案1】:

    解决了。

    只是忘了在清单中声明提供者:

    <provider
        android:name=".database.PlacesProvider"
        android:authorities="com.final.reversegeocoding"
        android:multiprocess="true" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-21
      • 1970-01-01
      相关资源
      最近更新 更多