gpt4 book ai didi

java - 异常-将异常写入包裹

转载 作者:行者123 更新时间:2023-12-02 12:00:27 26 4
gpt4 key购买 nike

这是一个向 sqlite 数据库插入简单文本数据的小作业。我为此使用了 SQLiteOpenHelper 类并使用了给定的代码。我在这里发现了一些类似的问题,但不完全相同。请帮帮我。这是异常(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 文件中使用了以下权限:

<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);
}

}

最佳答案

您需要询问runtime permissions.对于新版本来说,添加 list 是不够的。

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.
}
}

关于java - 异常-将异常写入包裹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47292505/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com