小编Cla*_*kXP的帖子

ActionBarSherlock getCustomView()在前ICS安卓版本上返回null

我正在使用ABS库开发一个应用程序.在两个fragmentActivities中,我需要在Actionbar中使用自定义视图.我的代码在ICS和JB中非常完美.但我在姜饼中测试并坠毁.

我的代码是下一个:

public class SearchContactActivity extends SherlockFragmentActivity {

    private EditText searchField;
    private ImageButton searchButton;


    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.search_contacts);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        View view = actionBar.getCustomView();//ICS & JB return the View OK, lower versions returns null
        searchField = (EditText) view.findViewById(R.id.editText_search);//here the app crash because view is null
        searchButton = (ImageButton) view.findViewById(R.id.button_search);

    }
}
Run Code Online (Sandbox Code Playgroud)

我的主题XML:

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <style name="Theme.Styled.Search" parent="Theme.Sherlock.Light">
       <item name="actionBarStyle">@style/Widget.Styled.ActionBar.Search</item>
        <item name="android:actionBarStyle">@style/Widget.Styled.ActionBar.Search</item>
    </style>
    <style name="Widget.Styled.ActionBar.Search" parent="Widget.Sherlock.Light.ActionBar">
        <item name="background">@drawable/bg_actionbar</item>
        <item name="android:background">@drawable/bg_actionbar</item>
        <item …
Run Code Online (Sandbox Code Playgroud)

android actionbarsherlock android-actionbar

0
推荐指数
1
解决办法
1044
查看次数