gpt4 book ai didi

android - Searchview 使应用程序崩溃

转载 作者:行者123 更新时间:2023-11-30 02:51:09 27 4
gpt4 key购买 nike

我使用的不是 menuItem 而是布局中的搜索 View 。我支持的最低 API 级别是 9,搜索 View 是 11。

现在我这样创建了我的搜索 View :

 <SearchView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/searchView"
android:layout_marginBottom="1dp"/>

在直线布局中。我注意到警告,所以我将其更改为:

<android.support.v7.widget.SearchView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/searchView"
android:layout_marginBottom="1dp"/>

但是我的应用程序在新的 Android 固件上崩溃了。

我在我的应用中这样使用它:

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {

SearchView searchView = (SearchView) findViewById(R.id.searchView);
searchView.setOnSearchClickListener(this);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {

return false;
}

@Override
public boolean onQueryTextChange(String s) {

FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
if (s.length() == 0) {
transaction.show(mapFragment);
} else {

transaction.hide(mapFragment);
}

updateListViewWithFilter(s);

transaction.commit();

return false;
}
});
}

我总是得到这个错误:

06-10 07:15:35.420    1534-1534/be.test.android.dvl E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{be.test.android.dvl/be.test.android.amoffices.OfficesActivity}: android.view.InflateException: Binary XML file line #14: Error inflating class SearchView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #14: Error inflating class SearchView
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:581)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:626)
at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:128)
at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:76)
at be.test.android.base.BaseActivity.setContentView(BaseActivity.java:106)
at be.test.android.amoffices.OfficesActivity.onCreate(OfficesActivity.java:59)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
            at android.app.ActivityThread.access$1500(ActivityThread.java:117)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:130)
            at android.app.ActivityThread.main(ActivityThread.java:3683)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:507)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
            at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: android.view.SearchView in loader dalvik.system.PathClassLoader[/data/app/be.test.android.dvl-1.apk]
at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at android.view.LayoutInflater.createView(LayoutInflater.java:471)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:549)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:626)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
            at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:128)
            at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:76)
            at be.test.android.base.BaseActivity.setContentView(BaseActivity.java:106)
            at be.test.android.amoffices.OfficesActivity.onCreate(OfficesActivity.java:59)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
            at android.app.ActivityThread.access$1500(ActivityThread.java:117)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:130)
            at android.app.ActivityThread.main(ActivityThread.java:3683)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:507)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
            at dalvik.system.NativeStart.main(Native Method)

最佳答案

我没有更改导入语句和使用支持小部件的方式。

除了搜索 View 之外,这段代码是我唯一更改的内容。

  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {

android.support.v7.widget.SearchView searchView = (android.support.v7.widget.SearchView) findViewById(R.id.searchView);
searchView.setOnSearchClickListener(this);
searchView.setOnQueryTextListener(new android.support.v7.widget.SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {

return false;
}

@Override
public boolean onQueryTextChange(String s) {

FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
if (s.length() == 0) {
transaction.show(mapFragment);
} else {

transaction.hide(mapFragment);
}

updateListViewWithFilter(s);

transaction.commit();

return false;
}
});
}

您可以看到我现在将 searchView 转换为 support.searchview。

关于android - Searchview 使应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24135517/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com