我有两个应用 App1 和 App2 。 App1在内容提供商中有数据库, App2 会在 App1 的数据库中插入数据。但是当我调用 getContentResolver()。insert(...)时,它总是以uri的形式返回null。
以下是 App1 和 App2
的代码App1: - 提供商
package com.oozeetech.apscure.provider;
import java.util.HashMap;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.text.TextUtils;
import com.oozeetech.apscure.base.Constants;
public class ThemeProvider extends ContentProvider {
private static HashMap<String, String> STUDENTS_PROJECTION_MAP;
static final int STUDENTS = 1;
static final int STUDENT_ID = 2;
static final UriMatcher uriMatcher;
static {
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(Constants.PROVIDER_NAME, Constants.TABLE_APSCURE_THEME, STUDENTS);
uriMatcher.addURI(Constants.PROVIDER_NAME, Constants.TABLE_APSCURE_THEME + "/#", STUDENT_ID);
}
/**
* Database specific constant declarations
*/
private SQLiteDatabase db;
/**
* Helper class that actually creates and manages
* the provider's underlying data repository.
*/
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, Constants.DATABASE_NAME, null, Constants.DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(Constants.CREATE_DB_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + Constants.TABLE_APSCURE_THEME);
onCreate(db);
}
}
@Override
public boolean onCreate() {
Context context = getContext();
DatabaseHelper dbHelper = new DatabaseHelper(context);
/**
* Create a write able database which will trigger its
* creation if it doesn't already exist.
*/
db = dbHelper.getWritableDatabase();
return (db == null) ? false : true;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
/**
* Add a new student record
*/
long rowID = db.insert(Constants.TABLE_APSCURE_THEME, "", values);
/**
* If record is added successfully
*/
if (rowID > 0) {
Uri _uri = ContentUris.withAppendedId(Constants.CONTENT_URI, rowID);
getContext().getContentResolver().notifyChange(_uri, null);
return _uri;
}
throw new SQLException("Failed to add a record into " + uri);
}
@Override
public Cursor query(Uri uri, String[] projection,
String selection, String[] selectionArgs, String sortOrder) {
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
qb.setTables(Constants.TABLE_APSCURE_THEME);
switch (uriMatcher.match(uri)) {
case STUDENTS:
qb.setProjectionMap(STUDENTS_PROJECTION_MAP);
break;
case STUDENT_ID:
qb.appendWhere(Constants.TAG_ID + "=" + uri.getPathSegments().get(1));
break;
default:
}
if (sortOrder == null || sortOrder == "") {
/**
* By default sort on student names
*/
sortOrder = Constants.TAG_ID;
}
Cursor c = qb.query(db, projection, selection,
selectionArgs, null, null, sortOrder);
/**
* register to watch a content URI for changes
*/
c.setNotificationUri(getContext().getContentResolver(), uri);
return c;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
int count = 0;
switch (uriMatcher.match(uri)) {
case STUDENTS:
count = db.delete(Constants.TABLE_APSCURE_THEME, selection, selectionArgs);
break;
case STUDENT_ID:
String id = uri.getPathSegments().get(1);
count = db.delete(Constants.TABLE_APSCURE_THEME, Constants.TAG_ID + " = " + id +
(!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : ""), selectionArgs);
break;
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
getContext().getContentResolver().notifyChange(uri, null);
return count;
}
@Override
public int update(Uri uri, ContentValues values,
String selection, String[] selectionArgs) {
int count = 0;
switch (uriMatcher.match(uri)) {
case STUDENTS:
count = db.update(Constants.TABLE_APSCURE_THEME, values, selection, selectionArgs);
break;
case STUDENT_ID:
count = db.update(Constants.TABLE_APSCURE_THEME, values,
Constants.TAG_ID + " = " + uri.getPathSegments().get(1) +
(!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : ""), selectionArgs);
break;
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
getContext().getContentResolver().notifyChange(uri, null);
return count;
}
@Override
public String getType(Uri uri) {
switch (uriMatcher.match(uri)) {
/**
* Get all student records
*/
case STUDENTS:
return "vnd.android.cursor.dir/vnd.com.oozeetech.apscure.provider.ThemeProvider.Apscure_Theme_Pattern";
/**
* Get a particular student
*/
case STUDENT_ID:
return "vnd.android.cursor.item/vnd.com.oozeetech.apscure.provider.ThemeProvider.Apscure_Theme_Pattern";
default:
throw new IllegalArgumentException("Unsupported URI: " + uri);
}
}
}
App1 AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.oozeetech.apscure">
<!--Content Provider persmissions-->
<permission
android:name="READ_DATABASE"
android:label="Read"
android:protectionLevel="normal" />
<permission
android:name="WRITE_DATABASE"
android:label="Write"
android:protectionLevel="normal" />
<application
android:name=".AppLockApplication"
android:allowBackup="true"
android:allowClearUserData="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:manageSpaceActivity="com.oozeetech.apscure.ManageSpaceActivity"
android:theme="@style/AppTheme">
<!--Content Provider Declaration for the third party themes-->
<provider
android:name=".provider.ThemeProvider"
android:authorities="com.oozeetech.apscure.provider.ThemeProvider"
android:exported="true"
android:multiprocess="true"
android:grantUriPermissions="true"
android:readPermission="com.oozeetech.apscure.READ_DATABASE"
android:writePermission="com.oozeetech.apscure.WRITE_DATABASE" />
<!--Content Provider Declaratikon for the third party themes-->
</application>
</manifest>
App2插入查询
ContentValues values = new ContentValues();
values.put(Constants.TAG_BG_IMAGE, getByteArrayOfImage(R.drawable.img_pattern_eighteen));
values.put(Constants.TAG_NODE_EMPTY_IMAGE, getByteArrayOfImage(R.drawable.node_small_active));
values.put(Constants.TAG_NODE_SELECTED_IMAGE, getByteArrayOfImage(R.drawable.node_active));
values.put(Constants.TAG_LINE_COLOR, "#D4E157");
values.put(Constants.TAG_THEME_TAG, "com.apscure.oozeetech.themetwo");
Uri uri = getContentResolver().insert(Uri.parse("content://com.oozeetech.apscure.provider.ThemeProvider/Apscure_Theme_Pattern"), values);
Toast.makeText(getBaseContext(), uri.toString() + R.string.alert_successfullyAppliedTheme, Toast.LENGTH_LONG).show();
App2 AndroidMenifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.apscure.oozeetech.themetwo">
<uses-permission android:name="com.oozeetech.apscure.READ_DATABASE" />
<uses-permission android:name="com.oozeetech.apscure.WRITE_DATABASE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
请让我知道错误,所以我可以解决。
由于
更新:
日志:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.toString()' on a null object reference
11-18 03:36:17.138 1959-1959/com.apscure.oozeetech.themetwo W/System.err: at com.apscure.oozeetech.themetwo.MainActivity.onCreate(MainActivity.java:43)
11-18 03:36:17.138 1959-1959/com.apscure.oozeetech.themetwo W/System.err: at android.app.Activity.performCreate(Activity.java:5990)
11-18 03:36:17.138 1959-1959/com.apscure.oozeetech.themetwo W/System.err: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
11-18 03:36:17.138 1959-1959/com.apscure.oozeetech.themetwo W/System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
11-18 03:36:17.138 1959-1959/com.apscure.oozeetech.themetwo W/System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
11-18 03:36:17.138 1959-1959/com.apscure.oozeetech.themetwo W/System.err: at android.app.ActivityThread.access$800(ActivityThread.java:151)
11-18 03:36:17.138 1959-1959/com.apscure.oozeetech.themetwo W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
11-18 03:36:17.138 1959-1959/com.apscure.oozeetech.themetwo W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
11-18 03:36:17.138 1959-1959/com.apscure.oozeetech.themetwo W/System.err: at android.os.Looper.loop(Looper.java:135)
11-18 03:36:17.138 1959-1959/com.apscure.oozeetech.themetwo W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5254)
11-18 03:36:17.138 1959-1959/com.apscure.oozeetech.themetwo W/System.err: at java.lang.reflect.Method.invoke(Native Method)
11-18 03:36:17.138 1959-1959/com.apscure.oozeetech.themetwo W/System.err: at java.lang.reflect.Method.invoke(Method.java:372)
11-18 03:36:17.138 1959-1959/com.apscure.oozeetech.themetwo W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
11-18 03:36:17.138 1959-1959/com.apscure.oozeetech.themetwo W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)