Reputation: 2714
I'm relatively new to Android, and I cannot get my project working.
I only want to display data (thanks to log.v) passed from a search field of my main activity, to another activity here called SearchResultsActivity
. I'm following the official android guide here to do so.
However, I got a SearchView java.lang.NullPointerException instead for this line in my MainActivity.java
, on logcat, when I launch the application:
SearchView searchView = (SearchView) menu.getItem(R.id.searchfield).getActionView();
MainActivity.java:
package com.example.fidbacks_search;
import android.os.Bundle;
import android.app.Activity;
import android.app.SearchManager;
import android.content.Context;
import android.view.Menu;
import android.widget.SearchView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_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.main, menu);
// Get the SearchView and set the searchable configuration
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.getItem(R.id.searchfield).getActionView();
// Assumes current activity is the searchable activity
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
return true;
}
}
SearchResultsActivity.java
package com.example.fidbacks_search;
import android.os.Bundle;
import android.app.Activity;
import android.app.SearchManager;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
public class SearchResultsActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_results);
Log.v("QUERY", "fuque");
// Get the intent, verify the action and get the query
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
Log.v("QUERY", query.toString());
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.search_results, menu);
return true;
}
}
Searchable.xml
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="test"
android:hint="test2" >
</searchable>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.fidbacks_search"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.fidbacks_search.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.fidbacks_search.SearchResultsActivity"
android:label="@string/title_activity_search_results"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable"/>
</activity>
</application>
</manifest>
layout/activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<SearchView
android:id="@+id/searchfield"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="144dp"
android:iconifiedByDefault="false" >
</SearchView>
</RelativeLayout>
Upvotes: 1
Views: 4614
Reputation: 746
use this in your menu.xml :-
<item
android:id="@+id/action_search"
app:showAsAction="collapseActionView|always"
android:title="@string/action_search"
app:actionViewClass="android.support.v7.widget.SearchView"/>
So you need to use app namespace instead of android in the menu item tag :
app:actionViewClass="android.support.v7.widget.SearchView"
Upvotes: 4
Reputation: 18933
The problem is because SearchView UI is in your activity_main.xml while in your java file you are find the id for it using menu item object refernce by this way.
SearchView searchView = (SearchView) menu.getItem(R.id.searchfield).getActionView();
So obviously you get Null pointer exception.
So for solution you have to add it to main.xml file in Menu Item.
<item android:id="@+id/search"
android:title="@string/search_title"
android:icon="@drawable/ic_search"
android:showAsAction="collapseActionView|ifRoom"
android:actionViewClass="android.widget.SearchView" />
If you don't need it on menu item then you can't add on onCreateOptionsMenu. You have to use it on oncreate() method. But i think Search menu will always be on Menu Item.
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) findViewById(R.id.searchfield);
// Assumes current activity is the searchable activity
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
Upvotes: 3
Reputation: 315
In Debug Check if menu.getItem(R.id.searchfield) is defined. When you call a method of inexistent element you will get nullPoint Exception. In addition, is a best pratic always check if element exist before call your methods.
Upvotes: 1