我使用 4.46 版本的核心 ORMLite 和 Android,我一直有这个问题。
Foo 类
@DatabaseTable
public class Foo {
@DatabaseField(generatedId = true)
public int id;
@DatabaseField(index = true)
public String title;
@ForeignCollectionField
ForeignCollection<Foo2> foo2s;
public Foo() {
// needed by ormlite
}
}
Foo2 类
@DatabaseTable
public class Foo2 {
@DatabaseField(generatedId = true)
public int id;
@DatabaseField
public Date date;
public Foo2() {
// needed by ormlite
}
}
ORMLiteFragment(我需要从 Fragment 调用 getHelper())
public class OrmLiteFragment extends Fragment {
private DatabaseHelper databaseHelper = null;
protected DatabaseHelper getHelper() {
if (databaseHelper == null) {
databaseHelper =
OpenHelperManager.getHelper(getActivity(), DatabaseHelper.class);
}
return databaseHelper;
}
@Override
public void onDestroy() {
super.onDestroy();
if (databaseHelper != null) {
OpenHelperManager.releaseHelper();
databaseHelper = null;
}
}
}
活动
public class MainActivity extends FragmentActivity {
/**
* Some methods non important for this...
*/
public static class SectionFragment extends OrmLiteFragment {
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
// Some code....
getHelper().getFoo2RuntimeDao(); // HERE FIRES THE ERROR!
// Some code....
}
}
}
DatabaseHelper 类与Android 示例代码页相同,但具有 Foo2RuntimeDao() 和 Foo1RuntimeDao()
问题
09-03 14:27:15.756: E/AndroidRuntime(557): FATAL EXCEPTION: main
09-03 14:27:15.756: E/AndroidRuntime(557): java.lang.IllegalArgumentException: No fields have a DatabaseField annotation in class java.lang.Character
09-03 14:27:15.756: E/AndroidRuntime(557): at com.j256.ormlite.table.DatabaseTableConfig.extractFieldTypes(DatabaseTableConfig.java:215)
09-03 14:27:15.756: E/AndroidRuntime(557): at com.j256.ormlite.table.DatabaseTableConfig.fromClass(DatabaseTableConfig.java:146)
09-03 14:27:15.756: E/AndroidRuntime(557): at com.j256.ormlite.table.TableInfo.<init>(TableInfo.java:53)
09-03 14:27:15.756: E/AndroidRuntime(557): at com.j256.ormlite.dao.BaseDaoImpl.initialize(BaseDaoImpl.java:149)
09-03 14:27:15.756: E/AndroidRuntime(557): at com.j256.ormlite.dao.BaseDaoImpl.<init>(BaseDaoImpl.java:126)
- 更新 1
如果我尝试获取一个queryForAll()(返回一个列表)并且我之前没有保存数据,我会得到一个 NullPointerException 而不是一个空列表或 null。像这样:
public class MainActivity2 extends OrmLiteBaseActivity<DatabaseHelper> {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getHelper().getChapterRunDao(); //This Works!
getHelper().getChapterRunDao().queryForAll(); //This throws NullPointerException
}
}
问题
09-04 15:32:13.261: E/AndroidRuntime(3615): FATAL EXCEPTION: main
09-04 15:32:13.261: E/AndroidRuntime(3615): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.emanga/com.emanga.ORMLite}: java.lang.NullPointerException
09-04 15:32:13.261: E/AndroidRuntime(3615): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
09-04 15:32:13.261: E/AndroidRuntime(3615): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)