是否可以使用android.support.v7.widget.SearchView创建对话框

时间:2014-04-28 02:59:31

标签: android dialog searchview

我正在尝试创建一个包含android.support.v7.widget.SearchView的对话框。我在常规Activity中成功使用了以下布局XML,但似乎无法在对话框中使用它:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.SearchView
            android:id="@+id/search_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    <ListView
            android:id="@+id/list_view"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />

</LinearLayout>

我想在对话框中使用相同的布局XML。当我显示对话框时,它给出以下异常:

FATAL EXCEPTION: main android.view.InflateException: Binary XML file line #7: Error inflating 
class android.support.v7.widget.SearchView at 
android.view.LayoutInflater.createView(LayoutInflater.java:606) at 
android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680) at 
android.view.LayoutInflater.rInflate(LayoutInflater.java:739) at 
android.view.LayoutInflater.inflate(LayoutInflater.java:489) at 
android.view.LayoutInflater.inflate(LayoutInflater.java:396) at 
android.view.LayoutInflater.inflate(LayoutInflater.java:352) at 
com.my.company.activity.MyActivity.selectReference(MyActivity.java:660) at 
com.my.company.activity.MyActivity$4.onClick(MyActivity.java:318) at 
android.view.View.performClick(View.java:3511) at 
android.view.View$PerformClick.run(View.java:14105) at 
android.os.Handler.handleCallback(Handler.java:605) at 
android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:137) at 
android.app.ActivityThread.main(ActivityThread.java:4424) at 
java.lang.reflect.Method.invokeNative(Native Method) at 
java.lang.reflect.Method.invoke(Method.java:511) at 
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) at 
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) at 
dalvik.system.NativeStart.main(Native Method) Caused by: 
java.lang.reflect.InvocationTargetException at 
java.lang.reflect.Constructor.constructNative(Native Method) at 
java.lang.reflect.Constructor.newInstance(Constructor.java:417) at 
android.view.LayoutInflater.createView(LayoutInflater.java:586) ... 18 more Caused by: 
android.view.InflateException: Binary XML file line #41: Error inflating class <unknown> at 
android.view.LayoutInflater.createView(LayoutInflater.java:606) at 
com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56) at 
android.view.LayoutInflater.onCreateView(LayoutInflater.java:653) at 
android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:678) at 
android.view.LayoutInflater.rInflate(LayoutInflater.java:739) at 
android.view.LayoutInflater.inflate(LayoutInflater.java:489) at 
android.view.LayoutInflater.inflate(LayoutInflater.java:396) at 
android.support.v7.widget.SearchView.<init>(SearchView.java:262) ... 21 more Caused by: 
java.lang.reflect.InvocationTargetException at 
java.lang.reflect.Constructor.constructNative(Native Method) at 
java.lang.reflect.Constructor.newInstance(Constructor.java:417) at 
android.view.LayoutInflater.createView(LayoutInflater.java:586) ... 28 more Caused by: 
android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path): 
TypedValue{t=0x2/d=0x7f01005f a=-1} at 
android.content.res.Resources.loadDrawable(Resources.java:1897) at 
android.content.res.TypedArray.getDrawable(TypedArray.java:601) at android.widget.ImageView.
<init>(ImageView.java:119) at android.widget.ImageView.<init>(ImageView.java:109) ... 31 more

我不想在对话框中使用ActionBar,但如果这是使用支持SearchView的唯一方法,那么我将如何在对话框中实现onCreateOptionsMenu()和onOptionsItemSelected()方法。

1 个答案:

答案 0 :(得分:1)

我忘了在清单中为我的活动添加一个ActionBarTheme样式(即android:theme =“@ style / CustomActionBarTheme”)!一切正常!