升级我的Android SDK后,出现“错误:无法访问FragmentActivity”

时间:2018-12-11 13:30:38

标签: android-studio android-gradle android-fragmentactivity

我有一个基于libuvccamera库的Android应用程序。

我的问题出在将我的SDK升级到最新版本之后。

当我尝试“进行项目”时,它运行良好。

但是当我尝试运行项目时,我收到以下错误消息:

  

错误:无法访问FragmentActivity

     

找不到android.support.v4.app.FragmentActivity的类文件

     

注意:某些输入文件使用或覆盖不推荐使用的API。

在此特定行中:

baseActivity.setPath(path);

此行是此功能的一部分:

public void handleUpdateMedia(final String path) {
            if (DEBUG) Log.v(TAG_THREAD, "handleUpdateMedia:path=" + path);
            mHandler.setPath(path);
            final Activity parent=mWeakParent.get();

            BaseActivity baseActivity=(BaseActivity) parent;
            if (baseActivity != null) {
                baseActivity.setPath(path);
            }
            final boolean released=(mHandler == null) || mHandler.mReleased;
            if (parent != null && parent.getApplicationContext() != null) {
                try {
                    if (DEBUG) Log.i(TAG, "MediaScannerConnection#scanFile");
                    MediaScannerConnection.scanFile(parent.getApplicationContext(), new String[]{path}, null, null);
                } catch (final Exception e) {
                    Log.e(TAG, "handleUpdateMedia:", e);
                }
                if (released || parent.isDestroyed())
                    handleRelease();
            } else {
                Log.w(TAG, "MainActivity already destroyed");
                // give up to add this movie to MediaStore now.
                // Seeing this movie on Gallery app etc. will take a lot of time.
                handleRelease();
            }
        }

哪个位于usbCameraCommon库(libuvccamera库使用的库之一)中。

还有其他人遇到这种问题吗?

谢谢

1 个答案:

答案 0 :(得分:0)

我找到了一个解决方案,但是有更好的解决方案,如果有人可以在这里写得更详细,我将不胜感激。

项目失败的原因是在libuvccamera项目的gradle文件中错误地使用了implementationapi

我只是将implementation用于所有依赖项项目,现在我知道这是错误的,但是我仍然不了解它们之间的区别。

所以无效的代码是:

dependencies {
    implementation fileTree(dir: new File(buildDir, 'libs'), include: '*.jar')
    implementation "com.android.support:support-annotations:${supportLibVersion}"
    implementation 'com.android.support:support-v4:25.3.1'
    implementation ("com.serenegiant:common:${commonLibVersion}") {
        exclude module: 'support-v4'
    }
}

当我返回旧代码时,它会起作用:

dependencies {
    compile fileTree(dir: new File(buildDir, 'libs'), include: '*.jar')
    compile "com.android.support:support-annotations:${supportLibVersion}"
    compile 'com.android.support:support-v4:25.3.1'
    compile ("com.serenegiant:common:${commonLibVersion}") {
        exclude module: 'support-v4'
    }
}

如果有人能通过一点解释就能正确解决问题,我将非常感激。

谢谢!

更新:

我找到了正确的解决方案,但仍然对它为什么起作用的一些解释表示赞赏:

dependencies {
    implementation fileTree(dir: new File(buildDir, 'libs'), include: '*.jar')
    implementation "com.android.support:support-annotations:${supportLibVersion}"
    api 'com.android.support:support-v4:25.3.1'
    api("com.serenegiant:common:${commonLibVersion}") {
        exclude module: 'support-v4'
    }
}