getActionBar() return null 和 requestFeature() 出现在 ActionBarActivity class
getActionBar() return null and requestFeature() appear in ActionBarActivity class
当我想在我的操作栏上添加一个应用程序图标时遇到了一些问题。
所以我有 ActionBarActivity class 在 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() return 为空,所以我添加 getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
但是这种方法仍然 return 一个错误,但是有了这个新的错误:
android.util.AndroidRuntimeException: requestFeature() must be called
before adding content
所以我试着把 requestFeature()
放在 super.onCreate(savedInstanceState);
之前
结果是...getActionBar() return 再次为 null,但没有发生任何错误。
这是我的 style.xml
我不知道这是否适用于我的所有活动,但是给你...
<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>
[更新]
所以这是我的全部Activityclass
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
会生成无操作栏。
我在 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'
}
}
}
那么这里到底发生了什么?
如果您使用 Theme.AppCompat 主题,那么您必须使用 ActionBarActivity 扩展您的 activity 其他虎钳获取 null 操作栏
指数:
public class MyActivity extends ActionBarActivity {
}
谢谢
因为你的ActionBarActivity是从支持库中导入的...
import android.support.v7.app.ActionBarActivity;
...您需要使用 getSupportActionBar()
而不是 getActionBar()
。
当我想在我的操作栏上添加一个应用程序图标时遇到了一些问题。
所以我有 ActionBarActivity class 在 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() return 为空,所以我添加 getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
但是这种方法仍然 return 一个错误,但是有了这个新的错误:
android.util.AndroidRuntimeException: requestFeature() must be called before adding content
所以我试着把 requestFeature()
放在 super.onCreate(savedInstanceState);
结果是...getActionBar() return 再次为 null,但没有发生任何错误。
这是我的 style.xml 我不知道这是否适用于我的所有活动,但是给你...
<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>
[更新] 所以这是我的全部Activityclass
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
会生成无操作栏。
我在 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'
}
}
}
那么这里到底发生了什么?
如果您使用 Theme.AppCompat 主题,那么您必须使用 ActionBarActivity 扩展您的 activity 其他虎钳获取 null 操作栏
指数:
public class MyActivity extends ActionBarActivity {
}
谢谢
因为你的ActionBarActivity是从支持库中导入的...
import android.support.v7.app.ActionBarActivity;
...您需要使用 getSupportActionBar()
而不是 getActionBar()
。