Kotlin:找不到符号 class 片段或其他 android classes

Kotlin : Cannot find symbol class Fragment or other android classes

我有一个 java 片段,里面有 viewPager..

public class FragmentWithViewPager extends Fragment {

    private class ViewPagerAdapter extends FragmentStatePagerAdapter {

        ViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int i) {
            Fragment fragment = new DeshFalView(); //<-- Problem here
            Bundle args = new Bundle();
            args.putInt("index", i);
            fragment.setArguments(args);
            return fragment;
        }

    }
}

现在我有另一个片段,它将填充在上面的片段中,并像这样用 kotlin 编写:

class DeshFalView : Fragment(), DeshfalContract.View {
    //More code...
}

我没有收到任何 lint 警告或错误,但是当我尝试 运行 应用程序时:

但是我得到一个错误:

Error:(79, 37) 错误:找不到符号 class DeshFalView

在上面突出显示的行中.. 这两个 classes 在同一个包中,如下所示

提前致谢...

问题:我犯了一个愚蠢的错误,我忘记了apply kotlin-android 插件

解决方法:appgradle文件上方粘贴:

apply plugin: 'kotlin-android'

有时我会遇到这种错误,即使是这样:

apply plugin: 'kotlin-android'

在我的应用中 gradle。

解决方法是删除app/build目录。此文件夹是自动生成的,所以删除它不会有问题。

希望这会对某人有所帮助。