例外-将例外写入包裹 [英] Exception- Writing exception to parcel
本文介绍了例外-将例外写入包裹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是将简单的文本数据插入sqlite数据库的一项小任务.我为此使用了SQLiteOpenHelper类,并使用了给定的代码.我在这里找到了一些类似的问题,但不完全相同.请帮帮我.这是例外-
This is a small assignment to insert simple text data to sqlite database. I have used SQLiteOpenHelper class for this and used given code. I've found some similiar questions here but not the exact same. Please help me out. Here is the exception-
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文件中使用了以下权限:
I've used following permissions in my AndroidManifest.XML file:
<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类-
this is my DBHelper class-
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);
}
}
推荐答案
您需要询问运行时权限.对于较新的版本,添加清单是不够的.
You need to ask runtime permissions. Adding manifest is not enough for newer versions.
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.
}
}
这篇关于例外-将例外写入包裹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文