我一直在开发一个利用 android 可比性库 (android-support-v4) 的 android 应用程序。在 SDK 20 之前,我能够使用以下 uses-sdk
编译我的应用程序进入我的 list 。
<uses-sdk android:minSdkVersion="7" />
我所有为结果开始 Activity 的 fragment 都正确收到了结果。
一天(在我更新到 SDK 20 之前)我正在修复 lint 问题,我添加了
android:targetSdkVersion
根据 lint 标志的请求到 list ,我很快意识到我的 fragment 都没有收到他们的 onActivityResilt() 调用。现在,自从我更新到 SDK 20.0.1,我不得不使用
android:targetSdkVersion
在 uses-sdk
进入我的 list ;否则永远不会生成 R.java。这是我目前的
uses-sdk
list 条目<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="16" />
目前由于这个奇怪的错误,我的应用程序目前仍然被破坏。
我已经尝试将我的兼容库更新为最新版本 r9。
我还仔细检查了我所有的
startActivityForResult()
在 fragment 中调用。他们都通过 fragment 的方法使用正确的调用。即
this.startActivityForResult(intent, requestCode);
不是
this.getActivity().startActivityForResult(intent, requestCode);
我目前的开发环境:
Android SDK 20.0.1
Android SDK Platform Tools 13
ADT 20.0.2
任何帮助将不胜感激,因为这个错误目前是阻止我进行最终 beta 测试的砖墙。
编辑
这是我的
ant debug
的输出$ ant debug Buildfile: /home/ricky/Development/workspace/location_ringer/LocationRinger/build.xml
-set-mode-check:
-set-debug-files:
-check-env: [checkenv] Android SDK Tools Revision 20.0.1 [checkenv] Installed at /app/android-sdk-linux_86
-setup: [echo] Project Name: ListActivity [gettype] Project Type: Application
-set-debug-mode:
-debug-obfuscation-check:
-build-setup: [echo] Resolving Build Target for ListActivity... [gettarget] Project Target: Google APIs [gettarget] Vendor: Google Inc. [gettarget] Platform Version: 4.1 [gettarget] API level:
16 [echo] ---------- [echo] Creating output directories if needed... [echo] ---------- [echo] Resolving Dependencies for ListActivity... [dependency] Library dependencies: [dependency] [dependency] ------------------ [dependency] Ordered libraries: [dependency] [dependency] ------------------ [dependency] API<=15: Adding annotations.jar to the classpath. [echo] ---------- [echo] Building Libraries with 'debug'...nodeps:
-set-mode-check:
-set-debug-files:
-check-env: [checkenv] Android SDK Tools Revision 20.0.1 [checkenv] Installed at /app/android-sdk-linux_86
-setup: [echo] Project Name: LocationLib [gettype] Project Type: Android Library
-set-debug-mode:
-debug-obfuscation-check:
-build-setup: [echo] Resolving Build Target for LocationLib... [gettarget] Project Target: Google APIs [gettarget] Vendor: Google Inc. [gettarget] Platform Version: 4.1 [gettarget] API level:
16 [echo] ---------- [echo] Creating output directories if needed... [echo] ---------- [echo] Resolving Dependencies for LocationLib... [dependency] Library dependencies: [dependency] No Libraries [dependency] [dependency] ------------------ [dependency] API<=15: Adding annotations.jar to the classpath.-pre-build:
-code-gen: [mergemanifest] No changes in the AndroidManifest files. [echo] Handling aidl files... [aidl] No AIDL files to compile. [echo] ---------- [echo] Handling RenderScript files... [renderscript] No RenderScript files to compile. [echo] ---------- [echo] Handling Resources... [aapt] No changed resources. R.java and Manifest.java untouched. [echo] ---------- [echo] Handling BuildConfig class... [buildconfig] No need to generate new BuildConfig.
-pre-compile:
-compile: [echo] Creating library output jar file...
-post-compile:
-obfuscate:
-dex: [echo] Library project: do not convert bytecode...
-crunch: [crunch] Crunching PNG Files in source dir: /home/ricky/Development/workspace/location_library/LocationLib/res
[crunch] To destination dir: /home/ricky/Development/workspace/location_library/LocationLib/bin/res [crunch] Crunched 0 PNG files to update cache-package-resources: [echo] Library project: do not package resources...
-package: [echo] Library project: do not package apk...
-post-package:
-do-debug: [echo] Library project: do not create apk... [propertyfile] Updating property file: /home/ricky/Development/workspace/location_library/LocationLib/bin/build.prop [propertyfile] Updating property file: /home/ricky/Development/workspace/location_library/LocationLib/bin/build.prop [propertyfile] Updating property file: /home/ricky/Development/workspace/location_library/LocationLib/bin/build.prop [propertyfile] Updating property file: /home/ricky/Development/workspace/location_library/LocationLib/bin/build.prop
-post-build:
debug:
nodeps:
-set-mode-check:
-set-debug-files:
-check-env: [checkenv] Android SDK Tools Revision 20.0.1 [checkenv] Installed at /app/android-sdk-linux_86
-setup: [echo] Project Name: ExceptionHandlerLib [gettype] Project Type: Android Library
-set-debug-mode:
-debug-obfuscation-check:
-build-setup: [echo] Resolving Build Target for ExceptionHandlerLib... [gettarget] Project Target: Android 4.1 [gettarget] API level:
16 [echo] ---------- [echo] Creating output directories if needed... [echo] ---------- [echo] Resolving Dependencies for ExceptionHandlerLib... [dependency] Library dependencies: [dependency] No Libraries [dependency] [dependency] ------------------ [dependency] API<=15: Adding annotations.jar to the classpath.-pre-build:
-code-gen: [mergemanifest] No changes in the AndroidManifest files. [echo] Handling aidl files... [aidl] No AIDL files to compile. [echo] ---------- [echo] Handling RenderScript files... [renderscript] No RenderScript files to compile. [echo] ---------- [echo] Handling Resources... [aapt] No changed resources. R.java and Manifest.java untouched. [echo] ---------- [echo] Handling BuildConfig class... [buildconfig] No need to generate new BuildConfig.
-pre-compile:
-compile: [echo] Creating library output jar file...
-post-compile:
-obfuscate:
-dex: [echo] Library project: do not convert bytecode...
-crunch: [crunch] Crunching PNG Files in source dir: /home/ricky/Development/workspace/exception_handler_library/ExceptionHandlerLib/res [crunch] To destination dir: /home/ricky/Development/workspace/exception_handler_library/ExceptionHandlerLib/bin/res [crunch] Crunched 0 PNG files to update cache
-package-resources: [echo] Library project: do not package resources...
-package: [echo] Library project: do not package apk...
-post-package:
-do-debug: [echo] Library project: do not create apk... [propertyfile] Updating property file: /home/ricky/Development/workspace/exception_handler_library/ExceptionHandlerLib/bin/build.prop [propertyfile] Updating property file: /home/ricky/Development/workspace/exception_handler_library/ExceptionHandlerLib/bin/build.prop [propertyfile] Updating property file: /home/ricky/Development/workspace/exception_handler_library/ExceptionHandlerLib/bin/build.prop [propertyfile] Updating property file: /home/ricky/Development/workspace/exception_handler_library/ExceptionHandlerLib/bin/build.prop
-post-build:
debug:
-pre-build:
-code-gen: [mergemanifest] Merging AndroidManifest files into one. [mergemanifest] Merging manifests from project and 2 libraries. [mergemanifest] Warning: [AndroidManifest.xml:3, AndroidManifest.xml:3] Main manifest has but library uses targetSdkVersion='16' [mergemanifest] Note: main manifest lacks a declaration, which defaults to value minSdkVersion or 1. [mergemanifest] Warning: [AndroidManifest.xml:3, AndroidManifest.xml:13] Main manifest has but library uses targetSdkVersion='16' [mergemanifest] Note: main manifest lacks a declaration, which defaults to value minSdkVersion or 1.
BUILD FAILED /app/android-sdk-linux_86/tools/ant/build.xml:616: null
Total time: 2 seconds
编辑
我查看了
/app/android-sdk-linux_86/tools/ant/build.xml:616
发现下面一行抛出了null enabled="${manifestmerger.enabled}">
这是整个街区
我将尝试禁用 list 合并选项,看看会发生什么
编辑
从 project.properties 中删除 manifestmerger.enabled=true 似乎解决了我的问题。该项目现在可以编译并完美运行。我想我应该将此错误报告给 android 团队。我的 list 有以下条目
<uses-sdk android:minSdkVersion="7" />
但是,如果我将 targetSdkVersion 提高到 7 以上,仍然不会调用 onActivityResult(),所以这并不能真正解决根本问题,但我的项目现在可以正常工作
最佳答案
在 fragment 中( 错误 startActivityForResult)
getActivty().startActivityForResult(intent, requestCode);
// Cannot receive in fragment
在 fragment 中( 正确 startActivityForResult)
startActivityForResult(intent, requestCode);
// use Fragment.startActivityForRusult()
// not Activity.startActivityForRusult()
Activity 中
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
在 fragment 中
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Do Action
}
关于android - fragment 兼容性 onActivityResult() 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11725490/