我正在尝试使用片段内的“自定义工具栏”添加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_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/toolbar_container"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar2"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorAccent"/>
<com.miguelcatalan.materialsearchview.MaterialSearchView
android:id="@+id/search_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</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
谢谢..
最佳答案
有同样的问题。更改
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);}
}
关于java - 如何在 fragment 上使用MaterialSearchView,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41699038/