我是Android的新手。启动连接打开SQLite DB时出现问题。基本上,它的连接打开是从Android Activity调用的工作。在我的场景中,我使用了 SherlockFragment 库。我的问题在那里。当我打开SherlocakFragement内部的连接时,我有以下错误。
03-03 09:12:52.320: ERROR/DatabaseUtils(2410): Writing exception to parcel
java.lang.SecurityException: Permission Denial: get/set setting for user asks to run as user -2 but is calling from user 0; this requires android.permission.INTERACT_ACROSS_USERS_FULL
at com.android.server.am.ActivityManagerService.handleIncomingUser(ActivityManagerService.java:13175)
at android.app.ActivityManager.handleIncomingUser(ActivityManager.java:2044)
at com.android.providers.settings.SettingsProvider.callFromPackage(SettingsProvider.java:615)
at android.content.ContentProvider$Transport.call(ContentProvider.java:279)
at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:273)
at android.os.Binder.execTransact(Binder.java:388)
at dalvik.system.NativeStart.run(Native Method)
03-03 09:12:52.330: ERROR/AndroidRuntime(2832): FATAL EXCEPTION: main
java.lang.NullPointerException
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
at com.frontiir.MobileAgent.DAO.SqlAccessDAO.Connection_Open(SqlAccessDAO.java:152)
at com.frontiir.MobileAgent.SyncPinFragment$1.onClick(SyncPinFragment.java:99)
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:185)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5493)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
at dalvik.system.NativeStart.main(Native Method)
03-03 09:12:52.845: ERROR/android.os.Debug(2410): !@Dumpstate > sdumpstate -k -t -z -d -o /data/log/dumpstate_app_error
03-03 09:12:53.090: ERROR/Launcher(2872): Error finding setting, default accessibility to not found: accessibility_enabled
这是我的SqlAccessDAO.java
public class SqlAccessDAO {
public static DbHelper isp_dbHelper;
private final Context ispContext;
public static SQLiteDatabase ispDatabase;
public static class DbHelper extends SQLiteOpenHelper{
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
//some code
}
@Override
public void onCreate(SQLiteDatabase db) {
//some code
);
db.execSQL("CREATE TABLE " + DATABASE_TABLE_PIN + " (" +
//some code
);
db.execSQL("CREATE TABLE " + DATABASE_TABLE_SYNCHISTORY + " (" +
//some code
);
db.execSQL("CREATE TABLE " + DATABASE_TABLE_TOPUP_HISTORY + " (" +
//some code
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//some code
}
}
public SqlAccessDAO(Context c){
ispContext = c;
}
public SqlAccessDAO Connection_Open() throws SQLException{
isp_dbHelper = new DbHelper(ispContext);
ispDatabase = isp_dbHelper.getWritableDatabase();
return this;
}
public void Connection_Close(){
isp_dbHelper.close();
}
}
当我在下面的调用中调用open_connection时,会发生上述错误。
这是我的SyncPinFragment.java
public class SyncPinFragment extends SherlockFragment implements View.OnClickListener{
SqlAccessDAO sqlaccess = new SqlAccessDAO(getSherlockActivity());
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.sync_pin_fragment, container, false);
btnclick = (Button) view.findViewById(R.id.btn_sync_pin);
btnclick.setOnClickListener(this);
return view;
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_sync_pin:
forSync();
break;
}
}
private void forSync(){
sqlaccess.Connection_Open(); //error occur in here when I breakpoint.
//to do something here
sqlaccess.Connection_Close();
}
}
以下 代码段 中的一切正常。
public class LoginActivity extends SherlockActivity{
SqlAccessDAO sqlaccess = new SqlAccessDAO(LoginActivity.this);
}
答案 0 :(得分:2)
1。 在AndroidManifest.xml中添加此权限:
<uses-permission android:name="android.permission.INTERACT_ACROSS_USERS_FULL">
2。 替换这一行:
SqlAccessDAO sqlaccess = new SqlAccessDAO(getSherlockActivity());
用这个
private SqlAccessDAO sqlaccess;
在forSync
()方法中,添加以下行
sqlaccess = new SqlAccessDAO(getActivity());