如何在Fragment中使用Actionbar-Android开发问答
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在Fragment中使用Actionbar-Android开发问答相关的知识,希望对你有一定的参考价值。
Actionbar是Activity的东西,Fragment是运行在Activity中的碎片,要使用actionBar要通过getActivity来处理、。1.Fragment是android3.0引入的新控件,果应用使用在3.0以下、1.6及以上的版本,需要引入v4包。
2.Fragment支持在不同的Activity中使用并且可以处理自己的输入事件以及生命周期方法等。
3.Fr期方法依赖于Activity的生命周期,例如一个Activity的onPause()的生命周期方法被调用的时候这个Activity中的所有的Fragment的onPause()方法也将被调用。
4.FragmentManager提供了对Activity运行时的Fragment的添加、删除、替换的操作。在Activity运行期间可以添加Fragment而不是在XML布局文件中进行定义。如果打算在Activity中改变Fragment的生命过程。如果要执行添加、删除、修改的操作,必须通过FragmentManager的对象获得一个FragmentTransaction对象,通过它的API来执行这些操作。
5.两个单独的Fragment之间是不应该进行通信的。应该使用所存在的Activity作为沟通的纽带。 参考技术A Actionbar是依附在Activity上的,你如果要使用actionbar,要确保对应的Activity有Actionbar,然后getActivity().getActionBar();
也可以用toolbar代替actionbar;本回答被提问者和网友采纳
如何在 Fragment 上使用 MaterialSearchView
【中文标题】如何在 Fragment 上使用 MaterialSearchView【英文标题】:How to use MaterialSearchView On Fragment 【发布时间】:2017-06-01 15:05:02 【问题描述】:我正在尝试在片段中添加带有自定义工具栏的MaterialSearchView。
所以我首先为它创建了 xml 布局。
search_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_
android:layout_>
<FrameLayout
android:id="@+id/toolbar_container"
android:layout_
android:layout_>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar2"
android:layout_
android:layout_
android:background="@color/colorAccent"/>
<com.miguelcatalan.materialsearchview.MaterialSearchView
android:id="@+id/search_view"
android:layout_
android:layout_ />
</FrameLayout>
</LinearLayout>
然后我添加了菜单项..
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/action_search"
android:title="@string/app_name"
android:icon="@drawable/ic_action_action_search"
app:showAsAction="collapseActionView|ifRoom"
/>
</menu>
然后在片段类中我添加了自定义操作栏。经过测试,代码运行良好。但是当我添加 MaterialSearchView .. 应用程序停止响应..
这是我的java代码..
public class searchView extends Fragment
Toolbar search;
MaterialSearchView search_view;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
View v = inflater.inflate(R.layout.search_view,container,false);
LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.search_view, container, false);
search = (Toolbar)layout.findViewById(R.id.toolbar2);
((AppCompatActivity) getActivity()).setSupportActionBar(search);
((AppCompatActivity) getActivity()).getSupportActionBar().setTitle("Contacts");
return v;
@Override
public void onCreateOptionsMenu (Menu menu, MenuInflater inflater)
inflater.inflate(R.menu.search,menu);
super.onCreateOptionsMenu(menu, inflater);
search_view = (MaterialSearchView) getView().findViewById(R.id.search_view);
final MenuItem item = menu.findItem(R.id.action_search);
search_view.setMenuItem(item);
完整的错误日志是如此之大..所以这里是一些部分
01-17 19:46:57.968 17920-17920/? A/art: art/runtime/runtime.cc:422] at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
01-17 19:46:57.968 17920-17920/? A/art: art/runtime/runtime.cc:422] at com.miguelcatalan.materialsearchview.MaterialSearchView.initiateView(MaterialSearchView.java:143)
01-17 19:46:57.968 17920-17920/? A/art: art/runtime/runtime.cc:422] at com.miguelcatalan.materialsearchview.MaterialSearchView.<init>(MaterialSearchView.java:93)
01-17 19:46:57.968 17920-17920/? A/art: art/runtime/runtime.cc:422] at com.miguelcatalan.materialsearchview.MaterialSearchView.<init>(MaterialSearchView.java:85)
01-17 19:46:57.968 17920-17920/? A/art: art/runtime/runtime.cc:422] at java.lang.reflect.Constructor.newInstance0!(Native method)
01-17 19:46:57.968 17920-17920/? A/art: art/runtime/runtime.cc:422] at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
01-17 19:46:57.968 17920-17920/? A/art: art/runtime/runtime.cc:422] at android.view.LayoutInflater.createView(LayoutInflater.java:645)
01-17 19:46:57.968 17920-17920/? A/art: art/runtime/runtime.cc:422] at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:787)
01-17 19:46:57.968 17920-17920/? A/art: art/runtime/runtime.cc:422] at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
01-17 19:46:57.968 17920-17920/? A/art: art/runtime/runtime.cc:422] at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
01-17 19:46:57.968 17920-17920/? A/art: art/runtime/runtime.cc:422] at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
01-17 19:46:57.968 17920-17920/? A/art: art/runtime/runtime.cc:422] at android.view.LayoutInflater.rInflate(LayoutInflater.java:861)
01-17 19:46:57.968 17920-17920/? A/art: art/runtime/runtime.cc:422] at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
01-17 19:46:57.968 17920-17920/? A/art: art/runtime/runtime.cc:422] at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
01-17 19:46:57.968 17920-17920/? A/art: art/runtime/runtime.cc:422] - locked <0x0cb4b03a> (a java.lang.Object[])
01-17 19:46:57.968 17920-17920/? A/art: art/runtime/runtime.cc:422] at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
01-17 19:46:57.968 17920-17920/? A/art: art/runtime/runtime.cc:422] at com.miguelcatalan.materialsearchview.MaterialSearchView.initiateView(MaterialSearchView.java:143)
01-17 19:46:57.968 17920-17920/? A/art: art/runtime/runtime.cc:422] at com.miguelcatalan.materialsearchview.MaterialSearchView.<init>(MaterialSearchView.java:93)
01-17 19:46:57.968 17920-17920/? A/art: art/runtime/runtime.cc:422] at com.miguelcatalan.materialsearchview.MaterialSearchView.<init>(MaterialSearchView.java:85)
01-17 19:46:57.968 17920-17920/? A/art: art/runtime/runtime.cc:422] at java.lang.reflect.Constructor.newInstance0!(Native method)
01-17 19:46:57.968 17920-17920/? A/art: art/runtime/runtime.cc:422] at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
01-17 19:46:57.968 17920-17920/? A/art: art/runtime/runtime.cc:422] at android.view.LayoutInflater.createView(LayoutInflater.java:645)
01-17 19:46:57.968 17920-17920/? A/art: art/runtime/runtime.cc:422] at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:787)
01-17 19:46:57.968 17920-17920/? A/art: art/runtime/runtime.cc:422] at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
01-17 19:46:57.968 17920-17920/? A/art: art/runtime/runtime.cc:422] at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
01-17 19:46:57.968 17920-17920/? A/art: art/runtime/runtime.cc:422] at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
01-17 19:46:57.968 17920-17920/? A/art: art/runtime/runtime.cc:422] at android.view.LayoutInflater.rInflate(LayoutInflater.java:861)
01-17 19:46:57.968 17920-17920/? A/art: art/runtime/runtime.cc:422] at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
01-17 19:46:57.969 17920-17920/? A/art: art/runtime/runtime.cc:422] at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
01-17 19:46:57.969 17920-17920/? A/art: art/runtime/runtime.cc:422] - locked <0x0cb4b03a> (a java.lang.Object[])
01-17 19:46:57.969 17920-17920/? A/art: art/runtime/runtime.cc:422] at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
01-17 19:46:57.969 17920-17920/? A/art: art/runtime/runtime.cc:422] at com.miguelcatalan.materialsearchview.MaterialSearchView.initiateView(MaterialSearchView.java:143)
01-17 19:46:57.969 17920-17920/? A/art: art/runtime/runtime.cc:422] at com.miguelcatalan.materialsearchview.MaterialSearchView.<init>(MaterialSearchView.java:93)
01-17 19:46:57.969 17920-17920/? A/art: art/runtime/runtime.cc:422] at com.miguelcatalan.materialsearchview.MaterialSearchView.<init>(MaterialSearchView.java:85)
01-17 19:46:57.969 17920-17920/? A/art: art/runtime/runtime.cc:422] at java.lang.reflect.Constructor.newInstance0!(Native method)
01-17 19:46:57.969 17920-17920/? A/art: art/runtime/runtime.cc:422] at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
01-17 19:46:57.969 17920-17920/? A/art: art/runtime/runtime.cc:422] at android.view.LayoutInflater.createView(LayoutInflater.java:645)
01-17 19:46:57.969 17920-17920/? A/art: art/runtime/runtime.cc:422] at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:787)
01-17 19:46:57.969 17920-17920/? A/art: art/runtime/runtime.cc:422] at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
01-17 19:46:58.178 17920-17920/? A/art: art/runtime/runtime.cc:427] Summary:
01-17 19:46:58.178 17920-17920/? A/art: art/runtime/runtime.cc:427] 503 of com.miguelcatalan.materialsearchview.MaterialSearchView (503 unique instances)
01-17 19:46:58.178 17920-17920/? A/art: art/runtime/runtime.cc:427] 1 of java.lang.ClassNotFoundException
01-17 19:46:58.178 17920-17920/? A/art: art/runtime/runtime.cc:427] 1 of java.lang.NoClassDefFoundError
01-17 19:46:58.178 17920-17920/? A/art: art/runtime/runtime.cc:427] 4 of java.lang.String (4 unique instances)
01-17 19:46:58.178 17920-17920/? A/art: art/runtime/runtime.cc:427] 2 of java.lang.Class (2 unique instances)
01-17 19:46:58.178 17920-17920/? A/art: art/runtime/runtime.cc:427] 1 of java.lang.String[] (4 elements)
01-17 19:46:58.178 17920-17920/? A/art: art/runtime/runtime.cc:427]
01-17 19:46:58.178 17920-17920/? A/art: art/runtime/runtime.cc:427]
01-17 19:46:58.178 17920-17920/? A/libc: Fatal signal 6 (SIGABRT), code -6 in tid 17920 (buckydroid.chat)
[ 01-17 19:46:58.178 357: 357 W/ ]
debuggerd: handling request: pid=17920 uid=10147 gid=10147 tid=17920
谢谢你..
【问题讨论】:
【参考方案1】:遇到了同样的问题。改变
search_view = (MaterialSearchView) getView().findViewById(R.id.search_view);
到
search_view = (MaterialSearchView) getActivity().findViewById(R.id.search_view);
以我的代码为例
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
// Inflate the layout for this fragment
setHasOptionsMenu(true);
view = inflater.inflate(R.layout.fragment_stock, container, false);
return view;
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu_main, menu);
MaterialSearchView searchView = (MaterialSearchView) getActivity().findViewById(R.id.search_view);
final MenuItem item = menu.findItem(R.id.action_search);
searchView.setMenuItem(item);
【讨论】:
【参考方案2】:您需要像这样在 Fragment 中添加setHasOptionsMenu
:
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
【讨论】:
还添加了错误日志..它太大了...所以我可以添加它的一部分以上是关于如何在Fragment中使用Actionbar-Android开发问答的主要内容,如果未能解决你的问题,请参考以下文章
如何在Fragment中使用PreferenceFragment
如何在Fragment中使用PreferenceFragment
如何在 Fragment 中使用 SharedPreferences 保存数据