gpt4 book ai didi

android - SearchView AppCompatActivity 中的空指针?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:03:21 24 4
gpt4 key购买 nike

我只是一个初学者,我在这里搜索了这个问题,发现了 4-5 个问题,但这对我没有帮助。我只是在 Actionbar 中尝试 searchView,我的 Activity 是 AppCompatActivity,我的代码在下面。我在这个 Activity 中使用工具栏。并使用这个 library也。创建选项菜单:

 @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$1.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 - SearchView AppCompatActivity 中的空指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30641568/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com