SearchView AppCompatActivity 中的空指针?
Null Pointer in SearchView AppCompatActivity?
我只是一个初学者,我在这里搜索了这个问题,发现了 4-5 个问题,但这并没有帮助 me.I 只是在 Actionbar 中尝试 searchView,我的 activity 是 AppCompatActivity 我的代码是 below.I 正在使用 activity 中的工具栏。并且也使用这个 library。
OncreatOptionMenu:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
SearchManager SManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
MenuItem searchMenuItem = menu.findItem(R.id.action_search);
SearchView searchViewAction = (SearchView) MenuItemCompat.getActionView(searchMenuItem);
searchViewAction.setSearchableInfo(SManager.getSearchableInfo(getComponentName()));
searchViewAction.setIconifiedByDefault(false);
return super.onCreateOptionsMenu(menu);
}
Xml:
<item android:id="@+id/action_search"
android:title="action_search"
MainActivity:showAsAction="ifRoom"
MainActivity:actionViewClass="android.support.v7.widget.SearchView" />
和logcat:
java.lang.NullPointerException
at algonation.com.spm.MainActivity.onCreateOptionsMenu(MainActivity.java:183)
at android.app.Activity.onCreatePanelMenu(Activity.java:2571)
at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:277)
at android.support.v7.internal.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:84)
at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallback.onCreatePanelMenu(AppCompatDelegateImplBase.java:251)
at android.support.v7.internal.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:84)
at android.support.v7.internal.app.ToolbarActionBar.populateOptionsMenu(ToolbarActionBar.java:449)
at android.support.v7.internal.app.ToolbarActionBar.run(ToolbarActionBar.java:66)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5292)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
at dalvik.system.NativeStart.main(Native Method)
如果您需要 MainActivity 的完整代码,请告诉我..
非常感谢。
您收到 NullPointerException 因为下面一行返回 null。
SManager.getSearchableInfo(getComponentName()
你需要
1 - Declare the activity(which needs to perform search) to accept the ACTION_SEARCH intent, in an <intent-filter> element.
2 - Specify the searchable configuration to use, in a <meta-data> element.
假设您正在 MainActivity 中执行搜索(即您想要的 AppCompatActivity),请参见下面的 示例:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my_main, menu);
MenuItem searchMenuItem = menu.findItem(R.id.action_search);
SearchView searchViewAction = (SearchView) MenuItemCompat
.getActionView(searchMenuItem);
// Get the SearchView and set the searchable configuration
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchableInfo searchableInfo = searchManager
.getSearchableInfo(getComponentName());
searchViewAction.setSearchableInfo(searchableInfo);
searchViewAction.setIconifiedByDefault(false);
return true;
}
@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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
AndroidManifest.xml
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
</application>
menu/activity_main.xml
<item
android:id="@+id/action_search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="ifRoom"
android:title="@string/action_search"/>
res/xml/searchable.xml
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_label"
android:hint="@string/search_hint" >
</searchable>
希望它能解决您的问题并供参考see here。
实际上,只要确保您导入的是 "android.support.v7.widget.SearchView" 而不是 "android.widget.SearchView"
import android.support.v7.widget.SearchView
这个帖子已经过半了,但回复对我不起作用。
对我有用的是将 "app" 更改为 "appcompat"。
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:appcompat="http://schemas.android.com/apk/res-auto"
<item
android:id="@+id/action_search"
android:title="@string/search_title"
appcompat:actionViewClass="android.support.v7.widget.SearchView"
appcompat:showAsAction="always"/>
</menu>
对不起我的英语。
我只是一个初学者,我在这里搜索了这个问题,发现了 4-5 个问题,但这并没有帮助 me.I 只是在 Actionbar 中尝试 searchView,我的 activity 是 AppCompatActivity 我的代码是 below.I 正在使用 activity 中的工具栏。并且也使用这个 library。 OncreatOptionMenu:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
SearchManager SManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
MenuItem searchMenuItem = menu.findItem(R.id.action_search);
SearchView searchViewAction = (SearchView) MenuItemCompat.getActionView(searchMenuItem);
searchViewAction.setSearchableInfo(SManager.getSearchableInfo(getComponentName()));
searchViewAction.setIconifiedByDefault(false);
return super.onCreateOptionsMenu(menu);
}
Xml:
<item android:id="@+id/action_search"
android:title="action_search"
MainActivity:showAsAction="ifRoom"
MainActivity:actionViewClass="android.support.v7.widget.SearchView" />
和logcat:
java.lang.NullPointerException
at algonation.com.spm.MainActivity.onCreateOptionsMenu(MainActivity.java:183)
at android.app.Activity.onCreatePanelMenu(Activity.java:2571)
at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:277)
at android.support.v7.internal.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:84)
at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallback.onCreatePanelMenu(AppCompatDelegateImplBase.java:251)
at android.support.v7.internal.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:84)
at android.support.v7.internal.app.ToolbarActionBar.populateOptionsMenu(ToolbarActionBar.java:449)
at android.support.v7.internal.app.ToolbarActionBar.run(ToolbarActionBar.java:66)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5292)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
at dalvik.system.NativeStart.main(Native Method)
如果您需要 MainActivity 的完整代码,请告诉我.. 非常感谢。
您收到 NullPointerException 因为下面一行返回 null。
SManager.getSearchableInfo(getComponentName()
你需要
1 - Declare the activity(which needs to perform search) to accept the ACTION_SEARCH intent, in an <intent-filter> element.
2 - Specify the searchable configuration to use, in a <meta-data> element.
假设您正在 MainActivity 中执行搜索(即您想要的 AppCompatActivity),请参见下面的 示例:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my_main, menu);
MenuItem searchMenuItem = menu.findItem(R.id.action_search);
SearchView searchViewAction = (SearchView) MenuItemCompat
.getActionView(searchMenuItem);
// Get the SearchView and set the searchable configuration
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchableInfo searchableInfo = searchManager
.getSearchableInfo(getComponentName());
searchViewAction.setSearchableInfo(searchableInfo);
searchViewAction.setIconifiedByDefault(false);
return true;
}
@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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
AndroidManifest.xml
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
</application>
menu/activity_main.xml
<item
android:id="@+id/action_search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="ifRoom"
android:title="@string/action_search"/>
res/xml/searchable.xml
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_label"
android:hint="@string/search_hint" >
</searchable>
希望它能解决您的问题并供参考see here。
实际上,只要确保您导入的是 "android.support.v7.widget.SearchView" 而不是 "android.widget.SearchView"
import android.support.v7.widget.SearchView
这个帖子已经过半了,但回复对我不起作用。 对我有用的是将 "app" 更改为 "appcompat"。
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:appcompat="http://schemas.android.com/apk/res-auto"
<item
android:id="@+id/action_search"
android:title="@string/search_title"
appcompat:actionViewClass="android.support.v7.widget.SearchView"
appcompat:showAsAction="always"/>
</menu>
对不起我的英语。