我是 Android 新手。当我尝试将工具栏添加到 Android java 代码时,出现上述标题错误。 Stackoverflow 中有一个标题为相同问题的答案,说制作工具栏布局并将其包含到我所需的布局中。我也尝试过。但这并没有解决我的问题。
package com.locationtracker2019;
import android.os.Bundle;
import android.widget.Toolbar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.database.DatabaseReference;
public class ListOnline extends AppCompatActivity {
//Firebase
DatabaseReference onlineRef, currentUserRef, counterRef;
FirebaseRecyclerAdapter<User, ListOnlineViewHolder> adapter;
//View
RecyclerView listOnline;
RecyclerView.LayoutManager layoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_online);
//Init View
listOnline = (RecyclerView)findViewById(R.id.listOnline);
listOnline.setHasFixedSize(true);
layoutManager = new LinearLayoutManager (this);
listOnline.setLayoutManager(layoutManager);
//set Toolbar and Logout / Join menu
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); <!--line1-->
toolbar.setTitle("LocationTracker2019"); <!--line2-->
}
}
第 1 行(工具栏) 和第 2 行setTitle 中带有红色下划线。
这是我的 build.gradle(Module:app)
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "com.locationtracker2019"
minSdkVersion 19
targetSdkVersion 29
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.recyclerview:recyclerview:1.0.0'
implementation "com.google.firebase:firebase-database:10.2.0"
implementation "com.google.firebase:firebase-auth:10.2.0"
implementation "com.firebaseui:firebase-ui-auth:1.2.0"
implementation "com.firebaseui:firebase-ui-database:1.2.0"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
SDK版本有问题吗?
最佳答案
添加以下内容解决了我的问题
import androidx.appcompat.widget.Toolbar;
我已经删除了
import android.widget.Toolbar;
谢谢@Lino
关于java - 错误: cannot find symbol variable toolbar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59480401/