简体   繁体   English

Realm.init()无法解析方法init在Application onCreate中不起作用?

[英]Realm.init() cannot resolve method init not working in Application onCreate?

Can't call init() method on Realm. 无法在Realm上调用init()方法。 Any ideas what I'm doing wrong? 有什么想法我做错了吗?

在此处输入图片说明

I have added the dependency in my build.gradle(project): 我在build.gradle(project)中添加了依赖项:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.1'
        classpath "io.realm:realm-gradle-plugin:3.2.0"

    }
}

and applied the plugin in the build.gradle(app) 并在build.gradle(app)中应用了插件

apply plugin: 'com.android.application'
apply plugin: 'realm-android'

So I made it work with Realm.init(MyApp.this) . 因此,我将其与Realm.init(MyApp.this) I think the issue is you have the dependencies inside the build.gradle of the whole project. 我认为问题在于您在整个项目的build.gradle中具有依赖项。 This: 这个:

 // Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
repositories {
    mavenCentral()
    jcenter()
}
dependencies {
    classpath 'com.android.tools.build:gradle:2.3.2'
    classpath 'com.google.gms:google-services:3.0.0'
}
}

allprojects {
repositories {
    mavenCentral()
}
}

When you should have it inside the build.gradle of your app: Like this: 当您应该将其包含在应用程序的build.gradle中时:

buildscript {
repositories {
    maven { url 'https://maven.fabric.io/public' }
    maven { url 'http://dl.bintray.com/amulyakhare/maven' }
    maven { url 'https://jcenter.bintray.com/' }
    maven { url "https://jitpack.io" }
}

dependencies {
    classpath 'io.fabric.tools:gradle:1.+'
    classpath "io.realm:realm-gradle-plugin:3.2.0"
}
}
apply plugin: 'com.android.application'
apply plugin: 'io.fabric'
apply plugin: 'realm-android'

repositories {
maven { url 'https://maven.fabric.io/public' }
maven { url 'http://dl.bintray.com/amulyakhare/maven' }
maven { url 'https://jcenter.bintray.com/' }
maven { url "https://jitpack.io" }
}


android {
compileSdkVersion 23
buildToolsVersion '25.0.0'
defaultConfig {
    applicationId "nl.hgrams.passenger"
    minSdkVersion 17
    targetSdkVersion 23
    versionCode 900
    versionName '2.8.2'
    multiDexEnabled true
}
...
}

It seems like a lagg at Android Studio indexes system. Android Studio索引系统似乎滞后 Try next solutions: 尝试下一个解决方案:

  1. Sync gradle files 同步gradle文件 在此处输入图片说明
  2. Invalidate IDE indexes and restart 使IDE索引无效并重新启动
    在此处输入图片说明
  3. And the last assumption - move io.realm:realm-gradle-plugin:3.2.0 dependency from project build.gradle file to build.gradle from app and define own buildscript section: 最后一个假设-将io.realm:realm-gradle-plugin:3.2.0依赖项从项目build.gradle文件移至app.build.gradle并定义自己的buildscript部分:

     buildscript { repositories { jcenter() } dependencies { classpath 'io.realm:realm-gradle-plugin:3.2.0' } } 

    Autogenerated build.gradle for project contains next note about this: 项目的自动生成的build.gradle包含有关此的下一条注释:

    // NOTE: Do not place your application dependencies here; //注意:请勿在此处放置应用程序依赖项; they belong 他们属于
    // in the individual module build.gradle files //在各个模块的build.gradle文件中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Realm - Realm.init(this) 在 onCreate 中失败? - Realm - Realm.init(this) failed in onCreate? 迁移之前或之后的Realm.init - Realm.init before or after migration 错误是“在创建 RealmConfiguration 之前调用 `Realm.init(Context)`” - The error is “Call `Realm.init(Context)` before creating a RealmConfiguration” 创建领域配置之前,领域数据库/应用程序在启动时崩溃/调用realm.init(context) - Realm Database / App crashes in start up / call realm.init(context) before creating a realm configuration Realm数据库的工具测试在创建Realmconfiguration之前给出了“Call'Franm.init(Context)'”即使我称它为 - Instrumental test for Realm database gives “Call 'Realm.init(Context)' before creating Realmconfiguration” even though I call it 领域方法“无法解析符号” - Realm method “Cannot Resolve Symbol” 哪个是在片段onCreateView或onCreate方法中初始化ViewModel的最佳位置? - Which is the best place to init ViewModel in fragment onCreateView or onCreate Method? 安卓 可以在onCreate()生命周期方法之外初始化对象吗? - Android. It's okay to init objects outside of onCreate() lifecycle method? 在OnCreate中定义按钮-无法解析方法中的符号 - Defining Button in OnCreate - Cannot resolve symbol in method 无法解析子类中的方法onCreate() - Cannot resolve method onCreate() in sub class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM