我使用4.46版本的核心ORMLite和Android,我一直有这个问题。
Foo class
@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 class
@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 examples code page相同,但使用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)
如果我尝试获取queryForAll()(返回列表)并且我之前没有保存数据,则会得到NullPointerException而不是空List或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)
答案 0 :(得分:2)
如果您没有字段错误,这将有助于
我从这里回答:ORMLite error no databasefield annotations exists
提供解决方案可能已经晚了,但这是我的解决方案:
你看到proguard尝试混淆代码,如果你读深度或介绍proguard
http://proguard.sourceforge.net/FAQ.html
-keep class com.j256.**<br>
-keepclassmembers class com.j256.** { *; }<br>
-keep enum com.j256.**<br>
-keepclassmembers enum com.j256.** { *; }<br>
-keep interface com.j256.**<br>
-keepclassmembers interface com.j256.** { *; }
-keepclassmembers class classpath.** {
public *;
}
答案 1 :(得分:1)
最后我认为这是DatabaseConfigUtil的问题。由于某些未知原因,它在原始文件夹中生成的旨在提高Android性能的文件未正确更新。
这只发生在我从Eclipse运行类时,如果我从终端运行,一切正常。我不知道为什么这种奇怪的行为。现在我总是从终端运行它。