Android 找不到符号变量ToolbarMain
我正在Android Studio中创建一个示例recyclerview,但在构建应用程序时遇到了一些问题:Android 找不到符号变量ToolbarMain,android,Android,我正在Android Studio中创建一个示例recyclerview,但在构建应用程序时遇到了一些问题: 找不到符号变量ToolbarMain(MainActivity.java:31) 找不到符号变量lvMain(MainActivity.java:32) 有人能帮我吗?代码如下: activity_main.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xml
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity">
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:background="@color/colorPrimary"
android:id="@+id/ToolbarMain">
</androidx.appcompat.widget.Toolbar>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/lvMain"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:dividerHeight="10dp"
android:layout_below="@+id/ToolbarMain"
android:divider="@null">
</ListView>
</RelativeLayout>
main_activity_single_item.xml
<androidx.cardview.widget.CardView android:layout_height="wrap_content"
android:layout_width="match_parent"
android:background="@color/colorAccent"
android:elevation="4dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="80dp"
android:layout_height="105dp"
android:id="@+id/ivMain"
android:layout_marginLeft="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvMain"
android:text="Timetable"
android:layout_marginLeft="6dp"
android:textStyle="bold"
android:layout_toRightOf="@+id/ivMain"
android:textSize="24sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvDescription"
android:text="Description"
android:layout_marginLeft="6dp"
android:layout_below="@+id/tvMain"
android:layout_toRightOf="@+id/ivMain"
android:textSize="16dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvClick"
android:text="Click Here"
android:layout_alignParentRight="true"
android:layout_marginTop="6dp"
android:textSize="12sp"
android:layout_below="@id/tvDescription"
android:layout_toRightOf="@id/ivMain"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
非常感谢您的帮助:)尝试清理和重建您的项目。您知道,在这个空白中:
private void setupUIViews() {
toolbar = (Toolbar)findViewById(R.id.ToolbarMain);
listView = (ListView)findViewById(R.id.lvMain);
}
您无法访问活动\u main.xml
,因此有两种方法供您使用:
一,
}
二,。
从private void setupUIViews(){}
中取出代码,并将其粘贴到onCreate()
中,不做任何更改
apply plugin: 'com.android.application'
android {
compileSdkVersion 30
buildToolsVersion "30.0.2"
defaultConfig {
applicationId "com.azripra.mytestingapplications"
minSdkVersion 24
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'com.google.android.material:material:1.2.1'
}
private void setupUIViews() {
toolbar = (Toolbar)findViewById(R.id.ToolbarMain);
listView = (ListView)findViewById(R.id.lvMain);
}
private void setupUIViews() {
View mView = getLayoutInflater().inflate(R.layout.activity_main , null);
toolbar = (Toolbar)mView.findViewById(R.id.ToolbarMain);
listView = (ListView)mView.findViewById(R.id.lvMain);