当尝试在Java中创建搜索视图时,我遇到了一系列的错误。

huangapple 未分类评论51阅读模式
标题翻译

When trying to create a search view in java, I get a list of errors

问题

我在cs50x上工作,正在尝试按照他们给我的步骤创建一个搜索视图。然而,当我尝试打开应用程序时,logcat输出如下:

2020-05-30 15:15:45.371 20144-20144/edu.harvard.cs50.pokedex E/AndroidRuntime: FATAL EXCEPTION: main
Process: edu.harvard.cs50.pokedex, PID: 20144
java.lang.ClassCastException: androidx.appcompat.widget.SearchView cannot be cast to android.widget.SearchView
at edu.harvard.cs50.pokedex.MainActivity.onCreateOptionsMenu(MainActivity.java:34)

还有一堆其他错误,但我已经检查过它们,它们并不重要。关键问题是课程告诉我在main_menu.xml中使用androidx版本的searchview,但它说不兼容。
以下是我的main_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:tools="http://schemas.android.com/tools"
    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:icon="@android:drawable/ic_menu_search"
        android:title="@string/search"
        app:actionViewClass="androidx.appcompat.widget.SearchView"
        app:showAsAction="always" />
</menu>

以及我的适配器(搜索视图部分):

@Override
public Filter getFilter() {
    return new PokemonFilter();
}

private class PokemonFilter extends Filter {
    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        FilterResults results = new FilterResults();
        if (constraint.length() == 0 || constraint == null) {
            results.values = pokemon;
            results.count = pokemon.size();
        }
        else {
            List<Pokemon> filteredPokemon = new ArrayList<>();
            for (Pokemon pokemon : filteredPokemon) {
                if (pokemon.getName().toLowerCase().startsWith(constraint.toString())) {
                    filteredPokemon.add(pokemon);
                }
            }
            results.values = filteredPokemon;
            results.count = filteredPokemon.size();
        }
        return results;
    }

    @Override
    protected void publishResults(CharSequence charSequence, FilterResults results) {
        filtered = (List<Pokemon>) results.values;
        notifyDataSetChanged();
    }
}

还有主活动:

public class MainActivity extends AppCompatActivity implements SearchView.OnQueryTextListener {
private RecyclerView recyclerView;
private PokedexAdapter adapter;
private RecyclerView.LayoutManager layoutManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    recyclerView = findViewById(R.id.recycler_view);
    adapter = new PokedexAdapter(getApplicationContext());
    layoutManager = new LinearLayoutManager(this);

    recyclerView.setAdapter(adapter);
    recyclerView.setLayoutManager(layoutManager);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main_menu, menu);
    MenuItem searchItem = menu.findItem(R.id.action_search);
    androidx.appcompat.widget.SearchView searchView = (androidx.appcompat.widget.SearchView) searchItem.getActionView();
    searchView.setOnQueryTextListener((androidx.appcompat.widget.SearchView.OnQueryTextListener) this);

    return true;
}

@Override
public boolean onQueryTextChange(String newText) {
    adapter.getFilter().filter(newText);
    return false;
}

@Override
public boolean onQueryTextSubmit(String newText) {
    adapter.getFilter().filter(newText);
    return false;
}
}

希望这能帮助你解决问题。如果你有任何其他问题,欢迎提出。

英文翻译

I am working on cs50x and I am trying to create a search view following the steps they gave me. However, when i try to open the app, the logcat outputs this:

2020-05-30 15:15:45.371 20144-20144/edu.harvard.cs50.pokedex E/AndroidRuntime: FATAL EXCEPTION: main
Process: edu.harvard.cs50.pokedex, PID: 20144
java.lang.ClassCastException: androidx.appcompat.widget.SearchView cannot be cast to android.widget.SearchView
at edu.harvard.cs50.pokedex.MainActivity.onCreateOptionsMenu(MainActivity.java:34)

And a buch of other erros, but I have checked them and they doesnt matter. The point is that the course is telling me to use the androidx version of the searchviw in the main_menu.xml but it says it is incompatible.
Here is my main_menu.xml:

    &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;menu xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;&gt;

    &lt;item
        android:id=&quot;@+id/action_search&quot;
        android:icon=&quot;@android:drawable/ic_menu_search&quot;
        android:title=&quot;@string/search&quot;
        app:actionViewClass=&quot;androidx.appcompat.widget.SearchView&quot;
        app:showAsAction=&quot;always&quot; /&gt;
&lt;/menu&gt;

And my adapter (The sarch view part):

@Override
public Filter getFilter() {
    return new PokemonFilter();
}

private class PokemonFilter extends Filter {
    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        FilterResults results = new FilterResults();
        if (constraint.length() == 0 || constraint == null) {
            results.values = pokemon;
            results.count = pokemon.size();
        }
        else {
            List&lt;Pokemon&gt; filteredPokemon = new ArrayList&lt;&gt;();
            for (Pokemon pokemon : filteredPokemon) {
                if (pokemon.getName().toLowerCase().startsWith(constraint.toString())) {
                    filteredPokemon.add(pokemon);
                }
            }
            results.values = filteredPokemon;
            results.count = filteredPokemon.size();
        }
        return results;
        }

    @Override
    protected void publishResults(CharSequence charSequence, FilterResults results) {
        filtered = (List&lt;Pokemon&gt;) results.values;
        notifyDataSetChanged();
    }
}

And the main activity:

public class MainActivity extends AppCompatActivity implements SearchView.OnQueryTextListener {
private RecyclerView recyclerView;
private PokedexAdapter adapter;
private RecyclerView.LayoutManager layoutManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    recyclerView = findViewById(R.id.recycler_view);
    adapter = new PokedexAdapter(getApplicationContext());
    layoutManager = new LinearLayoutManager(this);

    recyclerView.setAdapter(adapter);
    recyclerView.setLayoutManager(layoutManager);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main_menu, menu);
    MenuItem searchItem = menu.findItem(R.id.action_search);
    androidx.appcompat.widget.SearchView searchView = (androidx.appcompat.widget.SearchView) searchItem.getActionView();
    searchView.setOnQueryTextListener((androidx.appcompat.widget.SearchView.OnQueryTextListener) this);

    return true;
}

@Override
public boolean onQueryTextChange(String newText) {
    adapter.getFilter().filter(newText);
    return false;
}

@Override
public boolean onQueryTextSubmit(String newText) {
    adapter.getFilter().filter(newText);
    return false;
}

}

huangapple
  • 本文由 发表于 2020年5月31日 04:35:56
  • 转载请务必保留本文链接:https://java.coder-hub.com/62108376.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定