Click here to Skip to main content
15,851,387 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
my app need two activity.a button in the first activity must take to the another activity.but when i click the button it shows unfortunately my app stopped.i used intents.where is the problem.it is basically a text to speech app.
Java
MainActivity.java
@Override
public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.launcher);

	btnCtlr = (Button) findViewById(R.id.button1);
	btnCtlr.setOnClickListener(new ButtonClickHandler());
}

public class ButtonClickHandler implements View.OnClickListener {
	public void onClick(View view) {

		Intent intObj = new Intent(MainActivity.this, Welcome.class);

		startActivity(intObj);
	}
welcome.java
@Override
public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_main);

	ed1 = (EditText) findViewById(R.id.editText);
	b1 = (Button) findViewById(R.id.button);

	t1 = new TextToSpeech(getApplicationContext(),
			new TextToSpeech.OnInitListener() {
				@Override
				public void onInit(int status) {
					if (status != TextToSpeech.ERROR) {
						t1.setLanguage(Locale.UK);
					}
				}
			});

	b1.setOnClickListener(new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			String toSpeak = ed1.getText().toString();
			Toast.makeText(getApplicationContext(), toSpeak,
					Toast.LENGTH_SHORT).show();
			t1.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
		}
	});
}

public void onPause() {
	if (t1 != null) {
		t1.stop();
		t1.shutdown();
	}
	super.onPause();
}

@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;
}

@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);
}
Posted
v2

1 solution

If you haven't, you need to define your second activity in manifest file under activity.
For more info: Android Documentation[^]

-KR
 
Share this answer
 
v2
Comments
Member 11993529 3-Oct-15 3:12am    
it work out.thank you.
Krunal Rohit 3-Oct-15 3:14am    
Glad I could help.

-KR

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900