Java 导致更多问题的未知Uri(Android)

Java 导致更多问题的未知Uri(Android),java,android,eclipse,android-studio,fatal-error,Java,Android,Eclipse,Android Studio,Fatal Error,我一直在努力解决这个问题很长时间了。我会张贴什么,我认为是足够的,如果你需要更多的信息,然后请问我 public class PInfoProvider extends ContentProvider { private PInfoDatabase mOpenHelper; private static String TAG = PInfoProvider.class.getSimpleName(); private static final UriMatcher sU

我一直在努力解决这个问题很长时间了。我会张贴什么,我认为是足够的,如果你需要更多的信息,然后请问我

public class PInfoProvider extends ContentProvider {
    private PInfoDatabase mOpenHelper;

    private static String TAG = PInfoProvider.class.getSimpleName();
    private static final UriMatcher sUriMatcher = buildUriMatcher();

    private static final int PINFOS = 100;
    private static final int PINFOS_ID = 101;

    private static UriMatcher buildUriMatcher(){
        final UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
        final String authority = PInfoContract.CONTENT_AUTHORITY;
        matcher.addURI(authority, "PInfos", PINFOS);
        matcher.addURI(authority, "Pinfos/*", PINFOS_ID);
        return matcher;
    }
    @Override
    public boolean onCreate() {
        mOpenHelper = new PInfoDatabase(getContext());
        return true;
    }

    private void deleteDatabase(){
        mOpenHelper.close();
        PInfoDatabase.deleteDatabase(getContext());
        mOpenHelper = new PInfoDatabase(getContext());
    }
    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        final SQLiteDatabase db = mOpenHelper.getReadableDatabase();
        final int match = sUriMatcher.match(uri);

        SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
        queryBuilder.setTables(PInfoDatabase.Tables.PInfo);

        switch(match){
            case PINFOS:
                //do nothing
                break;
            case PINFOS_ID:
                String id = PInfoContract.PInfos.getPInfoId(uri);
                queryBuilder.appendWhere(BaseColumns._ID + "=" + id);
                break;
            default:
                throw new IllegalArgumentException("Unknown Uri: " + uri);
        }

        Cursor cursor = queryBuilder.query(db, projection, selection, selectionArgs, null, null, sortOrder);
        //projection in content provider means the list of columns you want to return.
        return cursor;

    }

    @Override
    public String getType(Uri uri) {
        final int match = sUriMatcher.match(uri);
        switch(match){
            case PINFOS:
                return PInfoContract.PInfos.CONTENT_TYPE;
            case PINFOS_ID:
                return PInfoContract.PInfos.CONTENT_ITEM_TYPE;
            default:
                throw new IllegalArgumentException("Unknown Uri: " + uri);
        }
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        //ContentValues: is a list of content values of our database such as the email and username. Contains the column names and the values we want to associate to it when we're writing to the database/
        Log.v(TAG, "insert(uri=" + uri + ", values =" + values.toString());

        final SQLiteDatabase db = mOpenHelper.getWritableDatabase();
        final int match = sUriMatcher.match(uri);

        switch(match){
            //only using the PInfo and not the ID becuase it's to insert only.
            case PINFOS:
                long recordID = db.insertOrThrow(PInfoDatabase.Tables.PInfo, null, values);
                return PInfoContract.PInfos.buildPInfoUri(String.valueOf(recordID));
            default:
                throw new IllegalArgumentException("Unknown Uri: " + uri);
        }
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        Log.v(TAG, "delete(uri=" + uri);

        if(uri.equals(PInfoContract.URI_TABLE)){
            deleteDatabase();
            return 0;
            //This will be executed if the user didn't input a valid record id.
            //Base content uri doesn't contain an ID nor a path.
        }
        final SQLiteDatabase db = mOpenHelper.getWritableDatabase();
        final int match = sUriMatcher.match(uri);

        String selectionCriteria = selection;

        switch(match){

            /*
            case PInfo:
                If this was called and didn't "break" it would change all the records in the table.
                We will still leave it out because we added the database deletion code above.
                 break;
             */

            case PINFOS_ID:
                String id = PInfoContract.PInfos.getPInfoId(uri);
                selectionCriteria = BaseColumns._ID + "=" + id
                        + (!TextUtils.isEmpty(selection) ? "AND (" + selection + ")" : "");
                return db.delete(PInfoDatabase.Tables.PInfo, selectionCriteria, selectionArgs);

            default:
                throw new IllegalArgumentException("Unknown Uri: " + uri);
        }

    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        Log.v(TAG, "update(uri=" + uri + ", values =" + values.toString());

        final SQLiteDatabase db = mOpenHelper.getWritableDatabase();
        final int match = sUriMatcher.match(uri);

        String selectionCriteria = selection;
        //We set selectionCriteria to equal selection in the case PInfo case was chosen. After all,
        //we still need to record the selection.

        switch(match){
            case PINFOS:
                //do nothing
                //If this was called and didn't "break" it would change all the records in the table.
                break;
            case PINFOS_ID:
                String id = PInfoContract.PInfos.getPInfoId(uri);
                selectionCriteria = BaseColumns._ID + "=" + id
                    + (!TextUtils.isEmpty(selection) ? "AND (" + selection + ")" : "");
                break;

            default:
                throw new IllegalArgumentException("Unknown Uri: " + uri);
        }
        return db.update(PInfoDatabase.Tables.PInfo, values, selectionCriteria, selectionArgs);
    }
}
以及logcat中提到的另一类:

public class AddActivity extends FragmentActivity{

    private final String LOG_TAG = AddActivity.class.getSimpleName();
    private TextView mWebsiteTextView, mEmailTextView, mUsernameTextView, mPasswordTextView;
    private Button mButton;
    private ContentResolver mContentResolver;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_edit);
        getActionBar().setDisplayHomeAsUpEnabled(true); //returns up one level rather than back to the top level

        mWebsiteTextView = (TextView) findViewById(R.id.pinfoWebsite);
        mEmailTextView = (TextView) findViewById(R.id.pinfoEmail);
        mUsernameTextView= (TextView) findViewById(R.id.pinfoUsername);
        mPasswordTextView = (TextView) findViewById(R.id.pinfoPassword);

        mContentResolver = AddActivity.this.getContentResolver();

        mButton = (Button) findViewById(R.id.saveButton);
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(isValid()){
                    ContentValues values = new ContentValues(); //Content Values Class is used to store a set of values that the ContentResolver can process.
                    values.put(PInfoContract.PInfoColumns.PINFO_WEBSITE, mWebsiteTextView.getText().toString()); //.put add values ot the set & .toString() to convert it to the right format
                    values.put(PInfoContract.PInfoColumns.PINFO_EMAIL, mEmailTextView.getText().toString());
                    values.put(PInfoContract.PInfoColumns.PINFO_USERNAME, mUsernameTextView.getText().toString());
                    values.put(PInfoContract.PInfoColumns.PINFO_PASSWORD, mPasswordTextView.getText().toString());

                    Uri returned = mContentResolver.insert(PInfoContract.URI_TABLE, values);
                    Log.d(LOG_TAG, "record id returned is; " + returned);
                    Intent intent = new Intent(AddActivity.this, MainActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intent);
                    finish(); //always use this when the activity's process is finished
                }else {
                    Toast.makeText(getApplicationContext(), "Please make sure you have inputted valid data.", Toast.LENGTH_LONG).show();
                }
            }
        });
    }

    private boolean isValid(){ //You can have this as complex as you want. Like checking other databases or number of charas.
        if(mWebsiteTextView.getText().toString().length() == 0 ||
                mEmailTextView.getText().toString().length() == 0 ||
                mUsernameTextView.getText().toString().length() == 0 ||
                mPasswordTextView.getText().toString().length() == 0 ){
            return false;
        }
        return true;
    }

    private boolean someDataEntered(){
        if(mWebsiteTextView.getText().toString().length() > 0 ||
                mEmailTextView.getText().toString().length() > 0 ||
                mUsernameTextView.getText().toString().length() > 0 ||
                mPasswordTextView.getText().toString().length() > 0 ){
            return true;
        }
        return false;
    }

    @Override
    public void onBackPressed() {
        if(someDataEntered()){
            PInfoDialog dialog = new PInfoDialog();
            Bundle args = new Bundle();
            args.putString(PInfoDialog.DIALOG_TYPE, PInfoDialog.CONFIRM_EXIT); //This method places a text string in the virtual screen at the specified location.
            dialog.setArguments(args);
            dialog.show(getSupportFragmentManager(), "confirm-exit");
        } else{
            super.onBackPressed(); //if someDataEntered() is false just do what usually happnes.
        }
    }
}
日志:

06-23 16:03:14.117 21940-21940/com.example.saleh.findmypassword E/ActivityThread﹕ 未能找到com.example.saleh.findmypassword.PInfoProvider的提供程序信息 06-23 16:03:14.121 21940-21940/com.example.saleh.findmypassword W/dalvikvm﹕ threadid=1:线程以未捕获异常退出(组=0xa4c95b20) 06-23 16:03:14.121 21940-21940/com.example.saleh.findmypassword E/AndroidRuntime﹕ 致命异常:主 进程:com.example.saleh.findmypassword,PID:21940 java.lang.IllegalArgumentException:未知URLcontent://com.example.saleh.findmypassword.PInfoProvider/pinfos 位于android.content.ContentResolver.insert(ContentResolver.java:1186) 在com.example.saleh.findmypassword.AddActivity$1.onClick(AddActivity.java:51)上 在android.view.view.performClick上(view.java:4438) 在android.view.view$PerformClick.run(view.java:18422) 位于android.os.Handler.handleCallback(Handler.java:733) 位于android.os.Handler.dispatchMessage(Handler.java:95) 位于android.os.Looper.loop(Looper.java:136) 位于android.app.ActivityThread.main(ActivityThread.java:5001) 位于java.lang.reflect.Method.Invokenactive(本机方法) 位于java.lang.reflect.Method.invoke(Method.java:515) 在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)上 位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 在dalvik.system.NativeStart.main(本机方法)

舱单:


}

由于您有此异常:

    java.lang.IllegalArgumentException: Unknown Uri: 
content://com.example.saleh.findmypassword.provider/pinfos at 
com.example.saleh.findmypassword.PInfoProvider.insert(PInfoProvider.java:97>)
检查提供商的包,它不存在(未知Uri):

必须是:

com.example.saleh.findmypassword.PInfoProvider
更新:

将变量
BASE\u CONTENT\u URI
的值更改为:

public static final Uri BASE_CONTENT_URI = Uri.parse("content://com.example.saleh.findmypassword.PInfoProvider"); 

UriMatcher
可能区分大小写,您的
Uri
中的大小写错误。我认为问题出在提供程序包中,发布您的AndroidManifest.xml我认为Uri问题请更改内容Uri名称请将Uri名称与包名(清单文件)一起检查com.example.saleh.findmypassword.provider另一个原因是为提供程序添加权限(如果缺少)添加了更多信息。更改:public static final Uri BASE\u CONTENT\u Uri=Uri.parse(“content://com.example.saleh.findmypassword.PInfoProvider");我添加了清单文件。请检查一下,我想我不应该改名字。PInfoProvider是权限为提供者时的名称。如果没有,那么请建议我做什么。是的,你是对的!名称为com.example.saleh.findmypassword.PInfoProvider,但在代码的某些部分,您使用com.example.saleh.findmypassword.provider作为提供程序的名称。是否有方法知道确切位置?将在一秒钟内发布Uri构建的类
com.example.saleh.findmypassword.provider
com.example.saleh.findmypassword.PInfoProvider
public static final Uri BASE_CONTENT_URI = Uri.parse("content://com.example.saleh.findmypassword.PInfoProvider");