2017-11-14 2 views
0

Dies ist eine kleine Aufgabe zum Einfügen von einfachen Textdaten in SQLite-Datenbank. Ich habe die SQLiteOpenHelper-Klasse dafür verwendet und den angegebenen Code verwendet. Ich habe hier einige ähnliche Fragen gefunden, aber nicht die gleichen. Bitte hilf mir. Hier ist die Ausnahme-Ausnahme- Schreiben Ausnahme für Paket

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) 

ich in meiner AndroidManifest.xml Datei folgenden Berechtigungen verwendet haben:

<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" /> 

das ist mein DBHelper klassen

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

}

+0

Sie haben eine fehlende Berechtigung – Zoe

+0

Danke @Zoe. Könnten Sie mir bitte sagen, welches? Ich habe schon viele probiert –

+1

Veröffentlichen Sie Ihr Manifest bitte –

Antwort

1

Sie müssen runtime permissions. Hinzufügen Manifest fragen für neuere nicht genügend Versionen ist.

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

Hallo @ EmreAktürk, Sorry, aber ich brauche nicht Kontakte in meiner App zu lesen. Es ist nur eine einfache App, um Daten zur SQLite-Tabelle hinzuzufügen. Ich weiß nicht, warum App diese Ausnahme zur Laufzeit zeigt. (Es stürzt nicht einmal die App) –

+0

Hallo @Wijay Sharma, Es gibt eine Klasse namens DatabaseUtils, die versucht, Kontakte zu lesen. Sie können es aus der ersten Zeile des Fehlerprotokolls verstehen. Diese Klasse sollte in einer Abhängigkeit sein, die Sie verwenden. Wenn es nicht abstürzt, ist es wahrscheinlich in try-catch-Block. –

-1

Sie müssen diese Zeile zu Ihrem Manifest hinzufügen:

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

enter image description here

+0

Bitte korrigieren Sie mich, wenn ich falsch liege. Aber es gibt keine solche Erlaubnis –

+0

Ich kopierte von Android Studio, von meinem Manifest –

+0

[Es existiert nicht] (https://developer.android.com/reference/android/Manifest.permission.html) – Zoe

Verwandte Themen