I am working on app that take the audio of the user and save them in an arrayList, then searching this array list to see if the desired word generated by the google audio API exists, so we can do actions. The action that I wanted is removed and replace it with a textview just to see if it is work properly, the audio recog is working properly, but I always got the textview text as stop. So what is the problem ?
package myfirstapp.myapps.me.voicerecognition;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.ArrayList;
public class MainActivity extends ActionBarActivity implements View.OnClickListener {
ListView lv;
Button recBtn;
static final int check = 1111;
TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.lv);
recBtn = (Button) findViewById(R.id.recBtn);
tv = (TextView) findViewById(R.id.textView);
recBtn.setOnClickListener(MainActivity.this);
}
public void onClick(View v) {
Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
i.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speak");
startActivityForResult(i, check);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == check && resultCode == RESULT_OK) {
ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
lv.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, results));
String s = "start";
for (String string : results) {
// if(string.matches("start"))
// tv.setText("Ok");
// else
// tv.setText("No start");
// }
if (string.startsWith(s))
tv.setText("Start");
else
tv.setText("Stop");
}
super.onActivityResult(requestCode, resultCode, data);
}
}
}
You don't break from the loop when you find a match, so unless the match is in the last String of the list, the text view will contain Stop at the end.
You might want to change it to :
boolean match = false;
for (String string : results) {
if (string.startsWith(s)) {
tv.setText("Start");
match = true;
break;
}
}
if (!match)
tv.setText("Stop");
Related
I am trying to make a "News App" with Android Studio. I have on my main activity a ListView and a floating button. On my second activity I got two TextViews, two EditViews and one Button. What I'd like to do is the following:
First I press on the floating Button from my main Activity. Then the second Activity should pop up and there I write on the two EditText's and then press on the button. After that I should go back to the main activity and there, a new Item should be added to the ListView. The new item should be filled with the text from the second activity's EditTexts.
This is my main activity:
package news;
import android.content.Intent;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Get subject value from addpost Activity
final String new_value = getIntent().getStringExtra("newSubject");
String [] new_subject = new String []
{new_value};
FloatingActionButton add_post_button = findViewById(R.id.post_btn);
final ListView post_view = findViewById(R.id.news_feed);
//Create Adapter to add the new subject to listview
if (new_subject != null) {
ArrayAdapter newslist_adapter = new ArrayAdapter(
MainActivity.this,
android.R.layout.simple_expandable_list_item_1, new_subject);
post_view.setAdapter(newslist_adapter);
}
add_post_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, addpost_activity.class);
MainActivity.this.startActivity(intent);
}
});
}
}
This is the second activity(addpost_activity):
package news;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.content.Intent;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;
public class addpost_activity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_addpost_activity);
Button post_button = findViewById(R.id.post_btn);
final EditText subject_text = findViewById(R.id.subject_edit);
post_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(subject_text.getText() != null) {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.putExtra("newSubject", subject_text.getText().toString());
startActivity(intent);
}
}
});
}
}
My problem is now that the app crashes every time I start it. I also get this message from my logs:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
Solved:
Thanks to Munir, the problem was solved.
Here are my activities
MainActivity:
package news;
import android.app.Activity;
import android.content.Intent;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ArrayAdapter newslist_adapter;
ArrayList<String> new_subject = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FloatingActionButton add_post_button = findViewById(R.id.post_btn);
//Create Adapter to add the new subject to listview
add_post_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, addpost_activity.class);
startActivityForResult(intent, 1);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
final ListView post_view = findViewById(R.id.news_feed);
if (requestCode == 1) {
if(resultCode == Activity.RESULT_OK){
String new_value = data.getStringExtra("newSubject");
new_subject.add(new_value);
newslist_adapter = new ArrayAdapter(
MainActivity.this,
android.R.layout.simple_expandable_list_item_1, new_subject);
post_view.setAdapter(newslist_adapter);
}
}
}
}
This is my second Activity(addpost_activity):
package news;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.content.Intent;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;
public class addpost_activity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_addpost_activity);
Button post_button = findViewById(R.id.post_btn);
final EditText subject_text = findViewById(R.id.subject_edit);
post_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(subject_text.getText() != null) {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.putExtra("newSubject", subject_text.getText().toString());
setResult(Activity.RESULT_OK, intent);
finish();
}
}
});
}
}
For that purpose I suggest using Fragments instead of two Activities. The MainActivity then holds all your data.
See Fragments guide.
From your MainActivity call the addpost_activity using startActivityForResult() method Instead of startActivity();
In your addpost_activity set the data which you want to return back to FirstActivity
post_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(subject_text.getText() != null) {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.putExtra("newSubject", subject_text.getText().toString());
setResult(Activity.RESULT_OK,intent );
finish();
}
}
});
And in the MainActivity Access the result by overriding onActivityResult()
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == Activity.RESULT_OK){
String result=data.getStringExtra("newSubject");
//Add this value to your adapter and call notifyDataSetChanged();
}
}
}
use ArrayList insted of String [] if you want to keep old entry in list change your code like below.
Main Activity
declare this variable as global
ArrayAdapter newslist_adapter;
ArrayList<String> new_subject = new ArrayList<>();
set Adapter like
newslist_adapter = new ArrayAdapter(
MainActivity.this,
android.R.layout.simple_expandable_list_item_1, new_subject);
post_view.setAdapter(newslist_adapter);
change startActivity to startActivityForResult
add_post_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, addpost_activity.class);
startActivityForResult(intent,100);
}
});
Override this method in onActivity
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 100) {
String result=data.getStringExtra("newSubject");
new_subject.add(result)
//Add this value to your adapter and call newslist_adapter.notifyDataSetChanged();
}
}
And In your addpost_activity set the data which you want to return back
post_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(subject_text.getText() != null) {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.putExtra("newSubject", subject_text.getText().toString());
setResult(100,intent );
finish();
}
}
});
In MainActivity.java, you should override a function named "onActivityRecsult" this method will be called when you come back from second activity to first activity.
I am trying to create an Android App that can listen to various commands and respond to it accordingly. for e.g, if i press mic and say "open whatsapp", it does it.
Question is How do i open Various apps with limited coding?
following code is to open whatsapp, but if i had to open other various app, Do i need to write same IF Statement for various apps?? wont it be too lenghty?
please provide a better solution.
package example.bot;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.Menu;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Locale;
public class MainActivity extends Activity {
private TextView txtSpeechInput;
private ImageButton btnSpeak;
private final int REQ_CODE_SPEECH_INPUT = 1234;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtSpeechInput = (TextView) findViewById(R.id.txtSpeechInput);
btnSpeak = (ImageButton) findViewById(R.id.btnSpeak);
btnSpeak.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
promptSpeechInput();
}
});
}
/**
* Showing google speech input dialog
* */
private void promptSpeechInput() {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
getString(R.string.speech_prompt));
try {
startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
} catch (ActivityNotFoundException a) {
Toast.makeText(getApplicationContext(),
getString(R.string.speech_not_supported),
Toast.LENGTH_SHORT).show();
}
}
/**
* Receiving speech input
* */
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQ_CODE_SPEECH_INPUT: {
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
if (result.size() == 0) {
} else {
String mostLikelyThingHeard = result.get(0);
// toUpperCase() used to make string comparison equal
if (mostLikelyThingHeard.toUpperCase().equals("OPEN WHATSAPP")) {
//informationMenu();
txtSpeechInput.setText(result.get(0));
Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.whatsapp");
//if (launchIntent != null) {
startActivity(launchIntent);//null pointer check in case package name was not found
} //else if()
}
}
}
super.onActivityResult(requestCode, resultCode, data);
}
}
//private void informationMenu() {
// Intent intent = new Intent(this, open_app.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.main, menu);
return true;
}
}
I have a problem in my code. I want to add items to a listview from another acivity onclickButton, but it only adds one item. And if I reapeat it, it only replaces the last added item. I can't figure out whats the problem some help please.
my code :
MainActivity:
package com.example.nasreddine.mtodubled; // project package
import android.app.AlertDialog;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity; //imports statements
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import android.content.DialogInterface;
public class MainActivity extends AppCompatActivity {
AlertDialog.Builder alert;
public ArrayList<City> listItems;
ArrayAdapter adapter;
ListView cityListView;
#Override
protected void onCreate(Bundle savedInstanceState) { //onCreate State
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listItems=new ArrayList<>();
adapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1,listItems);
//Displaying Data on ListView
cityListView=(ListView)findViewById(R.id.cityListView);
cityListView.setAdapter(adapter);
registerForContextMenu(cityListView);
listItems.add(new City("a","b","","","","",""));
listItems.add(new City("v","c","","","","",""));
updateListView();
cityListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
cityListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
alert.setTitle("Delete Item from list");
alert.setMessage("Are you sure you want to delete?");
alert.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
listItems.remove(position);
adapter.notifyDataSetChanged();
}
});
alert.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alert.show();
return true;
}
});
}
public void updateListView() {
Bundle bundle = getIntent().getExtras();
Intent intent=getIntent();
if (bundle != null) {
City a=new City(intent.getStringExtra("city"),intent.getStringExtra("country"),"/","/","/","/","/");
//listItems.add(a);
adapter.add(a);
adapter.notifyDataSetChanged();
}
}
public boolean onOptionsItemSelected(MenuItem item){
if (item.getItemId()==R.id.action_add){
Intent intent=new Intent(MainActivity.this,AddCity.class);
startActivity(intent);
return (true);
}
return (super.onOptionsItemSelected(item));
}
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main,menu);
return super.onCreateOptionsMenu(menu);
}
}
AddCity.java
package com.example.nasreddine.mtodubled;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class AddCity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_city);
Button addButton=(Button)findViewById(R.id.addButton);
final TextView cityAddText=(TextView)findViewById(R.id.cityAddText);
final TextView countryAddText=(TextView)findViewById(R.id.countryAddText);
addButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String city= cityAddText.getText().toString();
String country=countryAddText.getText().toString();
Intent intent =new Intent(AddCity.this,MainActivity.class);
intent.putExtra("city",city);
intent.putExtra("country",country);
startActivity(intent);
}
});
}
}
In your MainActivity.class start the AddCity.class using startActivityForResult().
public boolean onOptionsItemSelected(MenuItem item){
if (item.getItemId()==R.id.action_add){
Intent intent=new Intent(MainActivity.this,AddCity.class);
startActivityForResult(intent, requestCode); //ex: requestCode = 1
return (true);
}
return (super.onOptionsItemSelected(item));
}
After that in AddCity change add button click listener code with below:
addButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String city= cityAddText.getText().toString();
String country=countryAddText.getText().toString();
Intent intent =new Intent();
intent.putExtra("city",city);
intent.putExtra("country",country);
setResult(RESULT_OK, intent);
finish();
}
});
After that in MainActivity's onActivityResult() get data and add it to list. Also remove updatListView() method from MainActivity.
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
getActivity().invalidateOptionsMenu();
if (resultCode == Activity.RESULT_OK) {
if (data != null) {
City a=new City(data.getStringExtra("city"),data.getStringExtra("country"),"/","/","/","/","/");
listItems.add(a);
adapter.add(a);
adapter.notifyDataSetChanged();
}
}
}
}
You are restarting the MainActivity from AddCity Activity.
Rather than doing this, you need to start AddCity Activity, using method startActivityForResult().
And in AddCity Activity, rather than starting new MainActivity, you need to use setResult() method to send data to previous activity.
Also you need to override the onActivityResult method in MainActivity Class to owner the response from AddCity Activity.
Cheers!!!
Here is the code:
Class Main Activity,
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity
{
AlertDialog.Builder alert;
public List<City> listItems;
ArrayAdapter<City> adapter;
ListView cityListView;
#Override
protected void onCreate(Bundle savedInstanceState)
{ //onCreate State
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listItems = new ArrayList<>();
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, listItems);
//Displaying Data on ListView
Button addButton = (Button) findViewById(R.id.addButton);
addButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
startActivityForResult(new Intent(MainActivity.this, AddCity.class), 1);
}
});
cityListView = (ListView) findViewById(R.id.cityListView);
cityListView.setAdapter(adapter);
registerForContextMenu(cityListView);
listItems.add(new City("a", "b"));
listItems.add(new City("v", "c"));
updateListView();
}
public void updateListView()
{
Bundle bundle = getIntent().getExtras();
Intent intent = getIntent();
if(bundle != null)
{
City a = new City(intent.getStringExtra("city"), intent.getStringExtra("country"));
//listItems.add(a);
adapter.add(a);
adapter.notifyDataSetChanged();
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent)
{
super.onActivityResult(requestCode, resultCode, intent);
if(requestCode == 1 && resultCode == RESULT_OK)
{
City a = new City(intent.getStringExtra("city"), intent.getStringExtra("country"));
//listItems.add(a);
adapter.add(a);
adapter.notifyDataSetChanged();
}
}
}
Class Add City,
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class AddCity extends AppCompatActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.add_city);
Button addButton = (Button) findViewById(R.id.addButton);
final TextView cityAddText = (TextView) findViewById(R.id.cityAddText);
final TextView countryAddText = (TextView) findViewById(R.id.countryAddText);
addButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
String city = cityAddText.getText().toString();
String country = countryAddText.getText().toString();
Intent intent = new Intent();
intent.putExtra("city", city);
intent.putExtra("country", country);
setResult(RESULT_OK, intent);
}
});
}
}
Hopefully, this will help you.
Cheers!!!
i'm trying to create a simple app in which I have a button in the MainActivity and a ListView in the PairingList activity. I want the button to enable bluetooth and then list all the paired devices in the listview in the other activity. However, I get a runtime exception as soon as I've enabled BT after I clicked the button. So far this is my code:
MainActivity.java
package com.gmburg.android.bluetoothservice;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Set;
public class MainActivity extends AppCompatActivity {
Button btOn;
BluetoothAdapter btAdapter;
int REQUEST_CODE = 1;
Set<BluetoothDevice> paired_devices;
String plist[];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btOn = (Button) findViewById(R.id.btOn);
btOn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
btAdapter = BluetoothAdapter.getDefaultAdapter();
if (btAdapter == null) {
Toast.makeText(getBaseContext(), "Device has no bluetooth antenna", Toast.LENGTH_LONG).show();
} else {
if (!btAdapter.isEnabled()) {
Intent i = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(i, REQUEST_CODE);
}
}
}
});
}
public void onActivityResult(int requestcode, int resultcode, Intent data) {
if (requestcode == REQUEST_CODE) {
if (resultcode == RESULT_OK) {
Toast.makeText(getBaseContext(), "Bluetooth enabled", Toast.LENGTH_LONG).show();
paired_devices = btAdapter.getBondedDevices();
int count = paired_devices.size();
plist = new String[count];
int j = 0;
for(BluetoothDevice device : paired_devices){
plist[j] = device.getName();
j++;
}
}
Bundle btOn = new Bundle();
btOn.putStringArray("paires", plist);
Intent in = new Intent("pair_filter");
in.putExtras(btOn);
startActivity(in);
}
}
}
PairingList.java
package com.gmburg.android.bluetoothservice;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class PairingList extends Activity {
ListView lview;
String[] pairs;
ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pairinglist_layout);
lview = (ListView) findViewById(R.id.listviewid);
Bundle btOn = getIntent().getExtras();
pairs = btOn.getStringArray("pairs");
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, pairs);
lview.setAdapter(adapter);
}
}
The logger gives me this:
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {com.gmburg.android.bluetoothservice/com.gmburg.android.bluetoothservice.MainActivity}: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=pair_filter (has extras) }
All the help is appreciated!
You are getting below error
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=pair_filter (has extras)
this is because the intent you are trying to start activity for is not handled by any available activity/app in the phone. try resolveActivity method to find if any activity will be able to handle this, then only call startActivity/ForResult from your application.
resolveActivity Method's docs
So using suggestions from the last question I asked, I figured out how to call BarCodeScanner, and return the value to a field. so now, I have a toast that says "successful scan" and then I want to pass the result to a new activity. when I comment out my intent, everything works (minus the passing of data/switching of screen, obviously) but when I run my project as is, it FC's... no errors reported by eclipse in code or XML. Any insights?
package com.mhe.test.scan;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class main extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button myScanButton = (Button) findViewById(R.id.myScanButton);
totalbox = (EditText) findViewById(R.id.tBox);
myScanButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "PRODUCT_MODE");
startActivityForResult(intent, 0);
}
});
}
private EditText totalbox;
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
final String contents = intent.getStringExtra("SCAN_RESULT");
if ( totalbox != null )
totalbox.setText(contents);
Context context = getApplicationContext();
CharSequence text = "Successful Scan";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
Intent i = new Intent(main.this, Result.class);
i.putExtra("SNARP", "SCAN_RESULT");
startActivityForResult(i, 0);
} else if (resultCode == RESULT_CANCELED) {
if ( totalbox != null )
totalbox.setText("bummer");
}
}
}
}
And then to handle the data being passed, in the new activity:
package com.mhe.test.scan;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Result extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
Intent i = getIntent();
Bundle b = i.getExtras();
String foosh = b.getString("SNARP");
EditText box1 = (EditText) findViewById(R.id.tBox1);
box1.setText(foosh);
Try sending a Bundle object when calling the new intent.
Intent i = new Intent(main.this, Result.class);
Bundle b = new Bundle();
b.putString("SNARP", "SCAN_RESULT")
i.putExtras(b);
Try getting string in the child Activity this way.
public class Result extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
Intent i = getIntent();
String foosh = i.getStringExtra("SNARP");