在 support:appcompat-v7 版本 23 + 中使用 AndroidAnnotations 时应用崩溃

Crash app when using AndroidAnnotations with support:appcompat-v7 version 23 +

我正在使用最新的 AndroidAnnotations 4.1.0(link) 并尝试将 appcompat 更改为 24.2.1 以支持 targetVersion 24。但是,我的应用程序在使用 appcompat-v7 版本 23+ 时崩溃,但是,它在 appcompat-v7:22.2.0 上运行正常。任何建议都适用。以下是我的错误日志:

    FATAL EXCEPTION: main
  Process: ABC, PID: 1275
  java.lang.RuntimeException: Unable to start activity ComponentInfo{MyPackage/MyPackage.activities.MainActivity_}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2237)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2286)
      at android.app.ActivityThread.access0(ActivityThread.java:144)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246)
      at android.os.Handler.dispatchMessage(Handler.java:102)
      at android.os.Looper.loop(Looper.java:212)
      at android.app.ActivityThread.main(ActivityThread.java:5137)
      at java.lang.reflect.Method.invokeNative(Native Method)
      at java.lang.reflect.Method.invoke(Method.java:515)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:718)
      at dalvik.system.NativeStart.main(Native Method)
   Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
      at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
      at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
      at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
      at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
      at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
      at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284)
      at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
      at MyPackage.activities.MainActivity_.setContentView(MainActivity_.java:51)
      at MyPackage.activities.MainActivity_.onCreate(MainActivity_.java:40)
      at android.app.Activity.performCreate(Activity.java:5231)
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2201)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2286) 
      at android.app.ActivityThread.access0(ActivityThread.java:144) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:212) 
      at android.app.ActivityThread.main(ActivityThread.java:5137) 
      at java.lang.reflect.Method.invokeNative(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:515) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:718) 
      at dalvik.system.NativeStart.main(Native Method) 
   Caused by: java.lang.IllegalArgumentException: Binary XML file line #7: Must specify unique android:id, android:tag, or have a parent with an id for MyPackage.fragments.MainFragment_
      at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2387)
      at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:120)
      at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:376)
      at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:33)
      at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:75)
      at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
      at android.view.LayoutInflater.rInflate(LayoutInflater.java:755) 
      at android.view.LayoutInflater.inflate(LayoutInflater.java:492) 
      at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 
      at android.view.LayoutInflater.inflate(LayoutInflater.java:353) 
      at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284) 
      at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
      at MyPackage.activities.MainActivity_.setContentView(MainActivity_.java:51) 
      at MyPackage.activities.MainActivity_.onCreate(MainActivity_.java:40) 
      at android.app.Activity.performCreate(Activity.java:5231) 
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2201) 
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2286) 
      at android.app.ActivityThread.access0(ActivityThread.java:144) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:212) 
      at android.app.ActivityThread.main(ActivityThread.java:5137) 
      at java.lang.reflect.Method.invokeNative(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:515) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:718) 
      at dalvik.system.NativeStart.main(Native Method) 

这是我的 gradle

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

def PACKAGE_NAME = "MyPackage"
def VERSION_CODE = 1
def VERSION_NAME = "1.0.0"

def AAVersion = '4.1.0'

tasks.withType(JavaCompile) {
  options.encoding = 'UTF-8'
}

buildscript {
  repositories {
    mavenCentral()
    ...
  }
  dependencies {
    // replace with the current version of the Android plugin
    classpath 'com.android.tools.build:gradle:2.2.0'
    // replace with the current version of the android-apt plugin
    classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
    ...
  }
}

apt {
  arguments {
    androidManifestFile variant.outputs[0].processResources.manifestFile
    resourcePackageName PACKAGE_NAME
  }
}

repositories {
  mavenCentral()
  ...
}

android {
  compileSdkVersion 24
  buildToolsVersion "24.0.2"

  defaultConfig {
    applicationId PACKAGE_NAME
    minSdkVersion 14
    targetSdkVersion 24
    versionCode VERSION_CODE
    versionName VERSION_NAME

    // Enabling multidex support.
    multiDexEnabled true
  }

  signingConfigs {
    ...
  }

  buildTypes {
    release {
      minifyEnabled true
      proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
      signingConfig signingConfigs.release
    }
  }

  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }

  packagingOptions {
    ...
  }

  dexOptions {
    ...
  }

  ...
}

dependencies {
  apt "org.androidannotations:androidannotations:$AAVersion"
  compile "org.androidannotations:androidannotations-api:$AAVersion"
  ...
  compile 'com.android.support:appcompat-v7:24.2.1'
  compile 'com.google.android.gms:play-services-maps:9.6.1'
  compile 'com.google.android.gms:play-services-location:9.6.1'
  compile 'com.google.android.gms:play-services-gcm:9.6.1'
  compile 'com.squareup.retrofit2:retrofit:2.1.0'
  compile 'com.squareup.retrofit2:converter-gson:2.1.0'
  compile 'com.squareup.retrofit2:converter-jackson:2.1.0'
  compile 'com.squareup.retrofit2:adapter-rxjava:2.0.0'
  compile('com.squareup.okhttp3:okhttp:3.2.0') {
    exclude module: 'okhttp'
  }
  compile 'io.reactivex:rxandroid:1.0.1'
  compile 'com.squareup.okhttp3:logging-interceptor:3.2.0'
  compile 'com.github.mrmike:ok2curl:0.2.3'
  compile 'de.greenrobot:eventbus:2.4.0'
  compile 'com.android.support:multidex:1.0.1'
  compile('com.crashlytics.sdk.android:crashlytics:2.6.2@aar') {
    transitive = true;
  }
}

repositories {
  flatDir {
    dirs 'libs'
  }
}

因为它是从堆栈跟踪中准备好的,问题是您没有在 xml 布局中为您的 Fragment 添加任何 ID 和标签,您在其中声明了一个实例。有问题的片段是 MyPackage.fragments.MainFragment_。这是一个标准错误,与 AndroidAnnotations 无关。

<fragment android:name="MyPackage.fragments.MainFragment_"
              android:id="@+id/fragment_id" <!-- Add an ID -->
              android:layout_width="match_parent"
              android:layout_height="match_parent" />