"请勿在主题中请求Window.FEATURE_SUPPORT_ACTION_BAR并将WindowActionBar设置为false以使用工具栏而不是"

时间:2017-07-30 18:49:44

标签: java android

我收到错误"请勿在主题中请求Window.FEATURE_SUPPORT_ACTION_BAR并将WindowActionBar设置为false以使用工具栏代替"每当我尝试在Android Studio上的模拟器中运行我的应用程序时它会在启动时崩溃。我知道之前已经问过这个问题,但是当我删除extends AppCompatActivity时,我被告知要在其他帖子中执行此操作,然后我会在代码中向setSupportActionBar(toolbar)行提供错误信息。

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

主要活动是使用" AppTheme"

感谢。

MainActivity Code供参考:

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);
    }
}

样式代码供参考:

<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>

清单代码供参考:

<?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>

2 个答案:

答案 0 :(得分:2)

由于您的活动主题设置为AppTheme,因此默认情况下会附带“操作栏”。当您再拨打setSupportActionBar(toolbar)时,您的应用会崩溃,因为您已经有了一个操作栏(来自您的主题)。

只需将您的活动主题更改为清单中的AppTheme.NoActionBar即可。

答案 1 :(得分:2)

更改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>

替换为:

<!-- 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>