简体   繁体   中英

Why am I getting this error while using android studio.?

Why am i getting this error? I created a tool bar. It is showing that the class requires api level of 21 and the current min is 19. Please help me.

package com.example.whatsapp;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.Toolbar;

public class MainActivity extends AppCompatActivity {

    private Toolbar mToolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

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

go to your build.gradle(app) section of your IDE, scoll to the line that says

defaultConfig {
    applicationId "com.example.myapplication"
    minSdkVersion 23
    targetSdkVersion 30
    versionCode 1
    versionName "1.0"

    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

under the minSdkVersion part, put 19

Have you tried importing androidx.appcompat.widget.Toolbar instead of android.widget.Toolbar ? It's available in androidx.appcompat:appcompat:1.1.0 .

The error you're getting is more of a symptom in this case, and not the underlying condition - if you want to have the minSdkVersion set to 19 then you'll just have to utilize more of the unbundled libraries so that you can make use of "the fancy stuff from the future", in this case the Toolbar view introduced in Android L.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM