Tts.java
public void onInit(int arg0) {
Bundle dataBundle = Tts.this.getIntent().getExtras();
speech = dataBundle.getString("IMSENS");
tts.setLanguage(Locale.ENGLISH);
tts.speak(speech, TextToSpeech.QUEUE_FLUSH, null);
}
Im_SensShow.java
public class Im_SensShow extends ListActivity {
public void buttonGetClicked(View v){
Cursor cc = cursor;
Intent showSound = new Intent(Im_SensShow.this,Tts.class);
Bundle dataBundle = new Bundle();
dataBundle.putString("IMSENS",cc.getString(cc.getColumnIndexOrThrow(DBAdapter.KEY_IM_SENS)));
showSound.putExtras(dataBundle);
startActivityForResult(showSound,1);
}
I set tts to read a sentece from Im_SensShow.java and when I press the button in Im_SensShow.java page this progarm change to tts page and make sound
but actually I want it to send only sentence and doesn't change page (stay in Im_SensShow.java page)
I try to not declare .Tts in Manufest but it force close
What should I do?
Put the tts.speak() call in a [onItemClick()][1] listener.
You need to make sure that the TTS subsystem is already initialized (e.g. within onCreate() of your Im_SendShow activity).
Related
I'm trying to get the user information typed in the edit text. I want it saved into the Intent result variable. Trying to sending it back to the main activity afterwards. Keep getting the cannot resolve method. I'm thinking it must be that I'm missing a parameter in the putExtra() method
public class EnterDataActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_enterdata);
Button doneButton = (Button) findViewById(R.id.button_done);
final EditText getData = (EditText) findViewById(R.id.enter_data_here);
doneButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent result = new Intent();
result.putExtra(getData);
setResult(RESULT_OK, result);
finish(); // Ends sub-activity
}//ends onClick
});
}//ends onCreate void button
}
Well if you are trying to send the EditText that's not possible.
If you are trying to send the text that are on that EditText that's possible.
How to do it?
Declare a String to save the data (You can avoid this step, but that's more clear)
String mText = getData.getText().toString();
Then you'll use the getExtra() method to send the String to the new Activity
Intent i = new Intent(this, MyNewActivity.class);
i.putExtra("text_from_editText", mText);
startActivity(i);
Then the last step (You don't ask for it, but you'll need it), get the text.
//onCreate() of the second Activity
Intent i = getIntent();
String mText = i.getStringExtra("text_from_editText");
Replace result.putExtra(getData); with result.putExtra(getData.getText().toString()); to get the text from the EditText. Right now you're trying to put the entire EditText object into the intent as an extra instead of just the text from the EditText.
This is my first question here in SO. So, here's my problem: I'm developing an app for android. This is how my app flows:
I call a MainActivity;
The user press a button that calls a LoginActivity;
The user fill in the username and password;
LoginActivity calls a AsynchronousTask that send the data for a JAX-WS (I use ksoap2 library to call my SOAP web service);
If the information are right, a MenuActivity is called;
There is a button for "License Plate Search". When it is pressed, I call a Activity for input the license plate and an "OK" button for search;
When the "OK" button is pressed, I send the data for a new AsynchronousTask that search for the plate in my web service;
If the license plate is a valid one (activity result = OK) I populate my database with: License Plate Number, Year, Model and Developer;
After this I call a new Activity called ResultActivity where a show the vehicle information in a new Dialog (HERE IS THE PROBLEM);
But, if the result is CANCELED (when my web service doesn't found the license plate) I just show a Alert for the user.
The problem is happening when I try to show the information for the user. Here is my ResultActivity:
public class ResultadoBuscaPlacaActivity extends Activity {
private VeiculoDAO vDao;
TextView campoPlaca;
TextView campoModelo;
TextView campoMarca;
TextView campoAno;
TextView campoRenavam;
TextView campoProprietario;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_resultado_busca_placa);
campoPlaca = (TextView) findViewById(R.id.valor_placa);
campoModelo = (TextView) findViewById(R.id.valor_modelo);
campoMarca = (TextView) findViewById(R.id.valor_marca);
campoAno = (TextView) findViewById(R.id.valor_ano);
campoRenavam = (TextView) findViewById(R.id.valor_renavam);
campoProprietario = (TextView) findViewById(R.id.valor_proprietario);
vDao = new VeiculoDAO(this);
vDao.open();
}
public void confirm(View v){
finish();
}
#Override
protected void onStart() {
super.onStart();
Veiculo v = new Veiculo();
v = vDao.getFirstElement();
if (v.getPlaca()!=null){
campoPlaca.setText(v.getPlaca());
campoModelo.setText(v.getModelo());
campoMarca.setText(v.getMarca());
campoAno.setText(v.getAno());
campoRenavam.setText(v.getRenavan().toString());
campoProprietario.setText(v.getNomeProprietario());
}
}
#Override
protected void onDestroy() {
vDao.close();
super.onDestroy();
}
#Override
protected void onPause() {
vDao.close();
super.onPause();
}
}
The VeiculoDAO is a simple DAO that calls a Custom SQLiteOpenHelper class. The method getFirstElement() is here:
public Veiculo getFirstElement(){
Cursor cursor = database.query(CustomSQLiteOpenHelper.TABLE_VEICULO,
columns, null, null, null, null, null);
cursor.moveToFirst();
Veiculo v = new Veiculo();
while (!cursor.isAfterLast()) {
v.setAno(cursor.getInt(cursor.getColumnIndex(CustomSQLiteOpenHelper.COLUMN_ANO)));
v.setMarca(cursor.getString(cursor.getColumnIndex(CustomSQLiteOpenHelper.COLUMN_MARCA)));
v.setModelo(cursor.getString(cursor.getColumnIndex(CustomSQLiteOpenHelper.COLUMN_MOD)));
v.setNomeProprietario(cursor.getString(cursor.getColumnIndex(CustomSQLiteOpenHelper.COLUMN_NMPROP)));
v.setPlaca(cursor.getString(cursor.getColumnIndex(CustomSQLiteOpenHelper.COLUMN_PLACA)));
v.setRenavan(cursor.getLong(cursor.getColumnIndex(CustomSQLiteOpenHelper.COLUMN_MARCA)));
}
cursor.close();
return v;
}
What is actually happening is: when the result activity calls the method onStart() and goes to the line:
v = vDao.getFirstElement();
The UI goes back to MenuActivity (because I called the SearchActivity by startActivityForResult() ) but it stops there, when it supposed to call ResultActivity.
In logcat, this message runs in loop:
D/dalvikvm(28637): GC_CONCURRENT freed 631K, 15% free 18650K/21824K, paused 1ms+4ms, total 25ms<br>
If I comment the specific line above, the app runs "normally" and the result Activity is called, but the information are empty (of course).
Does anyone know what should I do?
This loop never terminates since the cursor is never advanced inside the loop and the return value of isAfterLast() does not change.
while (!cursor.isAfterLast()) {
}
Since you are only interested in the first result, change it to
if (cursor.moveToFirst()) {
}
You can remove the earlier moveToFirst() call as redundant. It's still important to check the return value as there may be no result rows and then moveToFirst() returns false.
Hi everyone.
I'm new in android and I'm working on an app in which I need to recall the same activity to enter the information of a variable amount of entities (passengers).
I have the following:
btnContinue3.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
for (int i=0; i<Pssngr; i++){
passenger[i] = new
Intent(getApplicationContext(), Pasajeros.class);
startActivity(passenger[i]);
}
}
});
Pssngr is the amount of passsengers or entites that need a unique activity to get their information entered.
The trigger is the button then the activities will be called one by one following an array
I try this code but after clicking on the button the app crashed.
Please someone help me find a way to make this work.
thanks
It crashes because You are trying to start x number of Activities at once.
If You have to run new Activity for each of Passengers best in this scenario will be startActivityForResult
I beliver effect You trying to get is that user clicks on button just once and activities for each passenger will open one after another.
To do it in method onClick You will start only first activity, don't use loop.
You start consequently next activities in onActivitiyResult
In addition to what Gustek mentions above, a better way to approach this would be to have one activity and just pass the different values from the parent (PassengersAvitivty) activity through the intent as below:
final Intent intent = new Intent(PassengersAcitivty.this, PassengersEntityActivity.class);
intent.putExtra("PASSENGER_FIRSTNAME", passenger[i].getName());
intent.putExtra("PASSENGER_LASTNAME", passenger[i].getLastName());
intent.putExtra("PASSENGER_EMAIL", passenger[i].getEmail());
startActivity(intent);
and here is how you can retrieve them on your activity (PassengerEntityActivity)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extras = getIntent().getExtras();
if (extras != null)
{
firstname = extras.getString("PASSENGER_FIRSTNAME");
lastname = extras.getString("PASSENGER_LASTNAME");
email = extras.getString("PASSENGER_EMAIL");
}
else
{
//Log.v("NO VALUE", "OOPS");
}
I can clarify further if needed.
My app consists of a number of activities, upto now I have had no problems with accessing the database when moving between the activities. However on the last listActivity (LocationActivity), I have an embedded button on each listView item.
When one of these buttons are clicked, it sends you to SpecificationEdit.java where the user inputs the specfication into some EditText fields for that listView item (a damaged component), but when you click Save it crashes with the following error message (note the data is saved to database ok):
java.lang.RuntimeException: Unable to resume activity blah blah
Exception: trying to requery an already closed cursor blah blah
Here is the listActivity class:
public class LocationActivity extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location);
setLongClick();
rmDbHelper = new RMDbAdapter(this);
rmDbHelper.open();
getIntents();
setUpViews();
setAdapter();
setTextChangedListeners();
}
protected void onResume(){
super.onResume();
final Cursor locationCursor = (Cursor) rmDbHelper.fetchLocationsForRun(runId);
startManagingCursor(locationCursor);
locationCursorSize = locationCursor.getCount();
setAdapter();
setTextChangedListeners();
}
And here is the bit in this activity where is sends you to SpecificationEdit.java
private void startComponentEdit() {
Intent i = new Intent(LocationActivity.this, SpecificationEdit.class);
i.putExtra("Intent_InspectionID", inspectionId);
i.putExtra("Intent_AreaID", areaId);
i.putExtra("Intent_RunID", runId);
i.putExtra("Intent_LocationID", locationId);
i.putExtra("Intent_Ref", locationRef);
i.putExtra("Intent_DamagedComponentID", damagedComponentId);
startActivityForResult(i, ACTIVITY_CREATE);
}
And here is the OnCreate in SpecificationEdit.java:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
rmDbHelper = new RMDbAdapter(this);
rmDbHelper.open();
Intent i = getIntent();
inspectionId = i.getLongExtra("Intent_InspectionID", -1);
areaId = i.getLongExtra("Intent_AreaID", -1);
runId = i.getLongExtra("Intent_RunID", -1);
locationId = i.getLongExtra("Intent_LocationID", -1);
damagedComponentId = i.getLongExtra("Intent_DamagedComponentID", -1);
setContentView(R.layout.edit_specification);
setUpViews();
populateFields();
fillSpinner();
setListeners();
}
With the bit of code which fires when you click the save button:
protected void saveDamagedComponentSpec() {
String manufacturer = ((Cursor)manufacturerSpinner.getSelectedItem()).getString(1).toString();
String text1 = specEditText1.getText().toString();
String text2 = specEditText2.getText().toString();
String text3 = specEditText3.getText().toString();
String text4 = specEditText4.getText().toString();
String notes_spec = specEditTextNotes.getText().toString();
rmDbHelper.saveDamagedComponentSpec(damagedComponentId, manufacturer, text1, text2, text3, text4, notes_spec);
if ("Yes".equals(specSaved)){
Toast.makeText(getApplicationContext(), "Component specification updated",
Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(getApplicationContext(), "Component specification added",
Toast.LENGTH_SHORT).show();
}
finish();
}
Finally, here is the code in my database helper class:
//Constructor - takes the context to allow the database to be opened/created
public RMDbAdapter(Context ctx) {
this.mCtx = ctx;
}
/**
* Open the rm database. If it cannot be opened, try to create a new
* instance of the database. If it cannot be created, throw an exception to
* signal the failure
*
* #return this (self reference, allowing this to be chained in an
* Initialisation call)
* #throws SQLException if the database could be neither opened or created
*/
public RMDbAdapter open() throws SQLException {
rmDbHelper = new DatabaseHelper(mCtx);
rmDb = rmDbHelper.getWritableDatabase();
return this;
}
public void close() {
rmDbHelper.close();
}
The weird thing is, you can click on one of the listView item (the actual item not the embedded item) or the button 'add new component' and this will send you to another activity ComponentEdit.java with very similar interface (to add a component to the list) as SpecificationEdit but which when it finishes doesn't crash the app.
I can't see any major difference between the two activities, yet one is crashing with this error when you return to LocationActivity and one is not.
I have just tried removing onResume and this made no difference.. Hit a brick wall with this and it's driving me nuts.
I should add that it is working ok on my emulator, but crashes when I test it on my phone (HTC One S). Very strange..
Don't forget to call rmDbHelper.close(); before start another activity
Right, found the issue (spot the obvious mistake):
Cursor componentsCursor = (Cursor) rmDbHelper.fetchDamagedComponentSpecForInspection(inspectionId, componentType);
startManagingCursor(componentsCursor);
Intent i = new Intent(this, SpecificationEdit.class);
i.putExtra("Intent_InspectionID", inspectionId);
i.putExtra("Intent_AreaID", areaId);
i.putExtra("Intent_RunID", runId);
i.putExtra("Intent_LocationID", locationId);
i.putExtra("Intent_Ref", locationRef);
i.putExtra("Intent_DamagedComponentID", damagedComponentId);
startActivityForResult(i, ACTIVITY_CREATE);
componentsCursor.close();
So it wasn't this obvious (I had some blocked out code left in before the componentsCursor.close()), but when I finished SpecifcationEdit.class, I guess it returns to this activity and tries to close the componentsCursor but obviously fails.
Stupid thing is, I hadn't actually got this cursor doing anything yet! Doh!
Just for some additional advice/whittering; my app is fundimentally different to the Google notePad example, as I don't actually use the startActivityForResult as they do (in fact now I understand it better I will replace these all with just startActivity) as I input the data into the database while still on the edit activities, there and then (rather than passing this information through an intent then addinig when you return to the previous activity.
I find this more logical in the realms of my code, but any feedback on this approach?
I'm getting problems using the Intent for navigate through the screens. I want to send a variable and use it in the other class.
I'm using a method, the method takes the variable but i don't know how to send it with the intent to the new screen, which will use it to do some things.
Main class calls the metod:
private void pantallaDetalles(final int identificador)
{
startActivityForResult(new Intent(this,MostrarDetalles.class),REQST_CODE);
}
MostrarDetalles.class is the *.java which will take the variable. I'm begining it like this:
public class MostrarDetalles extends Activity {
SQLiteDatabase db;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.detalles);
//more code...
Cursor c = db.rawQuery("SELECT * FROM table WHERE _id="+ identificador, null);
}
Did you see? I'm talking about this. I don't know how to send the "identificador" variable from the main class to the second class through the Intent.
Can you help me with this? Thank you very much in advice.
JMasia.
Use the extras bundle in the intent.
Intent i = new Intent(...);
i.putExtra("name_of_extra", myObject);
Then on onCreate:
getIntent.getIntExtra("name_of_extra", -1);
Screen 1:
Intent i=new Intent("com.suatatan.app.Result");
i.putExtra("VAR_RESULT", "Added !");
startActivity(i);
Screen 2: (Receiver):
TextView tv_sonuc = (TextView) findViewById(R.id.tv_sonuc);
Bundle bundle = getIntent().getExtras();
String var_from_prev_intent = bundle.getString("VAR_RESULT");
tv_sonuc.setText(var_from_prev_intent);
You can use Intent.putExtra() to bundle the data you want to send with the intent.