MY program is note working on the emulator. There are no errors in the code from eclipse. It says that "the process has stopped unexpectedly". I have tried to run the xml code by it self and it comes up without a problem. I also have a db file of the same activity however I am more suspicious of this file being the cause of the problem. I cannot find any thing to help find the answer. I do not know much about programming but I have been researching this application. The solution to this would help many people who might have this problem or are interested in this activity.
I am interested in inserting a fixed text from a button into a database.
import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class Watchnotes extends Activity implements OnClickListener,
android.view.View.OnClickListener {
Button button1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_);
button1.setOnClickListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity, menu);
return true;
}
// public interface
public void onClick(View arg0) {
boolean didItWork = true;
try{
switch (arg0.getId()) {
case R.id.button1:
String note = "XXXX";
NAME entry = new NAMEdb(NAME.this);
entry.open();
entry.createEntry(note);
entry.close();}
}catch (Exception e){
didItWork = false;
}finally{
if (didItWork){
Dialog d = new Dialog(this);
d.setTitle("XXXX");
d.show();}
}
}
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
switch (((View) arg0).getId()){
case R.id.button5:
Intent i = new Intent("com.intent.LIST");
startActivity(i);}}}
we have the same problem. Just initialize
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main3);
addListenerOnButton1();
addListenerOnButton2();
}
public void addListenerOnButton1() {
final Context context = this;
button = (Button) findViewById(R.id.btn1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, App2Activity.class);
startActivity(intent);
}
});
Related
I want to alert dialog to display if the password and login id doesn't match. I tried the following code, but when i run if the text are same then it executes, but in the case if the password and Login Id doesn't match an alert is supposed to be display but instead the process exits saying unfortunately your project is ended.
I have attached my code below
package com.example.explicitintent;
import java.security.PublicKey;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
Button b1, b2,b3;
EditText e1, e2;
String username="saras", password="greek";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
e1 = (EditText) findViewById(R.id.editText0001);
e2 = (EditText) findViewById(R.id.editText0002);
b1 = (Button) findViewById(R.id.button0002);
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0)
{
// TODO Auto-generated method stub
if (username.equals(e1.getText().toString()) && (password.equals(e2.getText().toString())))
{
Intent b = new Intent(MainActivity.this,Contacts.class);
String s = e1.getText().toString();
String s1 = e2.getText().toString();
b.putExtra("d1", s);
b.putExtra("d2", s1);
startActivity(b);
}
else
{
AlertDialog.Builder alt = new AlertDialog.Builder(getApplicationContext());
alt.setIcon(R.drawable.ic_launcher);
alt.setTitle("WARNING");
alt.setMessage("Do u want to re-enter password");
alt.setPositiveButton("YES", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface arg0, int arg1)
{
Toast.makeText(getApplicationContext(), "OK", Toast.LENGTH_SHORT).show();
}
});
alt.setNegativeButton("NO",new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface arg0, int arg1)
{
Toast.makeText(getApplicationContext(),"OK", Toast.LENGTH_SHORT).show();
}
});
alt.show();
}
}
});
b2 = (Button) findViewById(R.id.button0003);
b2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent c = new Intent(MainActivity.this, Reset.class);
startActivity(c);
}
});
b3 = (Button) findViewById(R.id.button1);
b3.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View arg0)
{
Toast.makeText(getApplicationContext(),"Password Saved", Toast.LENGTH_LONG).show();
}
});
}
#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;
}
}
Change this line
AlertDialog.Builder alt = new AlertDialog.Builder(getApplicationContext());
to
AlertDialog.Builder alt = new AlertDialog.Builder(arg0.getContext());
and you should change arg0 to something meaningful like view or v. If that doesn't work then please post the logcat so we can see what error you are getting. You need to use the appropriate Context for the situation and here you want the AlertDialog to use the Activity Context which is the same Context that your Button (or arg0) uses.
Note MainActivity.this will do the same thing here as argo.getContext() but I was recently informed that it is bad practice for reasons such as if you want to re-use this code then you have to change the activity name part of the code. Its a less dynamic way of accessing the Context.
Here is a good SO answer that addresses the issue of Context. It can be a tricky concept to grasp at first so you may want to read over it a few times and keep it close by.
I have a program and when I try to start it, it crashes. I really don't understand why, even Eclipse show's that there are no errors.
I can show you the code of a page on which I think is the problem.
package ctect.android.maxipro;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class BasicScreenActivity extends Activity {
private Button butonul1;
private Button butonul2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_basic_screen);
butonul1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View currentView) {
// TODO Auto-generated method stub
butonul2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View currentView) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(currentView.getContext(), NeedForSpeedActivity.class);
startActivityForResult(myIntent, 0);
Intent myIntent2 = new Intent(currentView.getContext(), Fifa2012Activity.class);
startActivityForResult(myIntent2, 0);
}
});
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.basic_screen, menu);
return true;
}
}
if somebody understands, please help me.
You forgot to assign an object to butonul1 before using it. You need to add this line before butonul1.setOnClickListener:
butonul1= (Button) findViewById(R.id.butonul1);
This is assuming that you gave it the id butonul1 in your layout file.
I am trying to a make an app that has just one button . When you press it plays a sound and when you LONG press it , It gives you the option to share . The app is working without any errors .. but the share option just wont open . And the button does nothing when you long press it .
package com.example.buttonclicksound;
import com.example.buttonclicksound.MainActivity;
import android.graphics.drawable.AnimationDrawable;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
protected static final String TAG = "MainActivity";
Button button1;
MediaPlayer mPlayer;
AnimationDrawable lightsAnimation;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//No title bar is set for the activity
requestWindowFeature(Window.FEATURE_NO_TITLE);
//Full screen is set for the Window
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
ImageView lights = (ImageView) findViewById(R.id.imageView1);
lightsAnimation = (AnimationDrawable) lights.getDrawable();
button1 = (Button) findViewById(R.id.button1);
mPlayer = MediaPlayer.create(MainActivity.this, R.raw.splash);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
mPlayer.start();
mPlayer.setLooping(false);
} catch (Exception e) {
Log.e("ButtonListenerActivity", "error: " + e.getMessage(),
e);
}
}
});
button1.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View v) {
shareIt();
return true;
}
private void shareIt() {
//sharing implementation here
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("audio/mPlayer");
}
});
}
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
lightsAnimation.start();
}
protected void onDestroy() {
super.onDestroy();
// TODO Auto-generated method stub
if (mPlayer != null) {
mPlayer.release();
mPlayer = null;
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Its extremly frustrating .. been working so hard at this .. and the Daam thing just wont work .
after some further work .. I am now stuck at this .. the send via box opens up .. i select email .. but all i get is a blank email
private void shareIt(MediaPlayer mPlayer) {
//sharing implementation here
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("audio/mp3");
sharingIntent.putExtra(Intent.EXTRA_SUBJECT,"Ringtone File:"+getResources().getResourceEntryName(mPlayer)+".mp3");
sharingIntent.putExtra(Intent.EXTRA_TEXT,"Ringtone File : "+getResources().getResourceEntryName(mPlayer)+".mp3");
sharingIntent.putExtra(Intent.EXTRA_STREAM,Uri.parse("android.resource://com.my.android.soundfiles/"+mPlayer));
sharingIntent.putExtra("sms_body","Ringtone File : "+getResources().getResourceEntryName(mPlayer)+".mp3");
startActivity(Intent.createChooser(sharingIntent, "Share Sound File"));
}
});
You're not doing anything with your sharingIntent, just creating it.
Can any let me know what I am doing wrong. Trying to get Text To Speech to work onClick for a Text View.
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.content.Intent;
import android.speech.tts.TextToSpeech;
public class MainActivity extends Activity implements TextToSpeech.OnInitListener
{
private TextToSpeech tts;
private static final int MY_DATA_CHECK_CODE = 1234;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_24);
Intent checkIntent = new Intent();
checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);
((Button)findViewById(R.id.btnClear)) .setOnClickListener(clearbutton);
}
public void onClick(View v)
{
TextView textSpeak = (TextView) findViewById(R.id.mainText);
tts.speak(textSpeak.getText(), TextToSpeech.QUEUE_FLUSH, null);
}
public void onInit( int i)
{
}
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == MY_DATA_CHECK_CODE)
{
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS)
{
tts = new TextToSpeech(this, this);
}
else
{
Intent installIntent = new Intent();
installIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivity(installIntent);
}
}
}
public void onDestory()
{
if (tts != null)
{
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
OnClickListener clearbutton = new OnClickListener()
{
public void onClick(View v)
{
TextView mainText = (TextView)findViewById(R.id.mainText);
mainText.setText("");
}
};
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.select8icons: setContentView(R.layout.layout_8);
break;
case R.id.select24icons: setContentView(R.layout.layout_24);
break;
case R.id.select63icons: setContentView(R.layout.layout_63);
break;
}
return true;
}
}
If anyone can help or has a better way to run the code, I love to hear your ideals. I try to research but every way I try gave me a error or something. Thanks in advance.
Your button will not work until TextToSpeech calls onInit. So you should disable btnSpeak until then.
Your TTS stuff looks fine. However, I dont see where you are binding any button to your onClick method and neither does your Activity seem to implement the onClickListener interface. You would need to do one or the other to get that onClick method to run.
So basically, I dont see any way your onClick method will ever get called.
Assuming you have some view/button in your layout the user is supposed to click to hear the tts, I think you want something like:
Button btnSpeak = (Button)findViewById(R.id.btnSpeak);
btnSpeak.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
TextView textSpeak = (TextView) findViewById(R.id.mainText);
tts.speak(textSpeak.getText(), TextToSpeech.QUEUE_FLUSH, null);
}
});
please guide me how can i call the menu on the other activity
Like here is the main activity
package com.droidnova.android.howto.optionmenu;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
public class ControlMenu extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.settings:
Intent intent = new Intent(this, ShowSettings.class);
startActivity(intent);
break;
case R.id.services: Toast.makeText(this, "You pressed the text!", Toast.LENGTH_LONG).show();
break;
case R.id.Quit:
new Thread(new Runnable() {
public void run() {
ControlMenu.this.finish();
// The following makes the Android Gods frown upon me
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(0);
}
}).start();
break;
default:
break;
}
return true;
}
}
now i have make another activity in which i need to show the same menu in this
package com.droidnova.android.howto.optionmenu;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class ShowSettings extends Activity {
Prefs myprefs = null;
final String tag = "CH12:ShowSettings";
AlertDialog.Builder adb;// = new AlertDialog.Builder(this);
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.showsettings);
this.myprefs = new Prefs(getApplicationContext());
// load screen
PopulateScreen();
this.adb = new AlertDialog.Builder(this);
final Button savebutton = (Button) findViewById(R.id.settingssave);
// create anonymous click listener to handle the "save"
savebutton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
try {
// get the string and do something with it.
final EditText email = (EditText) findViewById(R.id.emailaddress);
if (email.getText().length() == 0) {
AlertDialog ad = ShowSettings.this.adb.create();
ad.setMessage("Please Enter Your Email Address");
ad.show();
return;
}
final EditText serverurl = (EditText) findViewById(R.id.serverurl);
if (serverurl.getText().length() == 0) {
AlertDialog ad = ShowSettings.this.adb.create();
ad.setMessage("Please Enter The Server URL");
ad.show();
return;
}
// save off values
ShowSettings.this.myprefs.setEmail(email.getText().toString());
ShowSettings.this.myprefs.setServer(serverurl.getText().toString());
ShowSettings.this.myprefs.save();
// we're done!
finish();
} catch (Exception e) {
Log.i(ShowSettings.this.tag, "Failed to Save Settings [" + e.getMessage() + "]");
}
}
});
}
private void PopulateScreen() {
try {
final EditText emailfield = (EditText) findViewById(R.id.emailaddress);
final EditText serverurlfield = (EditText) findViewById(R.id.serverurl);
emailfield.setText(this.myprefs.getEmail());
serverurlfield.setText(this.myprefs.getServer());
} catch (Exception e) {
}
}
}
what should i write in the above code so my menu can also appear over here .
Create the menu as an xml resource, then you can access it from everywhere. Check the documentation. If you need to pass a certain data from only one activity to the other, extending Application seems too much, given you could just pass the info as en extra in the Intent. Besides there are "dangers" in that way of doing things (because of the activity lyfecicle) so you should use SharedPreferences in that case (instead of creating an Application global variable).
If you want to share some information or data or lists (or anything really) with your app, you should look at the following link and make your app as follows:
How to declare global variables in Android?