Не удается найти информацию о поставщике com

#android #android-contentprovider

#Android #android-contentprovider

Вопрос:

Я написал ContentProvider и ContentResolver, и я получил следующую ошибку:

Это мой XML-манифест ContentProvider:

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

    <uses-sdk
        android:minSdkVersion="18"
        android:targetSdkVersion="21" />

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_DATABASE"/>
    <uses-permission android:name="android.permission.WRITE_DATABASE"/>
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <provider 
            android:name="com.example.androidcontentprovider.ProviderActivity"
            android:authorities="com.hr.test"
            android:exported="true"
            android:enabled="true"
            ></provider>
    </application>


</manifest>
 

ContentProviderActivity:

 package com.example.androidcontentprovider;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Environment;

public class ProviderActivity extends ContentProvider {

    private static UriMatcher um = new UriMatcher(UriMatcher.NO_MATCH);
    private SQLiteDatabase sqLite;

    public ProviderActivity() {
        // TODO Auto-generated constructor stub
    }

    @Override
    public boolean onCreate() {
        // TODO Auto-generated method stub
        um.addURI("com.hr.test", "something", 1);

        String str = Environment.getExternalStorageDirectory().toString();

        sqLite = SQLiteDatabase.openDatabase(str "/test", 
                null, SQLiteDatabase.OPEN_READWRITE|
                SQLiteDatabase.CREATE_IF_NECESSARY);

        sqLite.execSQL("create table books ([id] integer primary key autoincrement not null,"
                  "[bookname] varchar(30) not null)");

        ContentValues cv = new ContentValues();
        cv.put("bookname", "first book");

        sqLite.insert("books", null, cv);
        return false;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
            String[] selectionArgs, String sortOrder) {
        // TODO Auto-generated method stub
        if(um.match(uri) == 1){
            Cursor cursor = sqLite.query("books", null, null, null, null, null, null);
            return cursor;
        }
        else {
            return null;
        }
    }

    @Override
    public String getType(Uri uri) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        // TODO Auto-generated method stub
        return null;
    }

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

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

}
 

CotentResolverActivity ( Решаемая активность ):

 package com.example.androidcontentresolver;

import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

    private final String PATH = "content://com.hr.test/something";
    private ContentResolver cr;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        cr = this.getContentResolver();
        Button button = (Button)findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Cursor cursor = cr.query(Uri.parse(PATH), null, null, null, null);
                while(cursor.moveToNext()){
                    String nameString =  cursor.getString(1);
                    TextView tv = (TextView)findViewById(R.id.tv);
                    tv.setText(nameString);
                }
                cursor.close();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
 

LogCat:

10-15 16:21:36.220: D / ViewRootImpl(27234): ViewPostImeInputStage ACTION_DOWN 10-15 16:21:38.270: E / ActivityThread(27234): не удалось найти информацию о поставщике для com.hr.test 10-15 16:21:38.280: D / AndroidRuntime(27234): завершение работы виртуальной машины 10-15 16:21:38.280: E / AndroidRuntime(27234): ФАТАЛЬНОЕ ИСКЛЮЧЕНИЕ: main 10-15 16:21:38.280: E /AndroidRuntime(27234): Процесс: com.example.androidcontentresolver, PID: 27234 10-15 16:21:38.280: E /AndroidRuntime(27234): java.lang.Исключение NullPointerException: попытка вызвать метод интерфейса ‘boolean android.database.Cursor.moveToNext()’ для нулевой ссылки на объект

Комментарии:

1. где ошибка приложения?

2. Я только что добавил LogCat.

3. ContentProvider#onCreate javadocs говорят: возвращает true, если поставщик был успешно загружен, false в противном случае