Jake Wharton在最近的一次演讲中提到了这个库,它看起来是避免大量样板的好方法,所以我试了一下。但没有任何成功。 https://github.com/SimonVT/schematic
下面是附加了注释的内容提供程序的定义以及清单提供程序元素。问题是Android Studio不喜欢提供程序定义,因为内容提供程序类不扩展ContentProvider。
Caused by: java.lang.ClassCastException: com.myapp.SchematicContentProvider
cannot be cast to android.content.ContentProvider
我错过了什么?它可能与我没有使用的android-apt有关(Schematic推荐它但似乎并不需要它) - 当我尝试使用android-apt时,我得到一个VerifyError,所以不得不从构建中删除它。
的AndroidManifest.xml
<provider
android:name="com.myapp.SchematicContentProvider"
android:authorities="com.myapp.provider"
android:exported="false" />
和班级定义:
import net.simonvt.schematic.annotation.ContentProvider;
import net.simonvt.schematic.annotation.ContentUri;
import net.simonvt.schematic.annotation.TableEndpoint;
@ContentProvider(authority = SchematicContentProvider.AUTHORITY, database = SchematicDatabase.class)
public class SchematicContentProvider {
public static final String AUTHORITY = "com.myapp.provider";
interface Path {
String ROUTES = "routes";
}
@TableEndpoint(table = SchematicDatabase.ROUTES) public static class Routes {
@ContentUri(path = Path.ROUTES, type = "vnd.android.cursor.dir/list", defaultSort = SchematicRouteColumns.TITLE + " ASC")
public static final Uri ROUTES = Uri.parse("content://" + AUTHORITY + "/" + Path.ROUTES );
}
}
我已经浏览了示意图示例应用程序(自述文件中的代码段是部分的)但我无法看到我错过的内容。我不确定如何确认代码生成是否正常,我该如何检查?我在构建下查看,但我只在Schematic包名下看到BuildConfig。
很遗憾它不适合我,它有很大的潜力。
答案 0 :(得分:4)
您收到此错误是因为com.myapp.SchematicContentProvider
是带注释的类,而不是生成的ContentProvider
(具有相同的名称)。
Louis Cognault提供了正确答案,但值得一提的是,Schematic对packageName
和@ContentProvider
注释有一个特殊参数@Database
。 packageName
定义生成的类的放置位置。它让你澄清AndroidManifest.xml
的创作。
提供商定义类:
@ContentProvider(
authority = SchematicContentProvider.AUTHORITY,
database = SchematicDatabase.class,
packageName = "com.myapp.providerpackage")
public class SchematicContentProvider {
...
}
数据库定义类:
@Database(
version = SchematicDatabase.VERSION,
packageName = "com.myapp.providerpackage"
)
public class SchematicDatabase{
public static final int VERSION = 1;
...
}
的AndroidManifest.xml:
<provider
android:name="com.myapp.providerpackage.SchematicContentProvider"
android:authorities="com.myapp.provider"
android:exported="false" />
答案 1 :(得分:3)
您没有声明正确的ContentProvider。 您必须在Manifest中声明生成的那个。 我想这样:
<provider
android:name=".yourOptionalPackage.generated.SchematicContentProvider"
android:authorities="com.myapp.provider"
android:exported="false" />
如果您的IDE(Android Studio / IntelliJ)显示红色警告,只需单击Make Project按钮即可生成代码。
如果它仍然不起作用,请在项目中包含apt-libs(值得),为了节省更多时间,请使用基于apt-libs的this awesome library;)
如果我解决了您的问题,请在评论中告诉我,以及您是否需要帮助配置您的gradle文件。