错误:找不到符号变量工具栏

问题描述 投票:0回答:1

我是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版本有问题吗?

java android android-toolbar
1个回答
0
投票

添加以下内容解决了我的问题

import androidx.appcompat.widget.Toolbar;

我已删除

import android.widget.Toolbar;

谢谢@Lino


0
投票

您在此处导入了错误的工具栏参考。使用

import androidx.appcompat.widget.Toolbar; 

代替

import android.widget.Toolbar;

并将工具栏添加到ActionBar

setSupportActionBar(toolbar);

© www.soinside.com 2019 - 2024. All rights reserved.