android searchview getSearchableInfo throws nullpointer exception - java

I was trying to implement sample search interface in MainActivity with searchview widget. But it keeps throwing null pointer exception at getSearchableInfo in the onCreateOptionsMenu method of MainActivity.
I already have tried the following things, but nothing has helped. Its just a sample code and its giving me a hard time.:
Using android.support.v7.widget.SearchView or using android.widget.SearchView in menu xml item and the onCreateOptionsMenu of MainActivity.
Using MenuItemCompat or using menu in onCreateOptionsMenu.
Using just getComponentName() or using new ComponentName(this, SearchActivity.class).
Using full path or using relative path for SearchActivity in AndroidManifest.
Adding following line in proguard-android.txt : -keep class android.support.v7.widget.SearchView { *; }
Here is the stacktrace:
Process: com.mycompany.testsearch, PID: 17480
java.lang.NullPointerException
at com.mycompany.testsearch.MainActivity.onCreateOptionsMenu(MainActivity.java:34)
at android.app.Activity.onCreatePanelMenu(Activity.java:2546)
at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:275)
at android.support.v7.app.ActionBarActivity.superOnCreatePanelMenu(ActionBarActivity.java:276)
at android.support.v7.app.ActionBarActivityDelegate$1.onCreatePanelMenu(ActionBarActivityDelegate.java:79)
at android.support.v7.app.ActionBarActivityDelegateBase.preparePanel(ActionBarActivityDelegateBase.java:979)
at android.support.v7.app.ActionBarActivityDelegateBase.doInvalidatePanelMenu(ActionBarActivityDelegateBase.java:1182)
at android.support.v7.app.ActionBarActivityDelegateBase.access$100(ActionBarActivityDelegateBase.java:79)
at android.support.v7.app.ActionBarActivityDelegateBase$1.run(ActionBarActivityDelegateBase.java:118)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5086)
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:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Here are my code files:
1] AndroidManifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mycompany.testsearch" >
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<meta-data
android:name="android.app.default_searchable"
android:value=".SearchableActivity" />
<activity
android:name=".SearchableActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable"
android:value=".activities.SearchableActivity" />
</activity>
<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>
</activity>
</application>
2] Main_menu.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:TestSearch="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/my_exp_search"
android:title="action bar search"
android:icon="#android:drawable/ic_menu_search"
TestSearch:showAsAction="always"
TestSearch:actionViewClass="android.support.v7.widget.SearchView" />
<item
android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="100"
TestSearch:showAsAction="never" />
</menu>
3] Searchable.xml:
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/app_name"
android:hint="#string/hint_text"
android:includeInGlobalSearch="true">
</searchable>
4] MainActivity:
public class MainActivity extends ActionBarActivity {
#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.menu_main, menu);
//SearchManager searchManager =
//(SearchManager)getSystemService(Context.SEARCH_SERVICE);
//SearchView searchView =
//(SearchView)menu.findItem(R.id.my_exp_search).getActionView();
MenuItem searchItem = menu.findItem(R.id.my_exp_search);
SearchView searchView =
(SearchView)MenuItemCompat.getActionView(searchItem);
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
// String pkg = "com.mycompany.testsearch";
// String cls = "com.mycompany.testsearch.SearchActivity";
// ComponentName mycomponent = new ComponentName(pkg,cls);
searchView.setSearchableInfo(
searchManager.getSearchableInfo(mycomponent));
searchView.setIconifiedByDefault(false);
return super.onCreateOptionsMenu(menu);
}
#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_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
5] SearchActivity:
public class SearchableActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_searchable);
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
Log.d("test","Reached searchable");
String query = intent.getStringExtra(SearchManager.QUERY);
TextView textView =
(TextView)findViewById(R.id.text_message);
textView.setText(query);
}
}
#Override
protected void onNewIntent(Intent intent) {
setIntent(intent);
handleIntent(intent);
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
TextView textView =
(TextView)findViewById(R.id.text_message);
textView.setText(query);
}
}
#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_searchable, menu);
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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Please help me resolve this issue.

Related

App not displaying anything when run

I am only a day old in using android studio. I have to create an app for my school project, i did that, i programmed it in android studio and it had no errors. Please help,
this is the Main activity named frontpagee
package com.example.lenovo.shop_easy;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class frontpagee extends Activity
{
public void sendMessage(View view){
Intent intent = new Intent(frontpagee.this, catalog.class);
startActivity(intent);
}
#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_frontpagee, menu);
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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
the XML goes like this
i want to move from this activity to the second ACTIVITY which is named Catalog
java is
package com.example.lenovo.shop_easy;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class catalog extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_catalog);
}
#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_catalog, menu);
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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void sendMessage(View view) {
Intent intent = new Intent(catalog.this, product1.class);
startActivity(intent);
}
public void sendMessage1(View view) {
Intent intent = new Intent(catalog.this, PRODUCT2.class);
startActivity(intent);
}
public void sendMessage2(View view) {
Intent intent = new Intent(catalog.this, PRODUCT3.class);
startActivity(intent);
}
public void sendMessage3(View view) {
Intent intent = new Intent(catalog.this, product4.class);
startActivity(intent);
}
public void sendMessage4(View view) {
Intent intent = new Intent(catalog.this, product5.class);
startActivity(intent);
}
public void sendMessage5(View view) {
Intent intent = new Intent(catalog.this, product6.class);
startActivity(intent);
}
public void sendMessage6(View view) {
Intent intent = new Intent(catalog.this, product7.class);
startActivity(intent);
}
public void sendMessage7(View view) {
Intent intent = new Intent(catalog.this, product8.class);
startActivity(intent);
}
public void sendMessage8(View view) {
Intent intent = new Intent(catalog.this, product9.class);
startActivity(intent);
}
public void sendMessage9(View view) {
Intent intent = new Intent(catalog.this, product10.class);
startActivity(intent);
}
}
<FrameLayout 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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".frontpagee"
android:background="#drawable/back">
<ImageView
android:layout_width="472dp"
android:layout_height="500dp"
android:id="#+id/imageView11"
android:src="#mipmap/cover"
android:layout_gravity="center_horizontal|top"
android:contentDescription="#string/content" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/text12"
android:id="#+id/button11"
android:layout_gravity="center_horizontal|bottom"
android:textSize="#dimen/size2"
android:clickable="true"
android:onClick="sendMessage"/>
</FrameLayout>
the code is correct says android studio but when i run the adb on my phone it installs the app, everything goes fine but when i open the app it displays nothing but only the app icon and name at the top and nothing else what to do. please help
and here the main xml file also
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.lenovo.shop_easy" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".frontpagee"
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=".catalog"
android:label="#string/title_activity_catalog" >
</activity>
<activity
android:name=".product1"
android:label="#string/title_activity_product1" >
</activity>
<activity
android:name=".PRODUCT2"
android:label="#string/title_activity_product2" >
</activity>
<activity
android:name=".PRODUCT3"
android:label="#string/title_activity_product3" >
</activity>
<activity
android:name=".product4"
android:label="#string/title_activity_product4" >
</activity>
<activity
android:name=".product5"
android:label="#string/title_activity_product5" >
</activity>
<activity
android:name=".product6"
android:label="#string/title_activity_product6" >
</activity>
<activity
android:name=".product7"
android:label="#string/title_activity_product7" >
</activity>
<activity
android:name=".product8"
android:label="#string/title_activity_product8" >
</activity>
<activity
android:name=".product9"
android:label="#string/title_activity_product9" >
</activity>
<activity
android:name=".product10"
android:label="#string/title_activity_product10" >
</activity>
</application>
</manifest>
what to do
You have not overridden the onCreate(Bundle savedInstance) method in class frontpagee and have not called setContentView(R.layout...) in it.

get map from geoserver to argis sdk android

I can't getMap from geoserver 2.7 to my Android application using argis SDK in Android studio 1.1.0 i try to get the map via WMS (Web Map Service) to my android app her's the code below
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import com.esri.android.map.MapView;
import com.esri.android.map.ogc.WMSLayer;
public class MainActivity extends Activity {
MapView mMapView;
WMSLayer wmsLayer;
String wmsURL;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// after the content of this activity is set
// the map can be accessed from the layout
mMapView = (MapView)findViewById(R.id.map);
// set up the wms url
wmsURL ="http://10.1.1.100:8090/geoserver/cyobjet/wms?service=WMS" +
"&version=1.1.0" +
"&request=GetMap" +
"&layers=cyobjet:object" +
"&styles=" +
"&bbox=-20.8250007629395,-9.94999980926514,4185.8251953125,1999.94995117188" +
"&width=690&height=330" +
"&srs=EPSG:900913" +
"&format=image%2Fpng";
wmsLayer = new WMSLayer(wmsURL);
wmsLayer.setImageFormat("image/png");
// available layers
String[] visibleLayers = {"cyobject:object"};
wmsLayer.setVisibleLayer(visibleLayers);
wmsLayer.setOpacity(0.5f);
mMapView.addLayer(wmsLayer);
// Set the Esri logo to be visible, and enable map to wrap around date line.
mMapView.setEsriLogoVisible(true);
mMapView.enableWrapAround(true);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
and My Androidmanifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pfe.loungou.map" >
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
android:allowBackup="true"
android:icon="#mipmap/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>
</activity>
</application>
</manifest>
and my Layout to see MAP in my android APP
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<!-- MapView -->
<com.esri.android.map.MapView
android:id="#+id/map"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</RelativeLayout>
I'm testing the APP in my android phone and when i open it i get black screen
PS: when i get the Link Below in my phone browser i get the MAP , i think that my geoserver is running and the issue with my Android code so hope helping me ;)
I got the map to work fine by removing the parameters from wmsURL. Here's the modified code:
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import com.esri.android.map.MapView;
import com.esri.android.map.ogc.WMSLayer;
public class MainActivity extends Activity {
MapView mMapView;
WMSLayer wmsLayer;
String wmsURL;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// after the content of this activity is set
// the map can be accessed from the layout
mMapView = (MapView)findViewById(R.id.map);
wmsURL = "http://10.1.1.100:8090/geoserver/cyobjet/wms";
wmsLayer = new WMSLayer(wmsURL);
wmsLayer.setImageFormat("image/png");
// available layers
String[] visibleLayers = {"cyobjet:object"};
wmsLayer.setVisibleLayer(visibleLayers);
wmsLayer.setOpacity(0.5f);
mMapView.addLayer(wmsLayer);
mMapView.setEsriLogoVisible(false);
mMapView.enableWrapAround(true);
}
#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);
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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

Error and have no clue why :(

i have been trying to figure out how to make a simple search button. I found this (Implementing SearchView in action bar) which i thought would help and i guess it did, i just cant figure out why there is an error here:
SearchableActivity.java
package com.ryan.buttonsimple;
import android.app.SearchManager;
import android.content.Context;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.widget.SearchView;
import android.view.Menu;
import android.view.MenuItem;
public class SearchableActivity extends ActionBarActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Get the SearchView and set the searchable configuration
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) findItemById(R.id.search_view); //**HERE IS THE ERROR! "findItemById" and "search_view" "Cannot resolve method for both**
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false); // Do not iconify the widget; expand it by default
}
#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_searchable, menu);
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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
here is my searchable.xml file:
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/app_name"
android:hint="#string/search_hint">
</searchable>
And here is my AndroidManifest.xml File:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ryan.buttonsimple" >
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".Main"
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=".SearchableActivity"
android:label="#string/title_activity_searchable" >
<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>

What have I done wrong to make my app run at startup?

I am just trying to make my webview app run upon the bootup of an android device. I already followed every single step mentioned in this post however it does not run on device boot up. What am I missing here?
public class BootUpReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
try {
Toast.makeText(context, "Start Up", Toast.LENGTH_LONG).show();
Intent serviceIntent = new Intent(context,
MainActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(serviceIntent);
} catch (Exception e) {
Toast.makeText(context, "Start Up not possible!", Toast.LENGTH_LONG).show();
}
}
public class MainActivity extends ActionBarActivity {
private WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
// Force links and redirects to open in the WebView instead of in a browser
mWebView.setWebViewClient(new WebViewClient());
mWebView.loadUrl("http://www.google.com/");
//Note: To detect when a URL has started and finished loading, use WebViewClient.onPageStarted and WebViewClient.onPageFinished.
}
#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);
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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
and here is the manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.aa.webview1" >
<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>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission android:name="android.permission.ACCESS_ASSISTED_GPS" />
<uses-permission android:name="android.permission.ACCESS_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name=".BootUpReceiver">
<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
</manifest>
your receiver is out of your application element, it should rather be inside
<receiver android:name=".BootUpReceiver"> // i am suppose to be a child of the application
<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
</application> // clossing application element
for more info check here , and here

How to get results from search result activity?

I have an Activity (MainActivity) with a search button in the action bar. It searches by some input string and shows the results in a ListView in another Activity (SearchResultsActivity). The user might click in any result to select it.
I want to return the value selected by the user to the main activity, but it's not working. I looked in the documentation but I didn't find anything related.
I tried to use setResult(Intent) in the results activity but the onActivityResult() from the main activity never gets called.
What am I doing wrong? How can I do it?
AndroidManifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="foo.bar" >
<!-- ... -->
<uses-sdk android:minSdkVersion="11" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name">
<!-- Main Activity -->
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main">
<meta-data
android:name="android.app.default_searchable"
android:value=".SearchResultsActivity" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Search results activity -->
<activity android:name=".SearchResultsActivity"
android:parentActivityName=".MainActivity" >
<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>
Menu.xml:
<menu xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
tools:context=".MainActivity">
<!-- Search -->
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
android:showAsAction="always"
android:actionViewClass="android.widget.SearchView" />
<!-- ... -->
</menu>
MainActivity:
public class MainActivity extends FragmentActivity {
// ...
#Override
public boolean onCreateOptionsMenu(Menu menu) {
final MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.activity_main_menu, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
return true;
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// never gets called!
super.onActivityResult(requestCode, resultCode, data);
}
// ...
}
SearchResultsActivity:
public class SearchResultsActivity extends Activity {
private ListView listResults;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search_results);
// get the action bar
ActionBar actionBar = getActionBar();
// Enabling Back navigation on Action Bar icon
actionBar.setDisplayHomeAsUpEnabled(true);
listResults = (ListView) findViewById(R.id.listResults);
handleIntent(getIntent());
}
#Override
protected void onNewIntent(Intent intent) {
setIntent(intent);
handleIntent(intent);
}
/**
* Handling intent data
*/
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
final Serializable[] results = find(query);
listResults.setAdapter(new ArrayAdapter<BusLine>(this, R.layout.list_view, busLines));
listResults.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Serializable selectedResult = (Serializable) parent.getItemAtPosition(position);
setResult(RESULT_OK, new Intent().putExtra("result", selectedResult));
finish();
}
});
}
}
}
I can understand why you might think that you would use setResult and onActivityResult to handle clicks in your search results activity, but that is not how it works. Only when you launch an activity with startActivityForResult do these functions apply -- not when displaying search results.
The solution is to call startActivity on your MainActivity in the normal fashion, that is, not using activity results. Something like this:
listResults.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Serializable selectedResult = (Serializable) parent.getItemAtPosition(position);
Intent intent = new Intent();
intent.putExtra("result", selectedResult);
intent.setClass(SearchResultsActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
});
Now you will also need to capture this search result in your MainActivity, although you need to make sure you handle the normal case as well as the search results case:
#Override
protected void onCreate(Bundle savedInstanceState) {
Intent intent = getIntent();
if (intent.hasExtra("result")) {
// Launched from search results
Serializable selectedResult = (Serializable) intent.getSerializableExtra("result");
...
}
...
}

Categories

Resources