问题描述
我有一个福尔摩斯片段活动从我使用的片段寻呼机适配器设置不同的福尔摩斯片段。
现在展现在每一个片段的搜索查看我把这种方法在片段::中的onCreate
此方法将显示在片段的动作条菜单项。
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
setHasOptionsMenu(真正的);
}
我现在还设置了onCreateOptionMenu()这样的:
@覆盖
公共无效onCreateOptionsMenu(功能菜单,MenuInflater充气)
{
super.onCreateOptionsMenu(菜单,充气);
inflater.inflate(R.menu.menu,菜单);
最终的菜单项searchItem = menu.findItem(R.id.menu_item_search);
最后搜索查看搜索查看=(搜索查看)searchItem.getActionView();
searchView.setQueryHint(搜索此处);
}
现在的奇怪的事情发生在我的第一个片段,我也做了同样的事情,在搜索查看获得成功打开。
如果
- 在我去的第一个片段,打开搜索查看然后进入第二个
- 片段搜索查看将得到开放。
同样的事情发生的第三片段
- 去第3段,
- 开启搜索查看,
- 回到第二frgment,搜索查看获得开业。
但是,如果我直接到第二个片段的搜索查看得不到开幕。
希望我是清楚的。
任何有关此线索?任何建议/链接将AP preciated ..
在此先感谢...
修改::
menu.xml文件
< XML版本=1.0编码=UTF-8&GT?;
<菜单的xmlns:机器人=http://schemas.android.com/apk/res/android>
<项目
机器人:ID =@ + ID / menu_item_search
机器人:图标=@可绘制/ abs__ic_search
机器人:showAsAction =ifRoom | collapseActionView
机器人:actionViewClass =com.actionbarsherlock.widget.SearchView
机器人:标题=搜索产品>
< /项目>
<项目
机器人:ID =@ + ID / root_menu
机器人:图标=@可绘制/ abs__ic_menu_moreoverflow_normal_holo_light
机器人:showAsAction =总是
机器人:标题=更多>
<菜单>
<项目
机器人:ID =@ + ID / menu_Home
机器人:图标=@可绘制/家
机器人:showAsAction =从不
机器人:标题=家/>
<项目
机器人:ID =@ + ID / menu_favourite
机器人:图标=@可绘制/最爱
机器人:showAsAction =从不
机器人:标题=收藏/>
<项目
机器人:ID =@ + ID / menu_Balance
机器人:图标=@可绘制/平衡
机器人:showAsAction =从不
机器人:标题=平衡/>
<项目
机器人:ID =@ + ID / menu_logout
机器人:图标=@可绘制/ btn_logout
机器人:showAsAction =从不
机器人:标题=注销/>
< /菜单>
< /项目>
< /菜单>
的logcat ::
12-06 16:45:26.401:E / AndroidRuntime(1581):致命异常:主要
12-06 16:45:26.401:E / AndroidRuntime(1581):java.lang.ClassCastException:com.mobifin.subscriber.ButtonPayActivity
12-06 16:45:26.401:E / AndroidRuntime(1581):在com.mobifin.subscriber.home.BillPayFragment.onCreateOptionsMenu(BillPayFragment.java:101)
12-06 16:45:26.401:E / AndroidRuntime(1581):在android.support.v4.app.Watson.onCreatePanelMenu(Watson.java:55)
12-06 16:45:26.401:E / AndroidRuntime(1581):在com.actionbarsherlock.ActionBarSherlock.callbackCreateOptionsMenu(ActionBarSherlock.java:560)
12-06 16:45:26.401:E / AndroidRuntime(1581):在com.actionbarsherlock.internal.ActionBarSherlockCompat.$p$pparePanel(ActionBarSherlockCompat.java:466)
12-06 16:45:26.401:E / AndroidRuntime(1581):在com.actionbarsherlock.internal.ActionBarSherlockCompat.dispatchInvalidateOptionsMenu(ActionBarSherlockCompat.java:265)
12-06 16:45:26.401:E / AndroidRuntime(1581):在com.actionbarsherlock.app.SherlockFragmentActivity.invalidateOptionsMenu(SherlockFragmentActivity.java:149)
12-06 16:45:26.401:E / AndroidRuntime(1581):在com.actionbarsherlock.app.SherlockFragmentActivity.supportInvalidateOptionsMenu(SherlockFragmentActivity.java:155)
12-06 16:45:26.401:E / AndroidRuntime(1581):在android.support.v4.app.Fragment.setMenuVisibility(Fragment.java:726)
12-06 16:45:26.401:E / AndroidRuntime(1581):在android.support.v4.app.FragmentPagerAdapter.setPrimaryItem(FragmentPagerAdapter.java:127)
12-06 16:45:26.401:E / AndroidRuntime(1581):在android.support.v4.view.ViewPager.populate(ViewPager.java:802)
12-06 16:45:26.401:E / AndroidRuntime(1581):在android.support.v4.view.ViewPager.completeScroll(ViewPager.java:1280)
12-06 16:45:26.401:E / AndroidRuntime(1581):在android.support.v4.view.ViewPager.computeScroll(ViewPager.java:1176)
12-06 16:45:26.401:E / AndroidRuntime(1581):在android.view.ViewGroup.drawChild(ViewGroup.java:1562)
12-06 16:45:26.401:E / AndroidRuntime(1581):在android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
12-06 16:45:26.401:E / AndroidRuntime(1581):在android.view.View.draw(View.java:6883)
12-06 16:45:26.401:E / AndroidRuntime(1581):在android.view.ViewGroup.drawChild(ViewGroup.java:1646)
12-06 16:45:26.401:E / AndroidRuntime(1581):在android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
12-06 16:45:26.401:E / AndroidRuntime(1581):在android.view.ViewGroup.drawChild(ViewGroup.java:1644)
12-06 16:45:26.401:E / AndroidRuntime(1581):在android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
12-06 16:45:26.401:E / AndroidRuntime(1581):在android.view.ViewGroup.drawChild(ViewGroup.java:1644)
12-06 16:45:26.401:E / AndroidRuntime(1581):在android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
12-06 16:45:26.401:E / AndroidRuntime(1581):在android.view.ViewGroup.drawChild(ViewGroup.java:1644)
12-06 16:45:26.401:E / AndroidRuntime(1581):在android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
12-06 16:45:26.401:E / AndroidRuntime(1581):在android.view.View.draw(View.java:6883)
12-06 16:45:26.401:E / AndroidRuntime(1581):在android.widget.FrameLayout.draw(FrameLayout.java:357)
12-06 16:45:26.401:E / AndroidRuntime(1581):在com.android.internal.policy.impl.PhoneWindow $ DecorView.draw(PhoneWindow.java:1862)
12-06 16:45:26.401:E / AndroidRuntime(1581):在android.view.ViewRoot.draw(ViewRoot.java:1522)
12-06 16:45:26.401:E / AndroidRuntime(1581):在android.view.ViewRoot.performTraversals(ViewRoot.java:1258)
12-06 16:45:26.401:E / AndroidRuntime(1581):在android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
12-06 16:45:26.401:E / AndroidRuntime(1581):在android.os.Handler.dispatchMessage(Handler.java:99)
12-06 16:45:26.401:E / AndroidRuntime(1581):在android.os.Looper.loop(Looper.java:130)
12-06 16:45:26.401:E / AndroidRuntime(1581):在android.app.ActivityThread.main(ActivityThread.java:3683)
12-06 16:45:26.401:E / AndroidRuntime(1581):在java.lang.reflect.Method.invokeNative(本机方法)
12-06 16:45:26.401:E / AndroidRuntime(1581):在java.lang.reflect.Method.invoke(Method.java:507)
12-06 16:45:26.401:E / AndroidRuntime(1581):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)
12-06 16:45:26.401:E / AndroidRuntime(1581):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-06 16:45:26.401:E / AndroidRuntime(1581):在dalvik.system.NativeStart.main(本机方法)
我已经实现了我想要的最后,但有一个问题..我不知道我什么都做的黑客/补丁或正确的方式,但它工作正常与< 3.0,但现在发现的问题,> = 3.0
我所做的是::
在我的容器的活动我已经声明onCreateOptionsMenu()和prepareOptionsMenu()::
@覆盖
公共布尔onCreateOptionsMenu(菜单菜单)
{
super.onCreateOptionsMenu(菜单);
。getSupportMenuInflater()膨胀(R.menu.menu,菜单);
返回true;
}
公共布尔prepareOptionsMenu(功能菜单)上
{
如果(versionStatus == 0)
{
menu.findItem(R.id.root_menu).setVisible(假);
}
返回super.on prepareOptionsMenu(菜单);
}
现在在每个片段中的onCreateOptionsMenu()我已经做了这样的事情::标签我检查操作系统的内部版本,并根据我manupulate我的选项菜单如下图所示。
@覆盖
公共无效onCreateOptionsMenu(功能菜单,MenuInflater充气)
{
super.onCreateOptionsMenu(菜单,充气);
INT currentapiVersion = android.os.Build.VERSION.SDK_INT;
INT versionStatus = 0;
如果(currentapiVersion == android.os.Build.VERSION_ codeS.FROYO || currentapiVersion == android.os.Build.VERSION_ codeS.GINGERBREAD)
{
versionStatus = 0;
}
否则,如果(currentapiVersion> = android.os.Build.VERSION_ codeS.HONEYCOMB)
{
versionStatus = 1;
}
如果(versionStatus == 1)
{
inflater.inflate(R.menu.menu,菜单);
menu.findItem(R.id.menu_item_search).setVisible(假);
最终的菜单项searchItem = menu.findItem(R.id.menu_item_search);
搜索查看搜索查看=(搜索查看)searchItem.getActionView();
searchView.setQueryHint(搜索产品);
searchView.setIconifiedByDefault(假);
searchView.setOnQueryTextListener(本);
}
其他
{
inflater.inflate(R.menu.menu,菜单);
最终的菜单项searchItem = menu.findItem(R.id.menu_item_search);
搜索查看搜索查看=(搜索查看)searchItem.getActionView();
searchView.setQueryHint(搜索产品);
searchView.setIconifiedByDefault(假);
searchView.setOnQueryTextListener(本);
}
}
它的正常工作2.3.3,但问题米现在面临的是3.0 +
1)无法看到扩大搜索查看
后的提示2)扩大在第一个片段的搜索查看...
3)移动到第二个片段(没有倒塌的搜索查看)
4)现在,在第二个片段扩大当前片段的搜索查看
5)的当前片段pressed当时的结果是一团糟:(
修改::
我已经做了我的集装箱活动如下变化,现在它的做工精细,最后我取得了我想要什么......
@覆盖
公共布尔onCreateOptionsMenu(菜单菜单)
{
super.onCreateOptionsMenu(菜单);
如果(versionStatus == 0)
{
。getSupportMenuInflater()膨胀(R.menu.menu,菜单);
}
返回true;
}
公共布尔prepareOptionsMenu(功能菜单)上
{
如果(versionStatus == 0)
{
menu.findItem(R.id.root_menu).setVisible(假);
}
返回super.on prepareOptionsMenu(菜单);
}
I have one Sherlock Fragment Activity from which i am setting the different sherlock fragments using fragment pager adapter.
Now to show the searchview in each of the fragment i have placed this method in onCreate of Fragment::
This method will show menu items in the ActionBar of the fragment.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
I have now also set the onCreateOptionMenu() like this ::
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu, menu);
final MenuItem searchItem = menu.findItem(R.id.menu_item_search);
final SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setQueryHint("Search Here");
}
Now the strange things happens that in my first fragment i have done the same thing and the searchview get opened successfully.
If
- i go to the first fragment,open the searchview then go to the second
- fragment the searchview will get opened.
Same thing happens for third fragment
- go to 3rd fragment,
- open searchview,
- back to the second frgment,searchview get opened.
But if i go to directly on the second fragment the searchview not get opened.
Hope i am clear.
Any clue about this?Any suggestion/links will be appreciated..
Thanks in Advance...
EDIT ::
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menu_item_search"
android:icon="@drawable/abs__ic_search"
android:showAsAction="ifRoom|collapseActionView"
android:actionViewClass="com.actionbarsherlock.widget.SearchView"
android:title="Search Products">
</item>
<item
android:id="@+id/root_menu"
android:icon="@drawable/abs__ic_menu_moreoverflow_normal_holo_light"
android:showAsAction="always"
android:title="More">
<menu>
<item
android:id="@+id/menu_Home"
android:icon="@drawable/home"
android:showAsAction="never"
android:title="Home"/>
<item
android:id="@+id/menu_favourite"
android:icon="@drawable/favourite"
android:showAsAction="never"
android:title="Favourite"/>
<item
android:id="@+id/menu_Balance"
android:icon="@drawable/balance"
android:showAsAction="never"
android:title="Balance"/>
<item
android:id="@+id/menu_logout"
android:icon="@drawable/btn_logout"
android:showAsAction="never"
android:title="Logout"/>
</menu>
</item>
</menu>
Logcat ::
12-06 16:45:26.401: E/AndroidRuntime(1581): FATAL EXCEPTION: main
12-06 16:45:26.401: E/AndroidRuntime(1581): java.lang.ClassCastException: com.mobifin.subscriber.ButtonPayActivity
12-06 16:45:26.401: E/AndroidRuntime(1581): at com.mobifin.subscriber.home.BillPayFragment.onCreateOptionsMenu(BillPayFragment.java:101)
12-06 16:45:26.401: E/AndroidRuntime(1581): at android.support.v4.app.Watson.onCreatePanelMenu(Watson.java:55)
12-06 16:45:26.401: E/AndroidRuntime(1581): at com.actionbarsherlock.ActionBarSherlock.callbackCreateOptionsMenu(ActionBarSherlock.java:560)
12-06 16:45:26.401: E/AndroidRuntime(1581): at com.actionbarsherlock.internal.ActionBarSherlockCompat.preparePanel(ActionBarSherlockCompat.java:466)
12-06 16:45:26.401: E/AndroidRuntime(1581): at com.actionbarsherlock.internal.ActionBarSherlockCompat.dispatchInvalidateOptionsMenu(ActionBarSherlockCompat.java:265)
12-06 16:45:26.401: E/AndroidRuntime(1581): at com.actionbarsherlock.app.SherlockFragmentActivity.invalidateOptionsMenu(SherlockFragmentActivity.java:149)
12-06 16:45:26.401: E/AndroidRuntime(1581): at com.actionbarsherlock.app.SherlockFragmentActivity.supportInvalidateOptionsMenu(SherlockFragmentActivity.java:155)
12-06 16:45:26.401: E/AndroidRuntime(1581): at android.support.v4.app.Fragment.setMenuVisibility(Fragment.java:726)
12-06 16:45:26.401: E/AndroidRuntime(1581): at android.support.v4.app.FragmentPagerAdapter.setPrimaryItem(FragmentPagerAdapter.java:127)
12-06 16:45:26.401: E/AndroidRuntime(1581): at android.support.v4.view.ViewPager.populate(ViewPager.java:802)
12-06 16:45:26.401: E/AndroidRuntime(1581): at android.support.v4.view.ViewPager.completeScroll(ViewPager.java:1280)
12-06 16:45:26.401: E/AndroidRuntime(1581): at android.support.v4.view.ViewPager.computeScroll(ViewPager.java:1176)
12-06 16:45:26.401: E/AndroidRuntime(1581): at android.view.ViewGroup.drawChild(ViewGroup.java:1562)
12-06 16:45:26.401: E/AndroidRuntime(1581): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
12-06 16:45:26.401: E/AndroidRuntime(1581): at android.view.View.draw(View.java:6883)
12-06 16:45:26.401: E/AndroidRuntime(1581): at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
12-06 16:45:26.401: E/AndroidRuntime(1581): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
12-06 16:45:26.401: E/AndroidRuntime(1581): at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
12-06 16:45:26.401: E/AndroidRuntime(1581): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
12-06 16:45:26.401: E/AndroidRuntime(1581): at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
12-06 16:45:26.401: E/AndroidRuntime(1581): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
12-06 16:45:26.401: E/AndroidRuntime(1581): at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
12-06 16:45:26.401: E/AndroidRuntime(1581): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
12-06 16:45:26.401: E/AndroidRuntime(1581): at android.view.View.draw(View.java:6883)
12-06 16:45:26.401: E/AndroidRuntime(1581): at android.widget.FrameLayout.draw(FrameLayout.java:357)
12-06 16:45:26.401: E/AndroidRuntime(1581): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1862)
12-06 16:45:26.401: E/AndroidRuntime(1581): at android.view.ViewRoot.draw(ViewRoot.java:1522)
12-06 16:45:26.401: E/AndroidRuntime(1581): at android.view.ViewRoot.performTraversals(ViewRoot.java:1258)
12-06 16:45:26.401: E/AndroidRuntime(1581): at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
12-06 16:45:26.401: E/AndroidRuntime(1581): at android.os.Handler.dispatchMessage(Handler.java:99)
12-06 16:45:26.401: E/AndroidRuntime(1581): at android.os.Looper.loop(Looper.java:130)
12-06 16:45:26.401: E/AndroidRuntime(1581): at android.app.ActivityThread.main(ActivityThread.java:3683)
12-06 16:45:26.401: E/AndroidRuntime(1581): at java.lang.reflect.Method.invokeNative(Native Method)
12-06 16:45:26.401: E/AndroidRuntime(1581): at java.lang.reflect.Method.invoke(Method.java:507)
12-06 16:45:26.401: E/AndroidRuntime(1581): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-06 16:45:26.401: E/AndroidRuntime(1581): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-06 16:45:26.401: E/AndroidRuntime(1581): at dalvik.system.NativeStart.main(Native Method)
I have achieved what i want finally but with one problem..I don't know whatever i have done is the hack/patch or the correct way but it is working fine in <3.0 but now find the issue in >=3.0.
What i have done is ::
In My Container Activity i have declare the onCreateOptionsMenu() and onPrepareOptionsMenu() ::
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
super.onCreateOptionsMenu(menu);
getSupportMenuInflater().inflate(R.menu.menu, menu);
return true;
}
public boolean onPrepareOptionsMenu(Menu menu)
{
if(versionStatus==0)
{
menu.findItem(R.id.root_menu).setVisible(false);
}
return super.onPrepareOptionsMenu(menu);
}
Now in each Fragment's onCreateOptionsMenu() i have done something like this ::
I have checked the build version of OS and based on that i manupulate my option menu like below ..
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
super.onCreateOptionsMenu(menu, inflater);
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
int versionStatus = 0;
if (currentapiVersion == android.os.Build.VERSION_CODES.FROYO || currentapiVersion == android.os.Build.VERSION_CODES.GINGERBREAD)
{
versionStatus=0;
}
else if(currentapiVersion >= android.os.Build.VERSION_CODES.HONEYCOMB)
{
versionStatus=1;
}
if(versionStatus==1)
{
inflater.inflate(R.menu.menu, menu);
menu.findItem(R.id.menu_item_search).setVisible(false);
final MenuItem searchItem = menu.findItem(R.id.menu_item_search);
SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setQueryHint("Search Products");
searchView.setIconifiedByDefault(false);
searchView.setOnQueryTextListener(this);
}
else
{
inflater.inflate(R.menu.menu, menu);
final MenuItem searchItem = menu.findItem(R.id.menu_item_search);
SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setQueryHint("Search Products");
searchView.setIconifiedByDefault(false);
searchView.setOnQueryTextListener(this);
}
}
It's working fine in 2.3.3 but problem m facing now is in 3.0 +
1)not able to see the hint after expanding the searchview
2)expand the searchview in first fragment...
3)Move to second fragment(without collapsing the searchview )
4)Now in second fragment expand the searchview of current fragment
5)in Current Fragment Pressed Back then the result is mess :(
EDIT ::
I have done the following changes in my Container Activity and now its working fine,Finally i have achieved what i want....
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
super.onCreateOptionsMenu(menu);
if(versionStatus==0)
{
getSupportMenuInflater().inflate(R.menu.menu, menu);
}
return true;
}
public boolean onPrepareOptionsMenu(Menu menu)
{
if(versionStatus==0)
{
menu.findItem(R.id.root_menu).setVisible(false);
}
return super.onPrepareOptionsMenu(menu);
}
这篇关于操作栏福尔摩斯搜索查看没有扩张上点击它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!