例外 - 为包裹写例外

时间:2017-11-14 18:05:44

标签: java android database sqlite sqliteopenhelper

这是一个将简单文本数据插入sqlite数据库的小任务。我已经使用了SQLiteOpenHelper类并使用了给定的代码。 我在这里发现了一些类似的问题但不完全相同。请帮帮我。这是例外 -

11-14 21:59:17.786 5746-5763/? E/DatabaseUtils: Writing exception to parcel
                                            java.lang.SecurityException: Neither user 10169 nor current process has android.permission.READ_PROFILE.
                                                at android.app.ContextImpl.enforce(ContextImpl.java:1921)
                                                at android.app.ContextImpl.enforceCallingOrSelfPermission(ContextImpl.java:1950)
                                                at android.content.ContextWrapper.enforceCallingOrSelfPermission(ContextWrapper.java:600)
                                                at com.android.providers.contacts.ProfileProvider.enforceReadPermission(ProfileProvider.java:54)
                                                at com.android.providers.contacts.ProfileProvider.query(ProfileProvider.java:84)
                                                at com.android.providers.contacts.ContactsProvider2.query(ContactsProvider2.java:5066)
                                                at android.content.ContentProvider$Transport.query(ContentProvider.java:214)
                                                at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:112)
                                                at android.os.Binder.execTransact(Binder.java:446)

我在AndroidManifest.XML文件中使用了以下权限:

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.Manifest.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

这是我的DBHelper类 -

public class DBHelper extends SQLiteOpenHelper {

public static final String DATABASE_NAME = "CANDIDATES";
public static final String TABLE_NAME = "CANDIDATES_TABLE";
public static final String NAME = "NAME";
public static final String AGE = "AGE";
public static final String QUAL = "QUALIFICATION";
public static final String SALARY = "SALARY";
public static final String ADD = "ADDRESS";
public static final String DES = "DESCRIPTION";
Context context;


public DBHelper(Context context) {
    super(context, DATABASE_NAME, null, 1);
    this.context=context;
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("create table "+ TABLE_NAME +" ("+NAME+" TEXT,"+AGE+" INTEGER,"+QUAL+" TEXT,"+SALARY+" INTEGER,"+ADD+" TEXT,"+DES+" TEXT )");

}

public boolean insertData(String name,int age,String qual,int salary, String add, String des){
    SQLiteDatabase db=this.getWritableDatabase();
    ContentValues contentValues=new ContentValues();
    contentValues.put(NAME,name);
    contentValues.put(AGE,age);
    contentValues.put(QUAL,qual);
    contentValues.put(SALARY,salary);
    contentValues.put(ADD,add);
    contentValues.put(DES,des);
    long result=db.insert(TABLE_NAME,null,contentValues);
    Log.v("check",name+age+""+qual+salary+""+add+des);
    if(result==-1){
        return false;
    }
    else return true;
}

public Cursor getData(){
    SQLiteDatabase db=this.getWritableDatabase();
    Cursor cursor=db.rawQuery("Select * from"+TABLE_NAME,null);
    return cursor;
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
    onCreate(db);
}

}

3 个答案:

答案 0 :(得分:2)

您需要问runtime permissions.对于较新的版本,添加清单是不够的。

if (ContextCompat.checkSelfPermission(thisActivity,
            Manifest.permission.READ_CONTACTS)
    != PackageManager.PERMISSION_GRANTED) {

// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
        Manifest.permission.READ_CONTACTS)) {

    // Show an explanation to the user *asynchronously* -- don't block
    // this thread waiting for the user's response! After the user
    // sees the explanation, try again to request the permission.

} else {

    // No explanation needed, we can request the permission.

    ActivityCompat.requestPermissions(thisActivity,
            new String[]{Manifest.permission.READ_CONTACTS},
            MY_PERMISSIONS_REQUEST_READ_CONTACTS);

    // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
    // app-defined int constant. The callback method gets the
    // result of the request.
}
}

答案 1 :(得分:0)

我的问题通过在清单中添加此权限来解决:

android.permission.WRITE_SETTINGS

答案 2 :(得分:-1)

您需要将此行添加到清单中:

<uses-permission android:name="android.permission.READ_PROFILE"/>

enter image description here