尝试获取现有数据库,房间 Android
Trying to get an existing database, Room Android
下午好。我在应用程序中创建了一个现有数据库。我想通过聊天室称呼她,但收到错误消息 IllegalArgumentException: Cannot provide null context for the database.
。
我的代码如下所示:
@Database(entities = {Series.class}, version = 1, exportSchema = false)
public abstract class AppDatabase extends RoomDatabase {
private static AppDatabase INSTANCE;
private static final String DB_NAME = "base.db";
public abstract CymbalDao cymbalDao();
public static AppDatabase getDatabase(final Context context) {
if (INSTANCE == null) {
synchronized (AppDatabase.class) {
if (INSTANCE == null) {
INSTANCE = Room.databaseBuilder(context,
AppDatabase.class, DB_NAME)
.allowMainThreadQueries()
.build();
}
}
}
return INSTANCE;
}
}
数据库位置:
..\src\main\assets\base.db
我想知道我做错了什么,也许能听到一些提示,谢谢!
片段调用:
public class CymbalsFragment extends Fragment {
List<CymbalSeries> cymbalSeries = new ArrayList<>();
CymbalsAdapter cymbalsAdapter;
final AppDatabase db = AppDatabase.getDatabase(getContext());
final CymbalDao cymbalDao = db.cymbalDao();
Logcat:
08-15 15:46:23.247 31769-31769/ru.alexsuvorov.paistewiki W/System.err:
java.lang.IllegalArgumentException: Cannot provide null context for the
database.
08-15 15:46:23.248 31769-31769/ru.alexsuvorov.paistewiki W/System.err: at android.arch.persistence.room.RoomDatabase$Builder.build(RoomDatabase.java:600)
at ru.alexsuvorov.paistewiki.db.AppDatabase.getDatabase(AppDatabase.java:25)
at ru.alexsuvorov.paistewiki.fragments.CymbalsFragment.<init>(CymbalsFragment.java:29)
at java.lang.Class.newInstance(Native Method)
at ru.alexsuvorov.paistewiki.StartDrawer.onNavigationItemSelected(StartDrawer.java:70)
at android.support.design.widget.NavigationView.onMenuItemSelected(NavigationView.java:154)
at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:822)
at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:171)
at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:973)
at
android.support.design.internal.NavigationMenuPresenter.onClick(NavigationMenuPresenter.java:342)
final AppDatabase db = AppDatabase.getDatabase(getContext());
此行正在重新调整上下文 null。上下文尚未附加到片段。如果您在片段的构造函数中调用 getContext() ,就会发生这种情况。在 onCreateView 或 onViewCreated() 中调用 getContext()。
示例代码
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
Context context = getContext();
final AppDatabase db = AppDatabase.getDatabase(context);
return inflater.inflate(R.layout.fragment_profile, container, false);
}
下午好。我在应用程序中创建了一个现有数据库。我想通过聊天室称呼她,但收到错误消息 IllegalArgumentException: Cannot provide null context for the database.
。
我的代码如下所示:
@Database(entities = {Series.class}, version = 1, exportSchema = false)
public abstract class AppDatabase extends RoomDatabase {
private static AppDatabase INSTANCE;
private static final String DB_NAME = "base.db";
public abstract CymbalDao cymbalDao();
public static AppDatabase getDatabase(final Context context) {
if (INSTANCE == null) {
synchronized (AppDatabase.class) {
if (INSTANCE == null) {
INSTANCE = Room.databaseBuilder(context,
AppDatabase.class, DB_NAME)
.allowMainThreadQueries()
.build();
}
}
}
return INSTANCE;
}
}
数据库位置:
..\src\main\assets\base.db
我想知道我做错了什么,也许能听到一些提示,谢谢!
片段调用:
public class CymbalsFragment extends Fragment {
List<CymbalSeries> cymbalSeries = new ArrayList<>();
CymbalsAdapter cymbalsAdapter;
final AppDatabase db = AppDatabase.getDatabase(getContext());
final CymbalDao cymbalDao = db.cymbalDao();
Logcat:
08-15 15:46:23.247 31769-31769/ru.alexsuvorov.paistewiki W/System.err:
java.lang.IllegalArgumentException: Cannot provide null context for the
database.
08-15 15:46:23.248 31769-31769/ru.alexsuvorov.paistewiki W/System.err: at android.arch.persistence.room.RoomDatabase$Builder.build(RoomDatabase.java:600)
at ru.alexsuvorov.paistewiki.db.AppDatabase.getDatabase(AppDatabase.java:25)
at ru.alexsuvorov.paistewiki.fragments.CymbalsFragment.<init>(CymbalsFragment.java:29)
at java.lang.Class.newInstance(Native Method)
at ru.alexsuvorov.paistewiki.StartDrawer.onNavigationItemSelected(StartDrawer.java:70)
at android.support.design.widget.NavigationView.onMenuItemSelected(NavigationView.java:154)
at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:822)
at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:171)
at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:973)
at
android.support.design.internal.NavigationMenuPresenter.onClick(NavigationMenuPresenter.java:342)
final AppDatabase db = AppDatabase.getDatabase(getContext());
此行正在重新调整上下文 null。上下文尚未附加到片段。如果您在片段的构造函数中调用 getContext() ,就会发生这种情况。在 onCreateView 或 onViewCreated() 中调用 getContext()。
示例代码
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
Context context = getContext();
final AppDatabase db = AppDatabase.getDatabase(context);
return inflater.inflate(R.layout.fragment_profile, container, false);
}