尝试在操作栏上的活动中添加搜索栏时出错
Android Manifest:
</activity>
<activity android:name=".AllTransactions"
android:launchMode="singleTop"
android:theme="@style/Theme.AppCompat.DayNight.DarkActionBar"
>
<intent-filter>
<action android:name="android.intent.action.SEARCH"
/>
</intent-filter>
<meta-data android:name="android.app.default_searchable"
android:value="com.cybussolutions.wikki.afry_pay.AllTransactions" />
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable"/>
Activity.java:
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.SearchView;
import java.util.List;
/**
* Created by Admin on 3/11/2016.
*/
public class AllTransactions extends AppCompatActivity{
private static final String TAG =null ;
private List<String> items;
private Menu menu;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_all_transactions);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflator=getMenuInflater();
menuInflator.inflate(R.menu.options_menu,menu);
SearchManager searchManager=(SearchManager) getSystemService(Context.SEARCH_SERVICE);
MenuItem searchActionbarItem=menu.findItem(R.id.search);
SearchView searchView=(SearchView) MenuItemCompat.getActionView(searchActionbarItem);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(true);
return true;
}
protected void handleIntent(Intent intent){
if (Intent.ACTION_SEARCH.equals(intent.getAction())){
String searchQuery=intent.getStringExtra(SearchManager.QUERY);
}
}
protected void newIntent (Intent intent){
Log.d(TAG,"on New Intent");
setIntent(intent);
handleIntent(intent);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.search:
onSearchRequested();
return true;
}
return false;
}
}
这是带有java代码的Mainfest文件。错误日志如下: 错误日志:
: FATAL EXCEPTION: main
java.lang.NullPointerException
at com.cybussolutions.wikki.afri_pay.AllTransactions.onCreateOptionsMenu(AllTransactions.java:41)
at android.app.Activity.onCreatePanelMenu(Activity.java:2476)
at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:341)
at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:85)
at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:258)
at android.support.v7.app.AppCompatDelegateImplV7.preparePanel(AppCompatDelegateImplV7.java:1303)
at android.support.v7.app.AppCompatDelegateImplV7.doInvalidatePanelMenu(AppCompatDelegateImplV7.java:1583)
at android.support.v7.app.AppCompatDelegateImplV7.access$100(AppCompatDelegateImplV7.java:89)
at android.support.v7.app.AppCompatDelegateImplV7$1.run(AppCompatDelegateImplV7.java:128)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
我找不到原因,我所遵循的教程是http://www.101apps.co.za/index.php/articles/using-search-in-your-apps-a-tutorial.html
在活动中添加搜索栏的正确步骤是什么。现在等待您的帮助..
答案 0 :(得分:1)
您在onCreateOptionsMenu()方法中缺少超级调用。它的签名看起来像这样
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return super.onCreateOptionsMenu(menu);
}
并像这样实例化您的搜索视图
SearchView actionSearchView = (SearchView) searchItem.getActionView();