import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
int count = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button abButton = (Button) findViewById(R.id.button1);
final TextView changelingtext = (TextView) findViewById(R.id.changeling);
abButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(getBaseContext(), "Buttons are working baby", Toast.LENGTH_LONG).show();
count++;
String a = Integer.toString(count);
changelingtext.setText(a);
gotonextpage(v);
}
});
}
public void gotonextpage(View view){
Intent intent = new Intent(this, SecondpageActivity.class);
startActivity(intent);
intent.putExtra("count", count);
//finish(); if you want to end this page
}
#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;
}
}
First class is above, second class is below
package com.example.collegematch;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class SecondpageActivity extends Activity {
int values;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.secondpage);
Intent intent = getIntent();
values = intent.getExtras().getInt("count");
Button exitButton = (Button) findViewById(R.id.exit);
Button textbutton = (Button) findViewById(R.id.coolbutton);
TextView texty = (TextView) findViewById(R.id.cooltext);
textbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(getBaseContext(), Integer.toString(values), Toast.LENGTH_LONG).show();
System.out.println(values);
}
});
exitButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(getBaseContext(), "Seeya", Toast.LENGTH_LONG).show();
finish();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.secondpage, menu);
return true;
}
}
In the mainActivity, everytime button abButton is pressed, it increases the count variable by 1. It also creates a new intent and sends that variable via extra to that intent.
In the second activity, the "values" variable getting the data from the intent is giving me a null pointer exception. Why?
Intent intent = new Intent(this, SecondpageActivity.class);
startActivity(intent);
intent.putExtra("count", count);
Change to
Intent intent = new Intent(this, SecondpageActivity.class);
intent.putExtra("count", count);
startActivity(intent);
You are setting the extra after you've already started the 2nd activity
just alter these two code lines,
Intent intent = new Intent(this, SecondpageActivity.class);
intent.putExtra("count", count);
startActivity(intent);
Your SecondActivity Intent start before setting the extra count to it.
Related
This question already has answers here:
Why does my Android app crash with a NullPointerException when initializing a variable with findViewById(R.id.******) at the beginning of the class?
(9 answers)
Closed 6 years ago.
I have this app but for some reason I can't get it to work past the launch screen. After a user clicks an item on the listview it generates a random int in the activity and based on that changes the textView. If the user's input matches the right .contains strings it'll print out a toast.
Here is the launch activity:
package com.eoincoogan.owner.omegaphi;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class LaunchActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launch);
ListView lv = (ListView) findViewById(R.id.subjectListView);
String[] subjectArray = new String[]{"Physics","Chemistry","Economics", "Geography"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, subjectArray);
lv.setAdapter(adapter);
lv.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (position == 0) {
Intent intent = new Intent(LaunchActivity.this, PhysicsFragment.class);
startActivity(intent);
} else if (position == 1) {
Intent intent = new Intent(LaunchActivity.this, ChemFragment.class);
startActivity(intent);
} else if (position == 2) {
Intent intent = new Intent(LaunchActivity.this, EconFragment.class);
startActivity(intent);
} else if (position == 3) {
Intent intent = new Intent(LaunchActivity.this, GeoActivity.class);
startActivity(intent);
}
}
}
);
}
}
Here is the activity after the LaunchActivity:
package com.eoincoogan.owner.omegaphi;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Random;
public class PhysicsFragment extends AppCompatActivity {
private TextView definitionTextView = (TextView) findViewById(R.id.definitonTextView);
String definitionText = definitionTextView.getText().toString();
private EditText userInputText = (EditText) findViewById(R.id.userInputText);
String userInput = userInputText.getText().toString();
Random rand = new Random();
int value = rand.nextInt(3);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_physics_fragment);
Button button = (Button)findViewById(R.id.button);
Button buttonTwo = (Button)findViewById(R.id.button2);
final Toast firstToast = Toast.makeText(this, "Correct", Toast.LENGTH_LONG);
final Toast secondToast = Toast.makeText(this, "Correct", Toast.LENGTH_LONG);
button.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
switch (value) {
case 0:
definitionText = "Diode";
if (userInput.contains("Current" + "one way" + "allows")) {
firstToast.show();
} else {
secondToast.show();
}
case 1:
definitionText = "First law of refraction";
if (userInput.contains("insidence" + "normal" + "refracted" + "ray")) {
firstToast.show();
} else {
secondToast.show();
}
case 2:
definitionText = "Second law of refraction";
if (userInput.contains("angle" + "incidence" + "reflection" + "equal")) {
firstToast.show();
} else {
secondToast.show();
}
}
Intent intent = getIntent();
finish();
startActivity(intent);
}
}
);
//button to launch screen//
buttonTwo.setOnClickListener(
new Button.OnClickListener(){
#Override
public void onClick(View v) {
Intent myIntent = new Intent(PhysicsFragment.this, LaunchActivity.class);
startActivity(myIntent);
}
}
);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_driver, 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);
}
}
Any help would be much appreciated.
add LaunchActivity to Manifest file
<activity android:name=".LaunchActivity"></activity>
java file,
Intent myIntent = new Intent(context, LaunchActivity.class);
startActivity(myIntent);
I found this code to create a share window:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ShareActionProvider;
public class AndroidShareActionProviderActivity extends Activity {
private ShareActionProvider myShareActionProvider;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.completed);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
MenuItem item = menu.findItem(R.id.menu_item_share);
myShareActionProvider = (ShareActionProvider)item.getActionProvider();
myShareActionProvider.setShareHistoryFileName(
ShareActionProvider.DEFAULT_SHARE_HISTORY_FILE_NAME);
myShareActionProvider.setShareIntent(createShareIntent());
return true;
}
private Intent createShareIntent() {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT,
"http://www.example.com/");
return shareIntent;
}
}
My question is, is it possible to make this share window open when an ImageButton outside the actionbar is clicked instead and if possible how to do that?
yes and not so difficult either. what you need to do is lauch the intent in your onclick. since that method returns the view u want it will be something like this
this.YOURIMAGEBUTTON.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(createShareIntent()));
}
});
I need help, I am making a simple application, and I donĀ“t know how to return to the MainActivity the string from the spinner and the name of the person when i click in the "Aceptar" button.
MainActivity.java
package com.example.holaamigos;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
public final static String EXTRA_SALUDO = "com.example.holaamigos.SALUDO";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText txtNombre = (EditText)findViewById(R.id.TxtNombre);
final Button btnHola = (Button)findViewById(R.id.BtnHola);
final CheckBox checkbox1 =(CheckBox)findViewById(R.id.checkBox1);
checkbox1.setOnCheckedChangeListener(new OnCheckedChangeListener(){
#Override
public void onCheckedChanged(CompoundButton arg0,
boolean checked) {
if (checked)
{
Toast.makeText(checkbox1.getContext(), "Activo", Toast.LENGTH_LONG).show();
btnHola.setVisibility(0);
btnHola.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, ActivitySaludo.class);
String saludo = txtNombre.getText().toString();
intent.putExtra(EXTRA_SALUDO, saludo);
startActivity(intent);
}
});
}
else
{
Toast.makeText(checkbox1.getContext(), "Inactivo", Toast.LENGTH_SHORT).show();
btnHola.setVisibility(View.INVISIBLE);
}
}
});
}
public void HobbyReturn(int requestcode, int resultadocode, Intent data) {
if (resultadocode == ActivitySaludo.ACEPTAR_OK); {
String string = data.getStringExtra(ActivitySaludo.ACEPTAR_OK);
}
}
#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;
}
}
ActivitySaludo
package com.example.holaamigos;
import com.example.holaamigos.R.string;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
public class ActivitySaludo extends Activity {
public static final String ACEPTAR_OK = "com.example.holaamigos.ACEPTAR_OK";
String myspinner;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_saludo);
Intent intent = getIntent();
String saludo = intent.getStringExtra(MainActivity.EXTRA_SALUDO);
TextView txtCambiado = (TextView) findViewById(R.id.TxtSaludo);
txtCambiado.setText(getString(R.string.hola_saludo) + " " + saludo);
final Spinner spinner = (Spinner)findViewById(R.id.SpinnerSaludo);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.hobby, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener () {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
parent.getItemAtPosition(pos);
myspinner = spinner.getItemAtPosition(pos).toString();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
//another call
}
});
final Button BtnAceptar=(Button) findViewById(R.id.buttonAceptar);
BtnAceptar.setOnClickListener(new OnClickListener (){
#Override
public void onClick(View v) {
Intent iboton = new Intent();
iboton.putExtra("HOBBY", myspinner);
setResult(ACEPTAR_OK, iboton);
finish();
}
});
}
}
You need to start your second activity with the flag that you are waiting for a result, so instead of startActivity you need to make use of startActivityForResult.
If you need a little bit more information take a look at this tutorial it should cover all you need to get things working.
Ok So i want to pass values from One activity to another using getextrand putextra method.
In Second Activity in which I want to receive data is full of contents like Buttons and Text view. and i want to set that certain value which i have received from MainActivity to a particular text box.
setContenView(R.id.intent)
is the easiest one method to show a string but what if I want to Set this value to one or more textview. My code is here
MainActivity
package com.prashant.cookbook;
import android.os.Bundle;
import android.app.Activity;
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;
public class MainActivity extends Activity {
static String Message_send="Prashant";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText et=(EditText)findViewById(R.id.editText1);
Button send=(Button)findViewById(R.id.send);
final Intent msg_send= new Intent(this,Second.class);
String MSG= et.getText().toString();
msg_send.putExtra(Message_send, MSG);
send.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startActivity(msg_send);
}
});
}
#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;
}
}
SecondAvtivity
package com.prashant.cookbook;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.TextView;
public class Second extends Activity {
private TextView tv;
private Intent rcv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
tv = (TextView) findViewById(R.id.msg_show);
rcv = getIntent();
String Show_msg;
Show_msg=rcv.getStringExtra(MainActivity.Message_send);
tv.setText(Show_msg);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.second, menu);
return true;
}
}
but when I run this code I got nothing but a blank second Activity Not even a default text
get Value entered by user in EditText on Button click and then use msg_send.putExtra for placing value in Intent as:
send.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String MSG= et.getText().toString(); //<< get value from EditText here
msg_send.putExtra(Message_send, MSG);
startActivity(msg_send);
}
});
I have a problem with my android app with speech recognition.My problem is that I want to show the same speech recognition input screen as is shown from the jellybean keyboard. If I invoke the Speech recognition intent from code, it does not keep listening and stops prematurely.
There must be a simple way to do this, but I can't find it.
here is my code
package com.bsdcservices.recognitiontest;
import java.util.ArrayList;
import java.util.Locale;
import android.os.Bundle;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.speech.RecognizerIntent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private final String TAG = "RecognitionTest";
private final int SPEECHRECOGNITION_RESULTCODE = 0;
private EditText editText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.textbox);
Button recognize = (Button) findViewById(R.id.recognize);
recognize.setOnClickListener(buttonClickListener);
}
public OnClickListener buttonClickListener = new OnClickListener(){
#Override
public void onClick(View v) {
startSpeechRecognitionActivity();
}
};
#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;
}
void startSpeechRecognitionActivity(){
// MyApplication.speechRecognitionActive = true;
// main.startSpeechRecognizer(TABSPEAK);
try{
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getClass().getPackage().getName());
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault().toString());
startActivityForResult(intent, SPEECHRECOGNITION_RESULTCODE);
} catch (ActivityNotFoundException error) {
Toast.makeText(getBaseContext(), "Speech recognition is not supported by your device" , Toast.LENGTH_LONG).show();
} catch( RuntimeException error ) {
Toast.makeText(getBaseContext(), error.getMessage() , Toast.LENGTH_LONG).show();
error.printStackTrace();
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
try
{
if (requestCode == SPEECHRECOGNITION_RESULTCODE) {
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> text = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
editText.append(text.get(0));
}
}
} catch( RuntimeException error ) {
Toast.makeText(getBaseContext(), error.getMessage() , Toast.LENGTH_LONG).show();
error.printStackTrace();
}
}
}