删除会生成一个exeption Java

时间:2017-10-31 17:56:18

标签: java android android-room

在应用程序中,我使用Room创建不同的表。我使用其中一个来保存用户在注册期间输入的区域,然后将这些区域添加到注册表单中的微调器中。在这个片段中,我使用了@Delete,直到今天仍然很好。我没有改变任何逻辑,甚至没有访问过这个片段。 当我注释掉代码的删除部分时,似乎房间本身工作正常。 请帮忙! 这是我的代码和日志。

@Dao
public interface RegionDao {
    @Query("SELECT * FROM Region")
    Cursor getAll();

    @Insert
    void insertAll(Region... regions);

    @Query("SELECT COUNT(*) from Region")
    int countRegions();

    @Delete
    void nukeTable(Region... regions);
}

**当exepton发生时,我首先将此代码作为删除方法作为网站上的答案之一建议,但随后根据文档建议更改它,结果是相同的

@Query("DELETE FROM Region")
    public void nukeTable();


    public class Fragment2 extends Fragment {

        AppDatabase myDB;
        .......
       @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup 
        container,
                                 Bundle savedInstanceState) {
           ..............................
            mAuth = FirebaseAuth.getInstance();
            ..
            initDB();
            myDB.regionDao().nukeTable();
            if (myDB.regionDao().countRegions() == 0) {
                addRegion();
            } 
         @Override
         public void onDestroy() {

            super.onDestroy();
            myDB.close();
        }

        private void initDB() {
            myDB = Room.databaseBuilder(getContext(),
                    AppDatabase.class, 
       "DataBase").allowMainThreadQueries().build();

        }

        public void addRegion() {
            Region newRegion1 = new Region();
            Region newRegion2 = new Region();
            Region newRegion3 = new Region();
            Region newRegion4 = new Region();
            Region newRegion5 = new Region();
            Region newRegion6 = new Region();

            newRegion1.setRegion("Alaska");
            newRegion2.setRegion("Texas");
            newRegion3.setRegion("Columbia");
            newRegion4.setRegion("Utah");
            newRegion5.setRegion("California");
            newRegion6.setRegion("Florida");

            myDB.regionDao().insertAll(newRegion1);
            myDB.regionDao().insertAll(newRegion2);
            myDB.regionDao().insertAll(newRegion3);
            myDB.regionDao().insertAll(newRegion4);
            myDB.regionDao().insertAll(newRegion5);
            myDB.regionDao().insertAll(newRegion6);
        }

LOG:

 java.lang.NoClassDefFoundError: Failed resolution of: Landroid/arch/core/executor/AppToolkitTaskExecutor;
                                                                        at android.arch.persistence.room.InvalidationTracker.refreshVersionsAsync(InvalidationTracker.java:403)
                                                                        at android.arch.persistence.room.RoomDatabase.endTransaction(RoomDatabase.java:219)
                                                                        at com.anastacia.test_2.room.RegionDao_Impl.nukeTable(RegionDao_Impl.java:70)
                                                                        at com.anastacia.test_2.Fragments.Fragment2.onCreateView(Fragment2.java:74)
                                                                        at android.support.v4.app.Fragment.performCreateView(Fragment.java:2354)
                                                                        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1419)
                                                                        at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1740)
                                                                        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1809)
                                                                        at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:799)
                                                                        at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2580)
                                                                        at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2367)
                                                                        at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2322)
                                                                        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2229)
                                                                        at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:700)
                                                                        at android.os.Handler.handleCallback(Handler.java:815)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:104)
                                                                        at android.os.Looper.loop(Looper.java:207)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5791)
                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)
                                                                     Caused by: java.lang.ClassNotFoundException: Didn't find class "android.arch.core.executor.AppToolkitTaskExecutor" on path: DexPathList[[zip file "/data/app/com.anastacia.test_2-2/base.apk", zip file "/data/app/com.anastacia.test_2-2/split_lib_dependencies_apk.apk", zip file "/data/app/com.anastacia.test_2-2/split_lib_slice_0_apk.apk", zip file "/data/app/com.anastacia.test_2-2/split_lib_slice_1_apk.apk", zip file "/data/app/com.anastacia.test_2-2/split_lib_slice_2_apk.apk", zip file "/data/app/com.anastacia.test_2-2/split_lib_slice_3_apk.apk", zip file "/data/app/com.anastacia.test_2-2/split_lib_slice_4_apk.apk", zip file "/data/app/com.anastacia.test_2-2/split_lib_slice_5_apk.apk", zip file "/data/app/com.anastacia.test_2-2/split_lib_slice_6_apk.apk", zip file "/data/app/com.anastacia.test_2-2/split_lib_slice_7_apk.apk", zip file "/data/app/com.anastacia.test_2-2/split_lib_slice_8_apk.apk", zip file "/data/app/com.anastacia.test_2-2/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/com.anastacia.test_2-2/lib/arm64, /vendor/lib64, /system/lib64]]
                                                                        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
                                                                        at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
                                                                        at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
                                                                        at android.arch.persistence.room.InvalidationTracker.refreshVersionsAsync(InvalidationTracker.java:403) 
                                                                        at android.arch.persistence.room.RoomDatabase.endTransaction(RoomDatabase.java:219) 
                                                                        at com.anastacia.test_2.room.RegionDao_Impl.nukeTable(RegionDao_Impl.java:70) 
                                                                        at com.anastacia.test_2.Fragments.Fragment2.onCreateView(Fragment2.java:74) 
                                                                        at android.support.v4.app.Fragment.performCreateView(Fragment.java:2354) 
                                                                        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1419) 
                                                                        at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1740) 
                                                                        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1809) 
                                                                        at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:799) 
                                                                        at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2580) 
                                                                        at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2367) 
                                                                        at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2322) 
                                                                        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2229) 
                                                                        at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:700) 
                                                                        at android.os.Handler.handleCallback(Handler.java:815) 
                                                                        at android.os.Handler.dispatchMessage(Handler.java:104) 
                                                                        at android.os.Looper.loop(Looper.java:207) 
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5791) 
                                                                        at java.lang.reflect.Method.invoke(Native Method) 
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901) 
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762) 
                                                                        Suppressed: java.lang.ClassNotFoundException: android.arch.core.executor.AppToolkitTaskExecutor
                                                                        at java.lang.Class.classForName(Native Method)
                                                                        at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
                                                                        at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
                                                                        at java.lang.ClassLoader.loadClass(ClassLoader.java:504)

1 个答案:

答案 0 :(得分:1)

日志引用无法找到类&#34; android.arch.core.executor.AppToolkitTaskExecutor&#34; 这是来自&#34; android.arch.lifecycle:compiler&#34;的类,请检查您的gradle并确保所有组件依赖项都是最新的版本,即#34; android.arch.lifecycle :编译器:<强> 1.0.0-RC1 &#34;截至今天

您可以在以下超链接中检查所有组件依赖关系版本

Adding Architecture Components to your Project - Google Developers

希望它有所帮助。