ActionBarSherlock getCustomView()在pre ICS的Android版本返回NULL [英] ActionBarSherlock getCustomView() return null on pre ICS android versions
本文介绍了ActionBarSherlock getCustomView()在pre ICS的Android版本返回NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在开发采用ABS库的应用程序。二fragmentActivities我需要使用在动作条的自定义视图。我的code的工作完美ICS和JB。但是我测试的姜饼而坠毁。
我的code是下一个:
公共类SearchContactActivity扩展SherlockFragmentActivity {
私人的EditText searchField;
私人的ImageButton searchButton;
@覆盖
保护无效的onCreate(捆绑为arg0){
super.onCreate(为arg0);
的setContentView(R.layout.search_contacts);
动作条动作条= getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(真正的);
查看查看= actionBar.getCustomView(); // ICS和放大器; JB返回查看确定,较低的版本,返回null
searchField =(EditText上)view.findViewById(R.id.editText_search); //这里的应用程序崩溃,因为观点是空
searchButton =(的ImageButton)view.findViewById(R.id.button_search);
}
}
我的主题XML:
< XML版本=1.0编码=UTF-8&GT?;
<资源的xmlns:机器人=http://schemas.android.com/apk/res/android>
<样式名称=Theme.Styled.Search父=Theme.Sherlock.Light>
<项目名称=actionBarStyle> @风格/ Widget.Styled.ActionBar.Search< /项目>
<项目名称=机器人:actionBarStyle> @风格/ Widget.Styled.ActionBar.Search< /项目>
< /风格>
<样式名称=Widget.Styled.ActionBar.Search父=Widget.Sherlock.Light.ActionBar>
<项目名称=背景> @可绘制/ bg_actionbar< /项目>
<项目名称=机器人:背景> @可绘制/ bg_actionbar< /项目>
<项目名称=backgroundSplit> @可绘制/ bg_actionbar< /项目>
<项目名称=机器人:backgroundSplit> @可绘制/ bg_actionbar< /项目>
<项目名称=机器人:titleTextStyle> @风格/ NoTitleText< /项目>
<项目名称=机器人:subtitleTextStyle> @风格/ NoTitleText< /项目>
<项目名称=机器人:displayOptions> showHome | useLogo | showCustom< /项目>
<项目名称=机器人:customNavigationLayout> @布局/ custom_action< /项目>
< /风格>
< /资源>
和我custom_view:
< XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT>
<的EditText
机器人:ID =@ + ID / editText_search
风格=@风格/ Widget.Styled.EditText
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentLeft =真
机器人:layout_toLeftOf =@ + ID / button_search
机器人:EMS =10
机器人:提示=@字符串/ hint_search
机器人:inputType =textNoSuggestions
机器人:layout_marginRight =5DP/>
<的ImageButton
机器人:ID =@ + ID / button_search
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignBottom =@ + ID / editText_search
机器人:layout_alignTop =@ + ID / editText_search
机器人:layout_alignParentRight =真
机器人:layout_marginRight =5DP
机器人:背景=@可绘制/ button_go_login_xml
机器人:以下属性来=15dp
机器人:paddingRight =15dp
机器人:SRC =@可绘制/ LUPA/>
< / RelativeLayout的>
我的logcat
10-18 18:03:34.259:E / AndroidRuntime(10576):致命异常:主要
10-18 18:03:34.259:E / AndroidRuntime(10576):java.lang.RuntimeException的:无法启动的活动ComponentInfo {com.co2fine.client / com.co2fine.client.SearchContactActivity}:显示java.lang.NullPointerException
10-18 18:03:34.259:E / AndroidRuntime(10576):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
10-18 18:03:34.259:E / AndroidRuntime(10576):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
10-18 18:03:34.259:E / AndroidRuntime(10576):在android.app.ActivityThread.access $ 1500(ActivityThread.java:117)
10-18 18:03:34.259:E / AndroidRuntime(10576):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:931)
10-18 18:03:34.259:E / AndroidRuntime(10576):在android.os.Handler.dispatchMessage(Handler.java:99)
10-18 18:03:34.259:E / AndroidRuntime(10576):在android.os.Looper.loop(Looper.java:123)
10-18 18:03:34.259:E / AndroidRuntime(10576):在android.app.ActivityThread.main(ActivityThread.java:3729)
10-18 18:03:34.259:E / AndroidRuntime(10576):在java.lang.reflect.Method.invokeNative(本机方法)
10-18 18:03:34.259:E / AndroidRuntime(10576):在java.lang.reflect.Method.invoke(Method.java:507)
10-18 18:03:34.259:E / AndroidRuntime(10576):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:874)
10-18 18:03:34.259:E / AndroidRuntime(10576):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:632)
10-18 18:03:34.259:E / AndroidRuntime(10576):在dalvik.system.NativeStart.main(本机方法)
10-18 18:03:34.259:E / AndroidRuntime(10576):由:显示java.lang.NullPointerException
10-18 18:03:34.259:E / AndroidRuntime(10576):在com.co2fine.client.SearchContactActivity.onCreate(SearchContactActivity.java:53)
10-18 18:03:34.259:E / AndroidRuntime(10576):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-18 18:03:34.259:E / AndroidRuntime(10576):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
10-18 18:03:34.259:E / AndroidRuntime(10576):11 ...更多
解决方案
尝试添加:
<项目名称=customNavigationLayout> @布局/ custom_action< /项目>
附和:
<项目名称=机器人:customNavigationLayout> @布局/ custom_action< /项目>
I'm developing an application using ABS Library. In two fragmentActivities I need use a custom view in the Actionbar. My code work perfect in ICS and JB. But I test in gingerbread and crashed.
My code is the next:
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);
}
}
My Theme 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 name="backgroundSplit">@drawable/bg_actionbar</item>
<item name="android:backgroundSplit">@drawable/bg_actionbar</item>
<item name="android:titleTextStyle">@style/NoTitleText</item>
<item name="android:subtitleTextStyle">@style/NoTitleText</item>
<item name="android:displayOptions">showHome|useLogo|showCustom</item>
<item name="android:customNavigationLayout">@layout/custom_action</item>
</style>
</resources>
and my custom_view:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/editText_search"
style="@style/Widget.Styled.EditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/button_search"
android:ems="10"
android:hint="@string/hint_search"
android:inputType="textNoSuggestions"
android:layout_marginRight="5dp" />
<ImageButton
android:id="@+id/button_search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/editText_search"
android:layout_alignTop="@+id/editText_search"
android:layout_alignParentRight="true"
android:layout_marginRight="5dp"
android:background="@drawable/button_go_login_xml"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:src="@drawable/lupa" />
</RelativeLayout>
My logcat
10-18 18:03:34.259: E/AndroidRuntime(10576): FATAL EXCEPTION: main
10-18 18:03:34.259: E/AndroidRuntime(10576): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.co2fine.client/com.co2fine.client.SearchContactActivity}: java.lang.NullPointerException
10-18 18:03:34.259: E/AndroidRuntime(10576): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
10-18 18:03:34.259: E/AndroidRuntime(10576): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
10-18 18:03:34.259: E/AndroidRuntime(10576): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-18 18:03:34.259: E/AndroidRuntime(10576): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
10-18 18:03:34.259: E/AndroidRuntime(10576): at android.os.Handler.dispatchMessage(Handler.java:99)
10-18 18:03:34.259: E/AndroidRuntime(10576): at android.os.Looper.loop(Looper.java:123)
10-18 18:03:34.259: E/AndroidRuntime(10576): at android.app.ActivityThread.main(ActivityThread.java:3729)
10-18 18:03:34.259: E/AndroidRuntime(10576): at java.lang.reflect.Method.invokeNative(Native Method)
10-18 18:03:34.259: E/AndroidRuntime(10576): at java.lang.reflect.Method.invoke(Method.java:507)
10-18 18:03:34.259: E/AndroidRuntime(10576): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:874)
10-18 18:03:34.259: E/AndroidRuntime(10576): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:632)
10-18 18:03:34.259: E/AndroidRuntime(10576): at dalvik.system.NativeStart.main(Native Method)
10-18 18:03:34.259: E/AndroidRuntime(10576): Caused by: java.lang.NullPointerException
10-18 18:03:34.259: E/AndroidRuntime(10576): at com.co2fine.client.SearchContactActivity.onCreate(SearchContactActivity.java:53)
10-18 18:03:34.259: E/AndroidRuntime(10576): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-18 18:03:34.259: E/AndroidRuntime(10576): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
10-18 18:03:34.259: E/AndroidRuntime(10576): ... 11 more
解决方案
Try adding:
<item name="customNavigationLayout">@layout/custom_action</item>
to go along with:
<item name="android:customNavigationLayout">@layout/custom_action</item>
这篇关于ActionBarSherlock getCustomView()在pre ICS的Android版本返回NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文