gpt4 book ai didi

android - getActionBar() 返回 null 并且 requestFeature() 出现在 ActionBarActivity 类中

转载 作者:行者123 更新时间:2023-11-30 02:04:14 24 4
gpt4 key购买 nike

当我想在我的操作栏上添加一个应用程序图标时遇到了一些问题。

所以我有 ActionBarActivity 类,它在 onCreate()

中有以下代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
setContentView(R.layout.activity_check_stock);

if(this.getActionBar() != null){
Log.d("ACTIONBAR_NOT_NULL", "ActionBar Not NULL");
//getActionBar().setHomeButtonEnabled(true);
//getActionBar().setDisplayHomeAsUpEnabled(true);
//getActionBar().setDisplayUseLogoEnabled(true);
//getActionBar().setDisplayOptions(getActionBar().DISPLAY_SHOW_HOME | getActionBar().DISPLAY_SHOW_TITLE);
//getActionBar().setIcon(R.drawable.vanwellis_logo);
}
else{
Log.d("ACTIONBAR_NULL","ActionBar NULL");
}

// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);

}

因为我的 getActionBar() 返回 null,所以我添加 getWindow().requestFeature(Window.FEATURE_ACTION_BAR);但是这种方法仍然返回一个错误,但是有这个新错误:

android.util.AndroidRuntimeException: requestFeature() must be called before adding content

所以我尝试将 requestFeature() 放在 super.onCreate(savedInstanceState); 之前

结果是……getActionBar() 再次返回 null,但没有发生任何错误。

这是我的 style.xml我不知道这是否适用于我的所有 Activity ,但给你...

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="android:actionBarStyle">@style/MyActionBar</item>
<item name="drawerArrowStyle">@style/MyDrawerArrowToggle</item>

<!-- Support library compatibility -->
<item name="actionBarStyle">@style/MyActionBar</item>
</style>

<style name="MyActionBar" parent="Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="android:titleTextStyle">@style/MyTitleTextStyle</item>
<item name="android:background">@color/action_bar_background</item>

<!-- Support library compatibility -->
<item name="titleTextStyle">@style/MyTitleTextStyle</item>
<item name="background">@color/action_bar_background</item>
</style>

<style name="MyDrawerArrowToggle" parent="Base.Widget.AppCompat.DrawerArrowToggle">
<item name="color">@color/foreground</item>
</style>

<style name="MyTitleTextStyle" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
<item name="android:textColor">@color/foreground</item>
</style>

[更新]所以这是我的整个 Activity 类

package com.vanwellis.vinnomobile;

import java.util.Locale;

import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

import com.vanwellis.vinnomobile.fragment.CheckStockFragment;


public class CheckStockActivity extends ActionBarActivity {

/**
* The {@link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {@link FragmentPagerAdapter} derivative, which will keep every
* loaded fragment in memory. If this becomes too memory intensive, it
* may be best to switch to a
* {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/
SectionsPagerAdapter mSectionsPagerAdapter;

/**
* The {@link ViewPager} that will host the section contents.
*/
ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_check_stock);

if(this.getActionBar() != null){
Log.d("ACTIONBAR_NOT_NULL", "ActionBar Not NULL");
//getActionBar().setHomeButtonEnabled(true);
//getActionBar().setDisplayHomeAsUpEnabled(true);
//getActionBar().setDisplayUseLogoEnabled(true);
//getActionBar().setDisplayOptions(getActionBar().DISPLAY_SHOW_HOME | getActionBar().DISPLAY_SHOW_TITLE);
//getActionBar().setIcon(R.drawable.vanwellis_logo);
}
else{
Log.d("ACTIONBAR_NULL","ActionBar NULL");
}

// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);

}


@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_check_stock, 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.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}


/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {

public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}

@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a CheckStockFragment (defined as a static inner class below).
return CheckStockFragment.newInstance(position + 1);
}

@Override
public int getCount() {
// Show 3 total pages.
return 3;
}

@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
case 2:
return getString(R.string.title_section3).toUpperCase(l);
}
return null;
}
}

}

我担心 Widget.AppCompat.Light.ActionBar.Solid.Inverse 会生成 no-actionbar。

我在我的 gradle 应用程序属性中使用具有这些配置的 android studio:

android {
compileSdkVersion 21
buildToolsVersion "21.1.2"

defaultConfig {
applicationId "com.vanwellis.vinnomobile"
minSdkVersion 15
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

那么这里到底发生了什么?

最佳答案

因为你的 ActionBarActivity 是从支持库导入的...

import android.support.v7.app.ActionBarActivity;

...您需要使用 getSupportActionBar() 而不是 getActionBar()

关于android - getActionBar() 返回 null 并且 requestFeature() 出现在 ActionBarActivity 类中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30971855/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com