I am following along a YouTube Tutorial on how to get a query from a searchview and I am getting this error cannot be referenced from a static context.
MainActivity.java:
package com.zoggfrombetelgeuse.clef;
import android.app.SearchManager;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar my_toolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(my_toolbar);
getSupportActionBar().setTitle(R.string.my_toolbar_tile);
Intent searchIntent = getIntent();
if(Intent.ACTION_SEARCH.equals(searchIntent.getAction())){
String query = searchIntent.getStringExtra(SearchManager.QUERY);
Toast.makeText(MainActivity.this, query, Toast.LENGTH_SHORT).show();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu,menu);
SearchView searchview = (SearchView) menu.findItem(R.id.action_search);
SearchManager searchmanager = (SearchManager) getSystemService(SEARCH_SERVICE);
SearchView.setSearchableInfo(searchmanager.getSearchableInfo(getComponentName()) );
return super.onCreateOptionsMenu(menu);
}
}
MainActicity is my only Activity.
Manifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.zoggfrombetelgeuse.clef">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<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.default_searchable"
android:value=".MainActivity"/>
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable"
/>
</activity>
</application>
Change this line
SearchView.setSearchableInfo(searchmanager.getSearchableInfo(getComponentName()) );
to
searchview.setSearchableInfo(searchmanager.getSearchableInfo(getComponentName()) );
Related
I am building 2 NFC enabled apps, one is supposed to send a message and the other to receive it.
Sending app Android Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.joyride.nfc_merchant">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.NFC_Merchant">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Sending app Main Activity
package com.joyride.nfc_merchant;
import androidx.appcompat.app.AppCompatActivity;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
String message;
NdefRecord ndefRecord;
NdefMessage ndefMessage;
NfcAdapter nfcAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nfcAdapter = NfcAdapter.getDefaultAdapter(this);
}
public void sendMessage(View view) {
message = ((EditText) findViewById(R.id.text_message)).getText().toString();
ndefRecord = NdefRecord.createTextRecord(null, message);
ndefMessage = new NdefMessage(ndefRecord);
nfcAdapter.setNdefPushMessage(ndefMessage, this);
}
}
Receiving app android Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.joyride.nfc_customer">
<uses-permission android:name="android.permission.NFC" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.NFC_Customer">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Receiving app Main Activity
package com.joyride.nfc_customer;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.nfc.NdefMessage;
import android.nfc.NfcAdapter;
import android.os.Bundle;
import android.os.Parcelable;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
TextView messageView = (TextView)findViewById(R.id.messageView);
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
Parcelable[] rawMessages =
intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
if (rawMessages != null) {
NdefMessage[] messages = new NdefMessage[rawMessages.length];
for (int i = 0; i < rawMessages.length; i++) {
messageView.setText(((NdefMessage) rawMessages[i]).toString());
}
}
}
}
}
I've installed each app on another phone. Off course the receiving app is not registering the message. Am I missing something here? How do I go about debugging this?
This question already has answers here:
How to start new activity on button click
(28 answers)
Closed 4 years ago.
I used startActivity() method but it is not working.I think this method is not working because my MainActivity extends app compat actvity.
So please help me to start activity in an app compat activity.
I try various times but when I start my apk and click on button then app crashes.
Here my code
main.xml
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#E96050"/>
<TableRow><Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/mainButton"
android:text="Open Second Activity"
android:onClick="yas"/></TableRow> </TableLayout>
MainActivity.java
package com.mycompany.myapp;
import android.app.Activity;
import android.os.*;
import android.support.v7.app.*;
import
android.support.v7.widget.Toolbar;
import android.view.*;
import android.widget.Toast;
import android.content.Intent;
import android.support.v7.app.*;
public class MainActivity extends
AppCompatActivity {
#Override
protected void onCreate(Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
// TODO: Implement this method
super.onCreateOptionsMenu(menu);
CreateMenu(menu);
return true;
}
private void CreateMenu(Menu menu) {
MenuItem mnu1 = menu.add(0,0,0,"item1");
{
mnu1.setIcon(R.drawable.ic_launcher);
mnu1.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
}
}
public void yas(View vv) {
Intent iiii = new
Intent(MainActivity.this,SecondAct.class);
startActivity(iiii);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<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>
<activity
android:name=".SecondActivity"
android:label="secondActivity"
android:parentActivityName=".MainActivity">
<intent-filter>
<action android:name="SecondActivity" />
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
</application>
use this
Intent intent=new Intent(this,Target_Activity.class);
startActivity(intent);
And open your manifest file add this line
<activity android:name=".Target_Activity"
android:parentActivityName=".MainActivity">
</activity>
I hope you try this code..
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
startActivity(intent);
Please add second activity into android manifest file.
<activity android:name=".SecondActivity"/> // define second activity name.
add this code in android manifest file..
<activity
android:name=".SecondActivity"
android:label="secondActivity" >
</activity>
I am having trouble making my SearchView in the ActionBar trigger an intent that is received by my SearchActivity. I think the issue is in the Manifest/XML somewhere, because the logcat isn't even showing the log in the onCreate method of the SearchActivity. I've used these resources, and others (including SO responses), to try and implement it, but haven't had any luck.
manifest:
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".activitiesFragments.MainActivity"
android:configChanges="orientation"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="android.app.default_searchable"
android:value=".activtiesFragments.SearchActivity"/>
</activity>
<activity
android:name=".activitiesFragments.LoginActivity"
android:label="#string/login_button_text"
android:screenOrientation="portrait" />
<activity
android:name=".activitiesFragments.SignUpActivity"
android:label="#string/create_account_button"
android:screenOrientation="portrait">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".activitiesFragments.LoginActivity" />
</activity>
<activity android:name=".activitiesFragments.SearchActivity">
<intent-filter>
<action android:name="android.intent.action.SEARCH"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable"/>
</activity>
</application>
MainActivity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_messages, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (android.support.v7.widget.SearchView) menu.findItem(R.id.friends_menu_item).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
return true;
}
SearchActivity
package dev.workman.shoutout.activitiesFragments;
import android.app.ListActivity;
import android.app.SearchManager;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import dev.workman.shoutout.R;
public class SearchActivity extends AppCompatActivity {
ArrayList<String> query_results = new ArrayList<>();
ArrayAdapter<String> adapter;
ListView results_view;
String TAG = "SearchActivity";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
results_view = (ListView) findViewById(R.id.results_list);
adapter = new ArrayAdapter<>(this, android.R.layout.simple_expandable_list_item_1, query_results);
results_view.setAdapter(adapter);
Log.d(TAG, "onCreate: created searchview activity");
handle_intent(getIntent());
}
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
query_results.clear();
handle_intent(getIntent());
}
public void handle_intent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
Log.d(TAG, "handle_intent: query " + query);
// process the query, add values to the query_results adpater
// then call adapter.notifyDataSetChanged()
}
}
}
I figured out my problem. Rather than an XML/manifest issue, I the problem lied with the getComponentName() call.
I changed it to
new ComponentName(MainActivity.this, SearchActivity.class)
and it works now. I hope this helps others with the same issue
Is there any way to use ADT's manifest merging feature (manifestmerger.enabled=true in project.properties) in Android Studio?
1. create sample project
2. add new module
3. Module Setting
4. Remove files in App Module
Move app/~~~/values/style.xml to common/~~~/values/style.xml
edit 1.
<!-- app/~~~/AndroidManifest.xml -->
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="kr.susemi99.manifestmergerforandroidstudio" >
<application >
<activity
android:name="kr.susemi99.common.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
edit 2.
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme"> <!-- add this line -->
<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>
edit 3.
package kr.susemi99.common;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
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)
{
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
int id = item.getItemId();
if (id == R.id.action_settings)
{
Toast.makeText(getApplicationContext(), "test toast", Toast.LENGTH_LONG).show();
}
return super.onOptionsItemSelected(item);
}
}
See all http://susemi99.kr/2368
I am trying to implement search functionality in my android app using SearchView within the layout of my app. I purposefully chose to have it in the layout rather than in the Action Bar.
The problem is that once I type the search text and hit the enter/search key on the keyboard nothing happens. The searchable intent is never called. I feel like I must be missing something that is causing the search not to trigger
I apologize if I haven't included the code in the correct format below. Please let me know if there is any additional information that would help. Thanks in advance!
Manifest
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.lotr.arda.MainActivity"
android:label="#string/app_name" >
</activity>
<activity
android:name=".WebActivity"
android:theme="#android:style/Theme.NoTitleBar" >
</activity>
<activity
android:name=".menuActivity"
android:theme="#android:style/Theme.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.google.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize">
</activity>
<activity android:name=".searchActivity" >
<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>
Res/XML/search
<?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/searchhint" >
</searchable>
SearchActivity.java
package com.lotr.arda;
import android.app.ListActivity;
import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
public class searchActivity extends ListActivity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search_page);
// 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);
System.out.println(query);
//doMySearch(query);
}
}
}
menuActivity.java (layout where searchview is located)
package com.lotr.arda;
import com.google.ads.AdRequest;
import com.google.ads.AdView;
import android.app.Activity;
import android.app.SearchManager;
import android.app.SearchableInfo;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.SearchView;
public class menuActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu_page);
try{
AdView myad = (AdView) findViewById(R.id.ad);
AdRequest adRequest = new AdRequest();
myad.loadAd(adRequest);
SearchManager searchManager = (SearchManager)getSystemService(SEARCH_SERVICE);
SearchableInfo searchableInfo = searchManager.getSearchableInfo(getComponentName());
SearchView sv = (SearchView)findViewById(R.id.searchView1);
sv.setSearchableInfo(searchableInfo);
Button alpha = (Button)this.findViewById(R.id.button1);
alpha.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(menuActivity.this, MainActivity.class);
startActivity(intent);
}});
}catch(Exception e) {System.out.println(e.toString());}
}
}
You are missing a SearchView#setOnQueryTextListener. You'll need to override the OnQueryTextSubmit() method to actually perform and handle your search.
EDIT: I am assuming you're querying a database and using a Cursor for the results.
mSearchView.setOnQueryTextListener(new OnQueryTextListener()
{
#Override
public void onQueryTextSubmit(String query) {
Cursor c = getContentResolver.query(URI, null, "col_1 = ? OR col_2 = ?", new String[] { query, query }, null);
//Do whatever you want with your Cursor here
}
}
);
For a quick look it seems you missed default_searchable setting in your AndroidManifest.
Under activity setting for MainActivity, you should add this
<meta-data
android:name="android.app.default_searchable"
android:value=".WebActivity" />
Make sure your searchables.xml file is in the correct location (i.e, in your res/xml/ folder) and that the same file does not contain any errors - otherwise you will find that (SearchManager)getSystemService(Context.SEARCH_SERVICE).getSearchableInfo(componentName) will return null, breaking your search functionality.
(Also, ensure you set componentName in the appropriate manner, because the example shown in the Android guide is for only when you are making searches and displaying searches in the same Activity.)