无法获得提供商

时间:2016-02-12 11:32:29

标签: android android-contentprovider provider

我在获取提供程序时遇到问题。我寻找答案,但我仍然无法得到正确的答案。请帮我解决一下这个。 这是我的清单:

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

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="19" />

<uses-permission android:name="android.permission.VIBRATE" />

<application
    android:allowBackup="true"
    android:icon="@drawable/icon"
    android:label="@string/app_name"
    android:theme="@style/LightTheme">
    <provider
        android:name="com.ultramegatech.ey.provider.ElementsProvider"
        android:authorities="com.ultramegatech.ey.provider"
        android:multiprocess="true"
        android:exported="false" />

    <activity
        android:name="PeriodicTableActivity"
        android:label="@string/app_name"
        android:screenOrientation="landscape"
        android:theme="@style/LightTheme.NoTitleBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="ElementListActivity"
        android:label="@string/titleElementList" />
    <activity
        android:name="ElementDetailsActivity"
        android:label="@string/app_name" />
    <activity
        android:name="EyPreferenceActivity"
        android:label="@string/titlePreferences" />
</application>
</manifest>

我的数据库提供商

package com.ultramegatech.ey.provider;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;

/**
* ContentProvider for accessing the chemical element database.
* 
* @author Steve Guidetti
*/
public class ElementsProvider extends ContentProvider {
/* Provider authority */

public static final String AUTHORITY = "com.ultramegatech.ey.provider";

/* Uri matcher ids */
private static final int ELEMENTS = 1;
private static final int ELEMENTS_ID = 2;
private static final int ELEMENTS_NUMBER = 3;
private static final int ELEMENTS_SYMBOL = 4;
private static final int ELEMENTS_FILTER = 5;

/* Uri matcher */
private static final UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
static {
    sUriMatcher.addURI(AUTHORITY, "elements", ELEMENTS);
    sUriMatcher.addURI(AUTHORITY, "elements/#", ELEMENTS_ID);
    sUriMatcher.addURI(AUTHORITY, "elements/n/#", ELEMENTS_NUMBER);
    sUriMatcher.addURI(AUTHORITY, "elements/s/*", ELEMENTS_SYMBOL);
    sUriMatcher.addURI(AUTHORITY, "elements/filter/*", ELEMENTS_FILTER);
}

/* SQLiteOpenHelper */
private DatabaseOpenHelper mDatabaseOpenHelper;

@Override
public boolean onCreate() {
    mDatabaseOpenHelper = new DatabaseOpenHelper(getContext());
    return true;
}

@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
        String sortOrder) {
    final SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
    switch(sUriMatcher.match(uri)) {
        case ELEMENTS:
            qb.setTables(Elements.TABLE_NAME);
            break;
        case ELEMENTS_ID:
            qb.setTables(Elements.TABLE_NAME);
            qb.appendWhere(Elements._ID + " = " + uri.getLastPathSegment());
            break;
        case ELEMENTS_NUMBER:
            qb.setTables(Elements.TABLE_NAME);
            qb.appendWhere(Elements.NUMBER + " = " + uri.getLastPathSegment());
            break;
        case ELEMENTS_SYMBOL:
            qb.setTables(Elements.TABLE_NAME);
            qb.appendWhere(Elements.SYMBOL + " = ");
            qb.appendWhereEscapeString(uri.getLastPathSegment());
            break;
        case ELEMENTS_FILTER:
            qb.setTables(Elements.TABLE_NAME);
            qb.appendWhere(Elements.NAME + " LIKE ");
            qb.appendWhereEscapeString(uri.getLastPathSegment() + "%");
            qb.appendWhere(" OR " + Elements.SYMBOL + " LIKE ");
            qb.appendWhereEscapeString(uri.getLastPathSegment() + "%");
            break;
        default:
            throw new IllegalArgumentException("Invalid URI: " + uri.toString());
    }

    final SQLiteDatabase db = mDatabaseOpenHelper.getReadableDatabase();
    final Cursor cursor = qb.query(db, projection, selection, selectionArgs, null, null,
            sortOrder);
    cursor.setNotificationUri(getContext().getContentResolver(), uri);
    return cursor;
}

@Override
public String getType(Uri uri) {
    switch(sUriMatcher.match(uri)) {
        case ELEMENTS:
        case ELEMENTS_FILTER:
            return Elements.DATA_TYPE;
        case ELEMENTS_ID:
        case ELEMENTS_NUMBER:
        case ELEMENTS_SYMBOL:
            return Elements.DATA_TYPE_ITEM;
        default:
            return null;
    }
}

@Override
public Uri insert(Uri uri, ContentValues values) {
    String table;
    switch(sUriMatcher.match(uri)) {
        case ELEMENTS:
            table = Elements.TABLE_NAME;
            break;
        default:
            throw new IllegalArgumentException("Invalid URI");
    }

    final SQLiteDatabase db = mDatabaseOpenHelper.getWritableDatabase();
    final long rowId = db.insert(table, null, values);
    if(rowId > 0) {
        getContext().getContentResolver().notifyChange(uri, null);
        return ContentUris.withAppendedId(uri, rowId);
    }

    throw new SQLException("Failed to insert row into " + uri.toString());
}

@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
    String table;
    switch(sUriMatcher.match(uri)) {
        case ELEMENTS:
            table = Elements.TABLE_NAME;
            break;
        case ELEMENTS_ID:
            table = Elements.TABLE_NAME;
            final String id = uri.getLastPathSegment();
            if(selection == null) {
                selection = Elements._ID + " = " + id;
            } else {
                selection += " AND " + Elements._ID + " = " + id;
            }
            break;
        case ELEMENTS_NUMBER:
            table = Elements.TABLE_NAME;
            final String number = uri.getLastPathSegment();
            if(selection == null) {
                selection = Elements.NUMBER + " = " + number;
            } else {
                selection += " AND " + Elements.NUMBER + " = " + number;
            }
            break;
        default:
            throw new IllegalArgumentException("Invalid URI");
    }

    final SQLiteDatabase db = mDatabaseOpenHelper.getWritableDatabase();
    final int rowsAffected = db.delete(table, selection, selectionArgs);
    getContext().getContentResolver().notifyChange(uri, null);
    return rowsAffected;
}

@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
    String table;
    switch(sUriMatcher.match(uri)) {
        case ELEMENTS:
            table = Elements.TABLE_NAME;
            break;
        case ELEMENTS_ID:
            table = Elements.TABLE_NAME;
            final String id = uri.getLastPathSegment();
            if(selection == null) {
                selection = Elements._ID + " = " + id;
            } else {
                selection += " AND " + Elements._ID + " = " + id;
            }
            break;
        case ELEMENTS_NUMBER:
            table = Elements.TABLE_NAME;
            final String number = uri.getLastPathSegment();
            if(selection == null) {
                selection = Elements.NUMBER + " = " + number;
            } else {
                selection += " AND " + Elements.NUMBER + " = " + number;
            }
            break;
        default:
            throw new IllegalArgumentException("Invalid URI");
    }

    final SQLiteDatabase db = mDatabaseOpenHelper.getWritableDatabase();
    final int rowsAffected = db.update(table, values, selection, selectionArgs);
    getContext().getContentResolver().notifyChange(uri, null);
    return rowsAffected;
}
}

这是Logcat:

  

02-12 06:00:42.041:E / AndroidRuntime(1245):   java.lang.RuntimeException:无法获取提供者   com.ultramegatech.ey.provider:java.lang.ClassNotFoundException:   没找到课程&#34; com.ultramegatech.ey.provider&#34;在路径上:   DexPathList [[zip文件   &#34; /data/app/com.ultramegatech.ey-1.apk"],nativeLibraryDirectories = [/数据/ APP-LIB / com.ultramegatech.ey-1,   /系统/ lib中]]       02-12 06:00:42.041:E / AndroidRuntime(1245):在android.app.ActivityThread.installProvider(ActivityThread.java:4774)       02-12 06:00:42.041:E / AndroidRuntime(1245):在android.app.ActivityThread.installContentProviders(ActivityThread.java:4366)       02-12 06:00:42.041:E / AndroidRuntime(1245):在android.app.ActivityThread.handleBindApplication(ActivityThread.java:4306)       02-12 06:00:42.041:E / AndroidRuntime(1245):在android.app.ActivityThread.access $ 1400(ActivityThread.java:135)       02-12 06:00:42.041:E / AndroidRuntime(1245):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1457)       02-12 06:00:42.041:E / AndroidRuntime(1245):在android.os.Handler.dispatchMessage(Handler.java:102)       02-12 06:00:42.041:E / AndroidRuntime(1245):在android.os.Looper.loop(Looper.java:137)       02-12 06:00:42.041:E / AndroidRuntime(1245):在android.app.ActivityThread.main(ActivityThread.java:4998)       02-12 06:00:42.041:E / AndroidRuntime(1245):at java.lang.reflect.Method.invokeNative(Native Method)       02-12 06:00:42.041:E / AndroidRuntime(1245):at java.lang.reflect.Method.invoke(Method.java:515)       02-12 06:00:42.041:E / AndroidRuntime(1245):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:777)       02-12 06:00:42.041:E / AndroidRuntime(1245):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)       02-12 06:00:42.041:E / AndroidRuntime(1245):at dalvik.system.NativeStart.main(Native Method)       02-12 06:00:42.041:E / AndroidRuntime(1245):引起:java.lang.ClassNotFoundException:没找到类   &#34; com.ultramegatech.ey.provider&#34;在路径上:DexPathList [[zip文件   &#34; /data/app/com.ultramegatech.ey-1.apk"],nativeLibraryDirectories = [/数据/ APP-LIB / com.ultramegatech.ey-1,   /系统/ lib中]]       02-12 06:00:42.041:E / AndroidRuntime(1245):at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)       02-12 06:00:42.041:E / AndroidRuntime(1245):at java.lang.ClassLoader.loadClass(ClassLoader.java:497)       02-12 06:00:42.041:E / AndroidRuntime(1245):at java.lang.ClassLoader.loadClass(ClassLoader.java:457)       02-12 06:00:42.041:E / AndroidRuntime(1245):在android.app.ActivityThread.installProvider(ActivityThread.java:4759)       02-12 06:00:42.041:E / AndroidRuntime(1245):... 12更多       02-12 06:00:54.471:I / Process(1245):发送信号。 PID:1245 SIG:9

我的.sql是在原始文件夹上,如果它有问题。 我只是在gitHub中获取代码,我将为我的论文修改它。谢谢你的回答将不胜感激。提前谢谢!

1 个答案:

答案 0 :(得分:0)

问题在于以下2行:

    android:name="com.ultramegatech.ey.provider"
    android:authorities="com.ultramegatech.ey.provider.ElementsProvider"

他们应该是相反的方式:

android:name="com.ultramegatech.ey.provider.ElementsProvider"
android:authorities="com.ultramegatech.ey.provider"

同时更改提供程序类中的authorityandroid:name应该指向类名而不仅仅是包。 authority可以是包名称或其他任何内容。