gpt4 book ai didi

java - SherlockFragment 内的 SQLite 连接打开错误

转载 作者:行者123 更新时间:2023-12-02 06:07:47 25 4
gpt4 key购买 nike

我是 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();
}
}

以下代码 fragment 中一切正常。

public class LoginActivity extends SherlockActivity{
SqlAccessDAO sqlaccess = new SqlAccessDAO(LoginActivity.this);
}

最佳答案

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

关于java - SherlockFragment 内的 SQLite 连接打开错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22137947/

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