我在运行项目时遇到错误。我正在从我的java文件和gradle控制台附加代码 以下是activity_funfacts.xml的代码形式。这里有一个RelativeLayout标记
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".FunFactsMyActivity"
android:background="#ff51b46d">
<TextView
android:text="Did you know"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24dp"
android:textColor="#80ffffff" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Aint strech it when they wake up in the morning"
android:id="@+id/factTextView"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="24dp" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Show Another Fun Fact"
android:id="@+id/showFactbButton"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="@android:color/white" />
</RelativeLayout>
Gradle Console输出
Executing tasks: [:app:assembleDebug]
Configuration on demand is an incubating feature.
:app:preBuild UP-TO-DATE
:app:preDebugBuild UP-TO-DATE
:app:compileDebugNdk UP-TO-DATE
:app:checkDebugManifest
:app:prepareDebugDependencies
:app:compileDebugAidl UP-TO-DATE
:app:compileDebugRenderscript UP-TO-DATE
:app:generateDebugBuildConfig UP-TO-DATE
:app:generateDebugAssets UP-TO-DATE
:app:mergeDebugAssets UP-TO-DATE
:app:generateDebugResValues UP-TO-DATE
:app:generateDebugResources UP-TO-DATE
:app:mergeDebugResources UP-TO-DATE
:app:processDebugManifest UP-TO-DATE
:app:processDebugResources UP-TO-DATE
:app:generateDebugSources UP-TO-DATE
:app:compileDebugJava
/Users/AndroidStudioProjects/FunFacts/app/src/main/java/com/example/funfacts/FunFactsMyActivity.java:29: error: cannot find symbol
final RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout);
^
symbol: variable relativeLayout
location: class id
1 error
FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:compileDebugJava'.
> 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.
BUILD FAILED
Total time: 5.252 secs
答案 0 :(得分:1)
您需要在android:id="@+id/relativeLayout"
代码下将ID定义为RelativeLayout
。
答案 1 :(得分:0)
此错误表示您的布局XML文件中没有ID relativeLayout
。
请检查R.layout.activity_funfacts
是否包含ID为RelativeLayout
的{{1}}。