“不要请求 Window.FEATURE_SUPPORT_ACTION_BAR 并在您的主题中将 WindowActionBar 设置为 false 以使用工具栏代替”

Cha*_*esD 2 java android

每当我尝试在 Android Studio 上的模拟器中运行我的应用程序时,我都会收到错误消息“不要请求 Window.FEATURE_SUPPORT_ACTION_BAR 并将主题中的 WindowActionBar 设置为 false 以使用工具栏”,并且在启动时崩溃。我知道之前已经问过这个问题,但是当我extends AppCompatActivity按照其他帖子中的要求删除时,它会给我setSupportActionBar(toolbar)代码中更远的行的错误。

据我了解,这是 Android Studio 对本机工具栏和我试图实现的工具栏感到困惑的错误。也许我误解了问题的关键?为什么setSupportActionBar(toolbar)一旦我摆脱了 extends 语句,它就会给我一个错误?

主要活动是使用“AppTheme”

谢谢。

MainActivity代码供参考:

package com.treehouse.android.movies;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;

import java.util.ArrayList;

//extends AppCompatActivity

public class MainActivity extends AppCompatActivity
{
    static public ArrayList<Movie> moviesList;
    static public ArrayList<String> images;
    public String mostPopular="http://api.themoviedb.org/3/movie/popular?api_key=";
    public String highRated="http://api.themoviedb.org/3/movie/top_rated?api_key=";
    //Make both GridAdapter and GridView non-Static?
    static public GridAdapter gridAdapter;
    static public GridView gridView;
    public static boolean connectionEnabled;
    public Context currentContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        String result="";
        moviesList=new ArrayList<>();
        images=new ArrayList<>();
        currentContext=getApplicationContext();


        if (isNetworkAvailable()!= false) {
            connectionEnabled=true;
            getJsonData(0);
            new GetMovies(currentContext);
            gridView =(GridView) findViewById(R.id.moviesGridView);
            gridAdapter =new GridAdapter(MainActivity.this,moviesList,images);
            gridView.setAdapter(gridAdapter);

            gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Intent movieIntent=new Intent(getApplicationContext(),DetailsActivity.class);
                    Log.i("Default position ", String.valueOf(position));
                    movieIntent.putExtra("position",position);
                    startActivity(movieIntent);
                }
            });
        }
        else{
            Toast.makeText(this, "Network Is Not Available", Toast.LENGTH_LONG).show();
            connectionEnabled=false;
        }



    }

    public boolean isNetworkAvailable() {
        ConnectivityManager connectivityManager
                = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        return activeNetworkInfo != null && activeNetworkInfo.isConnected();
    }


    //get json file
    //0 for most popular
    //1 for highest-rated
    public void getJsonData(int searchBy){
        GetMovies downloadTask=new GetMovies(currentContext);
        try {
            if (searchBy == 0 ){
                downloadTask.execute(mostPopular);
            }
            else if(searchBy == 1){
                downloadTask.execute(highRated);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.mostpopular_button) {
            if (isNetworkAvailable()!= false) {
                new GetMovies(currentContext).execute(mostPopular);
                gridAdapter.notifyDataSetChanged();
            }
            else{
                Toast.makeText(this, "Network Is Not Available", Toast.LENGTH_LONG).show();
            }
        }
        else if (id== R.id.highrated_button){
            if (isNetworkAvailable()!= false) {
                new GetMovies(currentContext).execute(highRated);
                gridAdapter.notifyDataSetChanged();
            }
            else{
                Toast.makeText(this, "Network Is Not Available", Toast.LENGTH_LONG).show();
            }
        }

        return super.onOptionsItemSelected(item);
    }
}
Run Code Online (Sandbox Code Playgroud)

样式代码供参考:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

</resources>
Run Code Online (Sandbox Code Playgroud)

清单代码供参考:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.treehouse.android.movies" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true" >
        <!-- android:theme="@style/AppTheme" part of application -->
        <activity android:name=".MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN"
                        android:theme="@style/AppTheme"
                    />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
Run Code Online (Sandbox Code Playgroud)

小智 6

改变你的 style.xml

<!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
Run Code Online (Sandbox Code Playgroud)

替换为:

<!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="windowActionBar">true</item>
        <item name="windowNoTitle">true</item>
    </style>
Run Code Online (Sandbox Code Playgroud)