我试图在Android的TabbedFragment中使用数据库(从SqliteOpenHelper扩展)。但是我遇到了StackOverFlow错误和TransactionTooLarge异常,这是我的logcat:
`--------- beginning of crash
com.wen.databaseinfragment E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.wen.databaseinfragment, PID: 6566
java.lang.StackOverflowError: stack size 8MB
at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5901)
at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java
com.wen.databaseinfragment D/Error: ERR: exClass=java.lang.StackOverflowError
com.wen.databaseinfragment D/Error: ERR: exMsg=stack size 8MB
com.wen.databaseinfragment D/Error: ERR: file=ViewGroup.java
com.wen.databaseinfragment D/Error: ERR: class=android.view.ViewGroup
com.wen.databaseinfragment D/Error: ERR: method=jumpDrawablesToCurrentState line=5901
D/Error: ERR: stack=java.lang.StackOverflowError: stack size 8MB
at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5901)
at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5905)
at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewG
D/Error: ERR: TOTAL BYTES WRITTEN: 13245704
E/JavaBinder: !!! FAILED BINDER TRANSACTION !!!
E/AndroidRuntime: Error reporting crash
android.os.TransactionTooLargeException
at android.os.BinderProxy.transactNative(Native Method)
at android.os.BinderProxy.transact(Binder.java:496)
at android.app.ActivityManagerProxy.handleApplicationCrash(ActivityManagerNative.java:4164)
at com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:89)
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)'
` 这是我的片段类:
public class dataBaseFragment extends Fragment {
private static final String TAG = "DataBaseFragment";
private TextView tv;
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v= inflater.inflate(R.layout.data_base_fragment,container,true);
Database db= new Database(getActivity().getApplicationContext());
SQLiteDatabase dbWrite= db.getWritableDatabase();
tv= (TextView) v.findViewById(R.id.tv);
tv.setText("Ciaoooooooo");
Log.d(TAG, "onCreateView: ");
ContentValues cv= new ContentValues();
cv.put("name","pippo");
cv.put("sex","maschio");
dbWrite.insert("user",null,cv);
cv= new ContentValues();
cv.put("name","tizia");
cv.put("sex","femmina");
dbWrite.insert("user",null,cv);
db.close();
SQLiteDatabase dbRead = db.getReadableDatabase();
Cursor cursor= dbRead.query("user",new String[]{"name","sex"},"name=?",new String[]{"pippo"},null,null,null);
while(cursor.moveToNext())
{
String name= cursor.getString(cursor.getColumnIndex("name"));
String sex =cursor.getString(cursor.getColumnIndex("sex"));
Log.d(TAG,String.format("name=%s, sex=%s",name,sex ));
}
return v;
}
}
这是我的Sqlite Openhelper类:
public class Database extends SQLiteOpenHelper {
public Database(Context context) {
super(context, "db", null,1);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE user(" +
"_id INTEGER PRIMARY KEY AUTOINCREMENT," +
"name TEXT DEFAULT \"\"," +
"sex TEXT DEFAULT \"\")");
}
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
答案 0 :(得分:0)
如果您没有附加膨胀的视图会发生什么:
View v= inflater.inflate(R.layout.data_base_fragment,container,false);
您不应该自己将片段的视图附加到容器,尽管它可能不是唯一的问题(抛出的异常将是IllegalStateException
)