当我尝试使用房间显示来自数据库sqllite的日期的RecyclerView时出现错误。但是我不知道为什么这个错误不会在任何时候出现。
E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
Process: corporate.foody.it.myapplication, PID: 20161
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:353)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
at java.util.concurrent.FutureTask.run(FutureTask.java:271)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:7313)
at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:1161)
at android.view.View.requestLayout(View.java:21995)
at android.view.View.requestLayout(View.java:21995)
at android.view.View.requestLayout(View.java:21995)
at android.view.View.requestLayout(View.java:21995)
at android.view.View.requestLayout(View.java:21995)
at android.view.View.requestLayout(View.java:21995)
at android.support.constraint.ConstraintLayout.requestLayout(ConstraintLayout.java:3172)
at android.view.View.requestLayout(View.java:21995)
at android.support.v7.widget.RecyclerView.requestLayout(RecyclerView.java:4090)
at android.support.v7.widget.RecyclerView.setAdapter(RecyclerView.java:1085)
at corporate.foody.it.myapplication.RistoranteFragment$1.doInBackground(RistoranteFragment.java:75)
at corporate.foody.it.myapplication.RistoranteFragment$1.doInBackground(RistoranteFragment.java:72)
at android.os.AsyncTask$2.call(AsyncTask.java:333)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
这是我的代码: DatabaseFoody.class
@Database(
entities = {Ristorante.class},
version = 2)
public abstract class DatabaseFoody extends RoomDatabase {
private static DatabaseFoody database;
public abstract RistoranteDao getRistoranteDao();
public static DatabaseFoody getIstance(Context context){
if (database == null){
database = Room.databaseBuilder(
context,
DatabaseFoody.class,
"db-foody.db").build();
}
我尝试删除AsyncTask,但问题仍然存在。在doInBackground()中是正确的插入代码? RistoranteFragment.class
public class RistoranteFragment extends Fragment {
// TODO: Customize parameter argument names
private static final String ARG_COLUMN_COUNT = "column-count";
// TODO: Customize parameters
private int mColumnCount = 1;
private OnListFragmentInteractionListener mListener;
public RistoranteFragment() {
}
// TODO: Customize parameter initialization
@SuppressWarnings("unused")
public static RistoranteFragment newInstance(int columnCount) {
RistoranteFragment fragment = new RistoranteFragment();
Bundle args = new Bundle();
args.putInt(ARG_COLUMN_COUNT, columnCount);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_ristorante_list, container, false);
// Set the adapter
if (view instanceof RecyclerView) {
Context context = view.getContext();
final RecyclerView recyclerView = (RecyclerView) view;
if (mColumnCount <= 1) {
recyclerView.setLayoutManager(new LinearLayoutManager(context));
} else {
recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount));
}
new AsyncTask<Void,Void,Void>(){
@Override
protected Void doInBackground(Void... voids) {
recyclerView.setAdapter(new RistoranteRecyclerViewAdapter
(DatabaseFoody.getIstance(getContext()).getRistoranteDao().TrovaTutti(),
mListener));
return null;
}
}.execute();
}
return view;
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnListFragmentInteractionListener {
// TODO: Update argument type and name
void onListFragmentInteraction(Ristorante item);
}
}
谢谢