无法找到提供商信息com

时间:2016-10-15 08:18:02

标签: android android-contentprovider

我写了一个ContentProvider和一个ContentResolver,我收到了以下错误:

这是我的ContentProvider清单XML:

<?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):关闭VM 10-15 16:21:38.280:   E / AndroidRuntime(27234):致命异常:主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()'   参考

0 个答案:

没有答案