【问题标题】:Replace can not find container with id替换找不到带有 id 的容器
【发布时间】:2019-07-06 19:24:42
【问题描述】:

我在我的项目中使用抽屉菜单和片段。当我运行我的项目时,我在日志中看到了错误。我该如何解决这个错误?

MainActivity 中的代码片段

if (fragment != null) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.gandash, fragment);
        ft.commit();
    }

activity_main.xml的代码

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="@layout/app_bar_main">

<FrameLayout
    android:id="@+id/gandash"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

我开始跑步时的错误 enter image description here

:app:buildInfoDebugLoader
:app:checkDebugClasspath UP-TO-DATE
:app:preBuild UP-TO-DATE
:app:preDebugBuild UP-TO-DATE
:app:compileDebugAidl NO-SOURCE
:app:compileDebugRenderscript UP-TO-DATE
:app:checkDebugManifest UP-TO-DATE
:app:generateDebugBuildConfig UP-TO-DATE
:app:prepareLintJar UP-TO-DATE
:app:mainApkListPersistenceDebug UP-TO-DATE
:app:generateDebugResValues UP-TO-DATE
:app:generateDebugResources UP-TO-DATE
:app:mergeDebugResources UP-TO-DATE
:app:createDebugCompatibleScreenManifests UP-TO-DATE
:app:processDebugManifest
:app:splitsDiscoveryTaskDebug UP-TO-DATE
:app:processDebugResources
:app:generateDebugSources
:app:javaPreCompileDebug UP-TO-DATE
C:\Users\User\Desktop\vp-site\app\src\main\java\com\example\user\client_vp\MainActivity.java:144: error: cannot find symbol
            ft.replace(R.id.gandash, fragment);
                           ^
  symbol:   variable gandash
  location: class id
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
1 error
:app:compileDebugJavaWithJavac FAILED
:app:buildInfoGeneratorDebug

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 4s
17 actionable tasks: 5 executed, 12 up-to-date

【问题讨论】:

  • 尝试干净的构建和无效/重启
  • 请从 logcat 添加完整的 Stacktrace。
  • 我试了几次,甚至重启了我的电脑,我什至不得不重置Android Studio的设置
  • 您是否检查过您是否正确使用了 setContentView() 的布局?
  • 是的,你能告诉我们你的完整活动代码吗?

标签: java android replace


【解决方案1】:

我认为这只是同步问题。
请尝试从 File > Invalidate Cache/Restart 重新启动您的 android studio ..

重启后别忘了清理你的项目

  • 构建 -> 清理项目
  • 工具 -> Android -> 将项目与 Gradle 文件同步

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-05
    • 2015-08-28
    • 2015-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-15
    • 2015-04-05
    相关资源
    最近更新 更多