布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/llist"
tools:context="com.android.parteek.contentprovider.Main2Activity">
<android.support.v7.widget.SearchView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/serch">
</android.support.v7.widget.SearchView>
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.android.parteek.contentprovider.Main2Activity">
</ListView>
</LinearLayout>
代码
package com.android.parteek.contentprovider;
import android.content.ContentResolver;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.SearchView;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
public class Main2Activity extends AppCompatActivity implements AdapterView.OnItemLongClickListener, SearchView.OnQueryTextListener {
ListView l;
ArrayList<Bean> arrayList;
UserAdapterr userAdapterr;
ContentResolver contentResolver;
SearchView searchView;
Bean b,b1;
int pos;
void views(){
l=(ListView)findViewById(R.id.list);
searchView=(SearchView)findViewById(R.id.serch);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
views();
contentResolver=getContentResolver();
retrieve();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.searchmenu,menu);
searchView=(SearchView)menu.findItem(R.id.search).getActionView();
searchView.setIconifiedByDefault(true);
searchView.setOnQueryTextListener(this);
searchView.setQueryHint("search.....");
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
return false;
}
}
错误
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.SearchView.setIconifiedByDefault(boolean)' on a null object reference
at com.android.parteek.contentprovider.Main2Activity.onCreateOptionsMenu(Main2Activity.java:147)
答案 0 :(得分:0)
如果您想在操作栏中使用它,请将其从活动中删除。将其添加到searchmenu.xml
。
您的错误与这些行相关,因此请修复您正在膨胀的xml文件
getMenuInflater().inflate(R.menu.searchmenu,menu);
searchView=(SearchView)menu.findItem(R.id.search).getActionView();
searchView.setIconifiedByDefault(true);
您可以有效删除此搜索视图
void views(){
l=(ListView)findViewById(R.id.list);
// searchView=(SearchView)findViewById(R.id.serch);
}