2

我的目標是編寫一個沒有Activity的ContentProvider。爲了測試,我在自己的應用程序中編寫了一個測試活動。對於那些想告訴我,android中仍然包含記錄器的人,我知道這一點。我的Android ContentProvider無法找到ContentResolver

這裏是ContentProvider的

public static final String AUTHORITY = "de.xyz.android.log4android"; 
public static final int LOGGER_ARROW_URI_ID = 1; 
public static final String ARROW_CONTENT_TYPE = 
    "vnd.de.xyz.android.cursor.dir/vnr.log"; 

public static final int LOGGER_ITEM_URI_ID = 2; 
public static final String ITEM_CONTENT_TYPE = 
    "vnd.de.xyz.android.cursor.item/vnr.log"; 

public static final UriMatcher sUriMatcher; 
static { 
    sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH); 
    sUriMatcher.addURI(AUTHORITY, LOGGER_TABLE, LOGGER_ARROW_URI_ID); 
    sUriMatcher.addURI(AUTHORITY, LOGGER_TABLE + "#", LOGGER_ITEM_URI_ID); 
} 

public static final Uri CONTENT_URI = Uri.parse("content://" 
     + LogServiceProvider.AUTHORITY + "/logs"); 

public static final DefaultLogEntry LOG_ENTRY = null; 

//Some more not important code 

@Override 
public Uri insert(Uri uri, ContentValues initialValues) { 
    synchronized (this) { 
     try { 
      long rowID = dbHelper.getWritableDatabase().insert(LOGGER_TABLE, 
        null, initialValues); 

      if (rowID > 0) { 
       Uri _uri = ContentUris.withAppendedId(CONTENT_URI, rowID); 
       getContext().getContentResolver().notifyChange(_uri, null); 
       return _uri; 
      } 
     } catch (Exception ex) { 
     } 
     throw new SQLException("Failed to insert row into " + uri); 
    } 
} 

//Some more not important code 

所以我也加一部分內容提供商相關的信息的manifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
      package="de.xyz.android.logger" 
      android:versionCode="1" 
      android:versionName="1.0"> 

    <provider 
     android:name="de.xyz.android.logger.LogServiceProvider" 
     android:authorities="de.xyz.android.log4android"/> 

    <application android:icon="@drawable/icon" android:label="@string/app_name"> 
    <uses-sdk android:minSdkVersion="4" /> 
</application> 

這裏是部分我的客戶活動,即在單獨的應用程序。

ContentResolver cr = getContentResolver(); 
Uri getItem = Uri.parse("content://de.xyz.android.log4android/logs"); 
ContentValues values = new ContentValues(); 
values.put("level","WARNING"); 
values.put("time","1986-11-16"); 
values.put("message","FistTest"); 
values.put("owner","jsb"); 
values.put("file","testLog.java"); 
values.put("line","27"); 
Uri newItem = cr.insert(getItem, values); 

logcat的告訴我,TestClient的不能找到ContentProvider的

十二月5日至27日:42:52.099:ERROR/ActivityThread(548):未能找到de.xyz.android供應商信息。 log4android

我的錯誤在哪裏。在我看來,我所有提到:Content Providers | Android。如果你能給我一個提示,那會很好。如果你需要更多的代碼片段來理解,請問我。

+0

你確定你先安裝了apk與提供者嗎? – Selvin 2011-05-27 13:58:31

回答

4
<provider android:name=".LogServiceProvider" 
      android:authorities="de.xyz.android.log4android" 
      android:exported="true" /> 

應該幫助:)

+0

謝謝;-) o上帝那個小點。 – 2011-05-27 14:00:47

+0

我不知道爲什麼...在這裏http://developer.android.com/guide/topics/manifest/provider-element.html#nm thay說應該是「完全合格的類名」或「帶點」所以你的方法應該也可以 – Selvin 2011-05-27 14:03:24

1

一個在此可以失敗是,如果你有一個需要參數的內容提供者定義構造函數深奧的方式。這將導致默認的0參數構造函數不存在。這會導致提供程序的運行時註冊失敗,因爲系統在清單中的android:name下指定的類中找不到零參數構造函數。