You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android开发:Toolbar findViewById无响应及API级别报错求助

Fixing Toolbar & Spinner Issues When Migrating to AndroidX

Hey there! Let's work through these common AndroidX migration issues step by step—you're totally on the right track, just a few small tweaks needed.

1. Fixing the "View requires API level 21" Error for Toolbar

The red underline on <Toolbar> happens because you're using the native Android Toolbar (android.widget.Toolbar), which was added in API 21. But since you're using AndroidX, you need to reference the AppCompat Toolbar (which supports down to API 16) by its full class name in your XML.

Replace your shorthand <Toolbar> tag with:

<androidx.appcompat.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

This tells Android to use the backward-compatible AndroidX version instead of the native one.

2. Fixing findViewById(R.id.toolbar) Not Returning a Value

There are a few key checks to fix this:

  • Always call setContentView() first: You can't find views in the layout until you've inflated it. Make sure your onCreate() method looks like this:
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); // Critical: Load the layout first
        
        // Initialize the Toolbar (no need for casting in modern Android)
        androidx.appcompat.widget.Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar); // Set it as your Activity's ActionBar
    }
    
  • Verify the ID matches: Double-check that the android:id="@+id/toolbar" in your XML exactly matches R.id.toolbar in your Java code. Typos here are a super common culprit!
  • Use the correct variable type: Make sure your Toolbar variable is declared as androidx.appcompat.widget.Toolbar, not the old support library version or native Toolbar.

3. Adding Spinner to the Toolbar (Quick Recap)

Once your Toolbar is working, adding the Spinner is straightforward. Just nest it inside the Toolbar in your XML:

<androidx.appcompat.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary">

    <Spinner
        android:id="@+id/toolbar_spinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end" />

</androidx.appcompat.widget.Toolbar>

Then initialize it in your Activity:

Spinner toolbarSpinner = findViewById(R.id.toolbar_spinner);

// Create an adapter for the Spinner
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<>(this,
        android.R.layout.simple_spinner_item,
        new String[]{"Item 1", "Item 2", "Item 3"});
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

toolbarSpinner.setAdapter(spinnerAdapter);

Quick Dependency Check

Make sure your app-level build.gradle (Module level) includes the AndroidX AppCompat dependency:

dependencies {
    implementation 'androidx.appcompat:appcompat:1.6.1' // Use the latest available version
}

That should resolve all your current issues. Let me know if you hit any other snags!

内容的提问来源于stack exchange,提问作者resander

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 08:20:42