【问题标题】:Why mToolbar and Toolbar doesn't work in android studio为什么 mToolbar 和 Toolbar 在 android studio 中不起作用
【发布时间】:2020-05-10 05:28:41
【问题描述】:

我什么都试过了。但我想不通。 总是告诉我它们不起作用--->mToolbar 和 Toolbar

import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
import android.widget.Toolbar;      

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.navigation.NavigationView;

public class MainActivity extends AppCompatActivity {

    private NavigationView navigationView;
    private DrawerLayout drawerLayout;
    private RecyclerView postList;
    private Toolbar mToolbar;
    private Toolbar supportActionBar;



mToolbar = (Toolbar) findViewById(R.id.main_page_toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setTitle("home");

也是我的格兰德。不知道有没有用。

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'com.google.android.material:material:1.0.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.google.firebase:firebase-auth:16.0.5'
    implementation 'com.google.firebase:firebase-database:16.0.4'
    implementation 'com.google.firebase:firebase-storage:16.0.4'
    implementation 'de.hdodenhof:circleimageview:3.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}

我试过 android.widget.support.v7.Toolbar;但它没有帮助

错误是:

来自 --> import android.widget.Toolbar

致 --> import androidx.appcompat.widget.Toolbar

【问题讨论】:

  • “不起作用”是什么意思?你得到什么样的错误?
  • 能否分享MainActivity的布局?
  • @SotirisKapa 请将错误转录到您的问题中,并随时在其中包含屏幕截图。通过这样做,其他成员可以使用搜索找到您的具体错误。

标签: java android android-studio android-widget android-toolbar


【解决方案1】:

您在 xml 文件中使用 androidX,但在您的 java 文件中导入旧库,尝试删除

   import android.widget.Toolbar

把你之前导入的合适的放在xml中

  import androidx.appcompat.widget.Toolbar

【讨论】:

    【解决方案2】:

    碰巧,在您的布局中,您使用的是 androidx 版本的工具栏吗?如果是,那就是造成此错误的原因。在您的 Activity 中,您引用了不同版本的工具栏。

    【讨论】:

    • 其实我是 android studio 的新手。如何更改 androidx 或如何解决此问题?
    • 请发布您的布局并完整发布您的 Activity 类。如果您正在使用,您应该能够查看是否在工具栏的 XML 标记内。即<androidx.appcompat.widget.Toolbar android:layout_width="match_parent" android:layout_height="match_parent" />
    【解决方案3】:

    我认为错误可能在 Toolbar 类的导入中 在您的 exepmlo 中,您已经在使用 androidrx 标准,但在 Toolbar 对象中,您使用的是旧的导入

    import android.widget.Toolbar
    

    但是新的导入是

    import androidx.appcompat.widget.Toolbar
    

    【讨论】:

    • 就是这样,非常感谢。一直以来,我都试图编辑我的帖子,但他们想告诉更多细节。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-11
    • 2020-01-16
    • 1970-01-01
    • 2016-04-19
    • 1970-01-01
    • 2016-09-07
    • 2021-03-20
    相关资源
    最近更新 更多