在 ActionBar 中使用 SearchView
Using SearchView in ActionBar
我正在尝试 运行 一个简单的代码,当用户单击 Search
action
.
时显示 SearchView
当我 运行 application
时,它以 致命异常.
关闭
我的代码
MainActivity.java
package luizugliano.com.br.customactionbar;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.SearchView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem item = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) item.getActionView();
searchView.setOnQueryTextListener(onSearch());
return true;
}
private SearchView.OnQueryTextListener onSearch() {
return new SearchView.OnQueryTextListener(){
@Override
public boolean onQueryTextSubmit(String query) {
// Usuário fez a busca
toastMessage("Buscar o texto: " + query);
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
// Mudou o texto digitado
return false;
}
};
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_search) {
toastMessage("Search!");
return true;
} else if (id == R.id.action_refresh){
toastMessage("Refresh!");
return true;
} else if (id == R.id.action_settings){
toastMessage("Settings!");
return true;
}
return super.onOptionsItemSelected(item);
}
private void toastMessage(String message){
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
}
menu_main.xml
<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=".MainActivity">
<item android:id="@+id/action_search" android:title="@string/action_search"
android:orderInCategory="100" android:icon="@drawable/ic_action_search" app:showAsAction="always"
android:actionViewClass="android.widget.SearchView"/>
<item android:id="@+id/action_refresh" android:title="@string/action_refresh"
android:orderInCategory="100" android:icon="@drawable/ic_action_refresh" app:showAsAction="always" />
<item android:id="@+id/action_settings" android:title="@string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />
logcat信息
10-20 00:30:21.327 806-806/luizugliano.com.br.customactionbar D/AndroidRuntime: Shutting down VM
10-20 00:30:21.327 806-806/luizugliano.com.br.customactionbar E/AndroidRuntime: FATAL EXCEPTION: main
10-20 00:30:21.327 806-806/luizugliano.com.br.customactionbar E/AndroidRuntime: Process: luizugliano.com.br.customactionbar, PID: 806
10-20 00:30:21.327 806-806/luizugliano.com.br.customactionbar E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.SearchView.setOnQueryTextListener(android.widget.SearchView$OnQueryTextListener)' on a null object reference
10-20 00:30:21.327 806-806/luizugliano.com.br.customactionbar E/AndroidRuntime: at luizugliano.com.br.customactionbar.MainActivity.onCreateOptionsMenu(MainActivity.java:29)
10-20 00:30:21.327 806-806/luizugliano.com.br.customactionbar E/AndroidRuntime: at android.app.Activity.onCreatePanelMenu(Activity.java:2846)
10-20 00:30:21.327 806-806/luizugliano.com.br.customactionbar E/AndroidRuntime: at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:298)
10-20 00:30:21.327 806-806/luizugliano.com.br.customactionbar E/AndroidRuntime: at android.support.v7.internal.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:85)
10-20 00:30:21.327 806-806/luizugliano.com.br.customactionbar E/AndroidRuntime: at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:243)
10-20 00:30:21.327 806-806/luizugliano.com.br.customactionbar E/AndroidRuntime: at android.support.v7.internal.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:85)
10-20 00:30:21.327 806-806/luizugliano.com.br.customactionbar E/AndroidRuntime: at android.support.v7.internal.app.ToolbarActionBar.populateOptionsMenu(ToolbarActionBar.java:448)
10-20 00:30:21.327 806-806/luizugliano.com.br.customactionbar E/AndroidRuntime: at android.support.v7.internal.app.ToolbarActionBar.run(ToolbarActionBar.java:65)
10-20 00:30:21.327 806-806/luizugliano.com.br.customactionbar E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:739)
10-20 00:30:21.327 806-806/luizugliano.com.br.customactionbar E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
10-20 00:30:21.327 806-806/luizugliano.com.br.customactionbar E/AndroidRuntime: at android.os.Looper.loop(Looper.java:148)
10-20 00:30:21.327 806-806/luizugliano.com.br.customactionbar E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5417)
10-20 00:30:21.327 806-806/luizugliano.com.br.customactionbar E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
10-20 00:30:21.327 806-806/luizugliano.com.br.customactionbar E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
10-20 00:30:21.327 806-806/luizugliano.com.br.customactionbar E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
10-20 00:30:21.686 806-820/luizugliano.com.br.customactionbar I/art: Background partial concurrent mark sweep GC freed 205(8KB) AllocSpace objects, 0(0B) LOS objects, 40% free, 5MB/8MB, paused 41.133ms total 119.832ms
10-20 00:30:35.570 806-806/? I/Process: Sending signal. PID: 806 SIG: 9
您需要与 Java 和 XML 代码库中搜索视图访问支持库的使用保持一致。不正确使用它会导致 NPE。
错误信息 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.SearchView.setOnQueryTextListener(android.widget.SearchView$OnQueryTextListener)' on a null object reference 表示正在尝试调用 在 null searchView 上设置 OnQueryTextListener。
在你的menu_main.xml中,更改
android:actionViewClass="android.widget.SearchView"
到
app:actionViewClass="android.support.v7.widget.SearchView"
然后在您的 MainActivity.java 中更改
SearchView searchView = (SearchView) item.getActionView();
和
SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search));
我正在尝试 运行 一个简单的代码,当用户单击 Search
action
.
SearchView
当我 运行 application
时,它以 致命异常.
我的代码
MainActivity.java
package luizugliano.com.br.customactionbar;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.SearchView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem item = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) item.getActionView();
searchView.setOnQueryTextListener(onSearch());
return true;
}
private SearchView.OnQueryTextListener onSearch() {
return new SearchView.OnQueryTextListener(){
@Override
public boolean onQueryTextSubmit(String query) {
// Usuário fez a busca
toastMessage("Buscar o texto: " + query);
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
// Mudou o texto digitado
return false;
}
};
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_search) {
toastMessage("Search!");
return true;
} else if (id == R.id.action_refresh){
toastMessage("Refresh!");
return true;
} else if (id == R.id.action_settings){
toastMessage("Settings!");
return true;
}
return super.onOptionsItemSelected(item);
}
private void toastMessage(String message){
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
}
menu_main.xml
<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=".MainActivity">
<item android:id="@+id/action_search" android:title="@string/action_search"
android:orderInCategory="100" android:icon="@drawable/ic_action_search" app:showAsAction="always"
android:actionViewClass="android.widget.SearchView"/>
<item android:id="@+id/action_refresh" android:title="@string/action_refresh"
android:orderInCategory="100" android:icon="@drawable/ic_action_refresh" app:showAsAction="always" />
<item android:id="@+id/action_settings" android:title="@string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />
logcat信息
10-20 00:30:21.327 806-806/luizugliano.com.br.customactionbar D/AndroidRuntime: Shutting down VM 10-20 00:30:21.327 806-806/luizugliano.com.br.customactionbar E/AndroidRuntime: FATAL EXCEPTION: main 10-20 00:30:21.327 806-806/luizugliano.com.br.customactionbar E/AndroidRuntime: Process: luizugliano.com.br.customactionbar, PID: 806 10-20 00:30:21.327 806-806/luizugliano.com.br.customactionbar E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.SearchView.setOnQueryTextListener(android.widget.SearchView$OnQueryTextListener)' on a null object reference 10-20 00:30:21.327 806-806/luizugliano.com.br.customactionbar E/AndroidRuntime: at luizugliano.com.br.customactionbar.MainActivity.onCreateOptionsMenu(MainActivity.java:29) 10-20 00:30:21.327 806-806/luizugliano.com.br.customactionbar E/AndroidRuntime: at android.app.Activity.onCreatePanelMenu(Activity.java:2846) 10-20 00:30:21.327 806-806/luizugliano.com.br.customactionbar E/AndroidRuntime: at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:298) 10-20 00:30:21.327 806-806/luizugliano.com.br.customactionbar E/AndroidRuntime: at android.support.v7.internal.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:85) 10-20 00:30:21.327 806-806/luizugliano.com.br.customactionbar E/AndroidRuntime: at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:243) 10-20 00:30:21.327 806-806/luizugliano.com.br.customactionbar E/AndroidRuntime: at android.support.v7.internal.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:85) 10-20 00:30:21.327 806-806/luizugliano.com.br.customactionbar E/AndroidRuntime: at android.support.v7.internal.app.ToolbarActionBar.populateOptionsMenu(ToolbarActionBar.java:448) 10-20 00:30:21.327 806-806/luizugliano.com.br.customactionbar E/AndroidRuntime: at android.support.v7.internal.app.ToolbarActionBar.run(ToolbarActionBar.java:65) 10-20 00:30:21.327 806-806/luizugliano.com.br.customactionbar E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:739) 10-20 00:30:21.327 806-806/luizugliano.com.br.customactionbar E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95) 10-20 00:30:21.327 806-806/luizugliano.com.br.customactionbar E/AndroidRuntime: at android.os.Looper.loop(Looper.java:148) 10-20 00:30:21.327 806-806/luizugliano.com.br.customactionbar E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5417) 10-20 00:30:21.327 806-806/luizugliano.com.br.customactionbar E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method) 10-20 00:30:21.327 806-806/luizugliano.com.br.customactionbar E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 10-20 00:30:21.327 806-806/luizugliano.com.br.customactionbar E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 10-20 00:30:21.686 806-820/luizugliano.com.br.customactionbar I/art: Background partial concurrent mark sweep GC freed 205(8KB) AllocSpace objects, 0(0B) LOS objects, 40% free, 5MB/8MB, paused 41.133ms total 119.832ms 10-20 00:30:35.570 806-806/? I/Process: Sending signal. PID: 806 SIG: 9
您需要与 Java 和 XML 代码库中搜索视图访问支持库的使用保持一致。不正确使用它会导致 NPE。
错误信息 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.SearchView.setOnQueryTextListener(android.widget.SearchView$OnQueryTextListener)' on a null object reference 表示正在尝试调用 在 null searchView 上设置 OnQueryTextListener。
在你的menu_main.xml中,更改
android:actionViewClass="android.widget.SearchView"
到
app:actionViewClass="android.support.v7.widget.SearchView"
然后在您的 MainActivity.java 中更改
SearchView searchView = (SearchView) item.getActionView();
和
SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search));