我已经尝试了很多教程,stackoverflow解决方案和Google教程 – > https://www.youtube.com/watch?v=9OWmnYPX1uc但是我无法在我的 *** 作栏中使用searchvIEw来工作.
有谁看到我做错了什么?
我总是在searchVIEw上得到nullpointerexception
我对ListvIEw的活动:
@OverrIDepublic boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.List_menu, menu); MenuItem searchItem = menu.findItem(R.ID.action_search); SearchVIEw searchVIEw = (SearchVIEw) MenuItemCompat.getActionVIEw(searchItem); searchVIEw.setonqueryTextListener( new SearchVIEw.OnqueryTextListener() { @OverrIDe public boolean onqueryTextsubmit(String query) { Toast.makeText(ContactListActivity.this, query, Toast.LENGTH_SHORT).show(); return false; } @OverrIDe public boolean onqueryTextChange(String newText) { Toast.makeText(ContactListActivity.this, newText, Toast.LENGTH_SHORT).show(); return false; } } ); SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); Componentname componentname = new Componentname(getApplicationContext(), TorrentListActivity.class); searchVIEw.setSearchableInfo(searchManager.getSearchableInfo(componentname)); return true;}
Searchable.xml
<?xml version="1.0" enCoding="utf-8"?><searchable androID:label="@string/app_name" xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:hint="Search" androID:voiceSearchMode="showVoiceSearchbutton|launchRecognizer"/>
List_menu.xml
<?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" xmlns:tools="http://schemas.androID.com/tools" tools:context="be.vanlooverenkoen.torrentsearch.TorrentListActivity"> <item androID:ID="@+ID/action_search" androID:title="Search" androID:icon="@drawable/ic_action_search" app:showAsAction="ifRoom|collapseActionVIEw" androID:actionVIEwClass="androID.support.v7.Widget.SearchVIEw"/> <item androID:ID="@+ID/action_settings" androID:orderIncategory="100" androID:title="@string/menu_settings" app:showAsAction="never" /></menu>
编辑1
我不是关于1活动的建议,而是关于我需要过滤列表视图列表的另一个活动
**编辑2 **
import androID.content.Context;import androID.util.Log;import androID.vIEw.LayoutInflater;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.Widget.BaseAdapter;import androID.Widget.ImageVIEw;import androID.Widget.TextVIEw;import java.util.linkedList;import be.vanlooverenkoen.torrentsearch.CommonUtilitIEs.CommonUtilitIEs;import be.vanlooverenkoen.torrentsearch.model.Torrent;/** * Created by Koen on 19/03/2016. */public class Torrentlistadapter extends BaseAdapter { private linkedList<Torrent> torrents; private LayoutInflater layoutInflater; public Torrentlistadapter(Context context, linkedList<Torrent> torrents) { this.torrents = torrents; layoutInflater = LayoutInflater.from(context); } @OverrIDe public int getCount() { return torrents.size(); } @OverrIDe public Object getItem(int position) { return torrents.get(position); } @OverrIDe public long getItemID(int position) { return position; } @OverrIDe public VIEw getVIEw(int position, VIEw convertVIEw, VIEwGroup parent) { VIEwHolder holder; if (convertVIEw == null) { convertVIEw = layoutInflater.inflate(R.layout.List_row_layout, null); holder = new VIEwHolder(); holder.TitleVIEw = (TextVIEw) convertVIEw.findVIEwByID(R.ID.txt_Title); holder.seedVIEw = (TextVIEw) convertVIEw.findVIEwByID(R.ID.txt_seedsValue); holder.peerVIEw = (TextVIEw) convertVIEw.findVIEwByID(R.ID.txt_peersValue); holder.sizeVIEw = (TextVIEw) convertVIEw.findVIEwByID(R.ID.txt_sizeValue); holder.dateVIEw = (TextVIEw) convertVIEw.findVIEwByID(R.ID.txt_datevalue); holder.torrentTypeTitleVIEw = (TextVIEw) convertVIEw.findVIEwByID(R.ID.torrenttypeTitle); holder.torrentTypeVIEw = (TextVIEw) convertVIEw.findVIEwByID(R.ID.torrentTypeVIEw); holder.verifIEdimg = (ImageVIEw) convertVIEw.findVIEwByID(R.ID.verifIEd); convertVIEw.setTag(holder); } else { holder = (VIEwHolder) convertVIEw.getTag(); } Torrent torrent = torrents.get(position); holder.TitleVIEw.setText(torrent.getTitle()); Log.d("Seeds", String.valueOf(holder.TitleVIEw.getText())); holder.seedVIEw.setText(String.valueOf(torrent.getSeeds())); holder.peerVIEw.setText(String.valueOf(torrent.getPeers())); holder.sizeVIEw.setText(torrent.getSize()); holder.dateVIEw.setText(torrent.getDateOfRelease()); if(CommonUtilitIEs.checkForShowTorrentType(layoutInflater.getContext())){ holder.torrentTypeVIEw.setText(torrent.getTorrentType()); holder.torrentTypeTitleVIEw.setText("Torrent Type:"); }else{ holder.torrentTypeVIEw.setText(""); holder.torrentTypeTitleVIEw.setText(""); } if (torrent.isLegitUploader()) { holder.verifIEdimg.setimageResource(R.drawable.verifIEd); }else{ holder.verifIEdimg.setimageDrawable(null); } return convertVIEw; } static class VIEwHolder { TextVIEw TitleVIEw; TextVIEw seedVIEw; TextVIEw peerVIEw; TextVIEw sizeVIEw; TextVIEw dateVIEw; TextVIEw torrentTypeTitleVIEw; TextVIEw torrentTypeVIEw; ImageVIEw verifIEdimg; }}
编辑3
FATAL EXCEPTION: main Process: be.vanlooverenkoen.torrentsearch, PID: 10509 java.lang.NullPointerException: Attempt to invoke virtual method 'voID androID.support.v7.Widget.SearchVIEw.setonqueryTextListener(androID.support.v7.Widget.SearchVIEw$OnqueryTextListener)' on a null object reference at be.vanlooverenkoen.torrentsearch.TorrentListActivity.onCreateOptionsMenu(TorrentListActivity.java:105) at androID.app.Activity.onCreatePanelMenu(Activity.java:2852) at androID.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:298) at androID.support.v7.vIEw.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:85) at androID.support.v7.app.AppCompatDelegateImplBase$AppCompatwindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:241) at androID.support.v7.app.AppCompatDelegateImplV7.preparePanel(AppCompatDelegateImplV7.java:1273) at androID.support.v7.app.AppCompatDelegateImplV7.doInvalIDatePanelMenu(AppCompatDelegateImplV7.java:1553) at androID.support.v7.app.AppCompatDelegateImplV7.access0(AppCompatDelegateImplV7.java:89) at androID.support.v7.app.AppCompatDelegateImplV7.run(AppCompatDelegateImplV7.java:129) at androID.os.Handler.handleCallback(Handler.java:739) at androID.os.Handler.dispatchMessage(Handler.java:95) at androID.os.Looper.loop(Looper.java:148) at androID.app.ActivityThread.main(ActivityThread.java:5422) at java.lang.reflect.Method.invoke(Native Method) at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:616)
解
我的天啊
机器人:actionVIEwClass = “androID.support.v7.Widget.SearchVIEw”
一定是
应用程式:actionVIEwClass = “androID.support.v7.Widget.SearchVIEw”
然后噗它工作
解决方法:
在List_menu.xml中,
androID:actionVIEwClass="androID.support.v7.Widget.SearchVIEw"
应改为:
app:actionVIEwClass="androID.support.v7.Widget.SearchVIEw"
总结 以上是内存溢出为你收集整理的android – 无法在 *** 作栏中获取SearchView全部内容,希望文章能够帮你解决android – 无法在 *** 作栏中获取SearchView所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)