I want to show the value inserted by user in first window to the next window.
I am accepting the User weight & height in first window and I want to show it on the second screen as Your weight & Height.
I search a lot and even tried a code but in emulator m getting forcefully closed error.
First Activity :
public class BMI_Main extends Activity
{
EditText BMI_weight;
public String weight;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.bmi_main);
Button submit =(Button)findViewById(R.id.BMI_submit);
BMI_weight = (EditText)findViewById(R.id.BMI_EdTx_kg);
submit.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
weight = BMI_weight.getText().toString();
// create a bundle
Bundle bundle = new Bundle();
// add data to bundle
bundle.putString("wt", weight);
// add bundle to the intent
Intent intent = new Intent(v.getContext(), BMI_Result.class);
intent.putExtras(bundle);
startActivityForResult(intent, 0);
}
}
);
Second Activity :
public class BMI_Result extends Activity
{
TextView Weight = (TextView)findViewById(R.id.BMI_TxtVw_wt);
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.bmi_result);
//get the bundle
Bundle bundle = getIntent().getExtras();
// extract the data
String weight = bundle.getString("wt");
Weight.setText(weight);
}
So please help me for it..
As far as I can see you have the following member definition in BMI_Result:
TextView Weight = (TextView)findViewById(R.id.BMI_TxtVw_wt);
But you can only call findViewById after the class was initialized, since it is a member function of the View class, so change this line to:
TextView Weight;
And add this line to the onCreate method right after setContentView(...):
Weight = (TextView)findViewById(R.id.BMI_TxtVw_wt);
Edit: It said "...right after super.onCreate(...)", now it's correct ;)
You should override onCreate()
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
And the token at the end of onCreate is wrong.
You should use the Context.startActivity(Intent intent) method in your first window/Activity.
Store your data which you want to pass to the second window/Activity in the Intent object, like:
Intent intent = new Intent();
intent.putExtra("weight", weight);
intent.putExtra("height", height);
this.startActivity(intent);
And retrieve them in the second screen/Activity in the onCreate method, like:
Intent intent = getIntent(); // This is the intent you previously created.
int weight = intent.getExtra("weight");
int height = intent.getExtra("height");
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.
Hi in android i know how to send image from one activity to another and send text from text view to another,separately. But i want to know in an activity we have both text view and image view.But my need is i want to send both text and image from one activity to another.Any suggestions is welcomed.
Thank you.
What do you mean by sending along an image? For passing a string, use Intent extras and Bundle.
In your first activity...
someButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(CurrentActivity.this, NextActivity.class);
intent.putExtra("text_contents", someTextView.getText().toString();
startActivity(intent);
}
});
In the second activity onCreate(), you retrieve the intent extras you passed via Bundle...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
String someString = bundle.getString("text_contents");
}
}
If your "image" is an R.drawable resource, then you could simply add that as well to the intent extras:
intent.putExtra("image_resource", R.drawable.some_image_resource);
And retrieve it from the Bundle like:
int someImageResource = bundle.getInt("image_resource");
And from there you can apply it to some ImageView:
someImageView.setImageResource(someImageResource);
EDIT: made a slight correction + if your "image" is a bitmap, then see Anjali's answer.
I don't understand why I my app keeps on crashing. According to LogCat, it is caused by a NullPointerExeption on a line, however, I cannot find why it is occurring. What's supposed to happen is an image resource for an ImageView is changed in MainActivity by pressing a button on another activity. I have followed multiple guides, but get the recurring error. From what I can see it occurs when extra information is passed through an Intent, something is null but I cannot find it.
LogCat
05-03 22:33:03.158: E/AndroidRuntime(31629): Caused by: java.lang.NullPointerException
05-03 22:33:03.158: E/AndroidRuntime(31629): at com.crackedporcelain.crackedcalibration.MainActivity.onCreate(MainActivity.java:21)
Line 21 (offender in MainActivity)
String gridPressed = content.getString("gridPressed");
Main Activity (receiver)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bundle content = getIntent().getExtras();
// Set image based on which button was pressed
String gridPressed = content.getString("gridPressed");
ImageView spotlightOne = (ImageView) findViewById(R.id.variousOne);
if (gridPressed.equals("one")) {
spotlightOne.setImageResource(R.drawable.one);
} else if (gridPressed.equals("two")) {
spotlightOne.setImageResource(R.drawable.two);
} else {
spotlightOne.setImageResource(R.drawable.one);
}
ImageView buttonGrid = (ImageView) findViewById(R.id.phoneReference);
buttonGrid.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(
"com.crackedporcelain.crackedcalibration.IDENTIFICATION"));
}
});
}
Other Activity (sender)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.identification);
ImageView gridButton1 = (ImageView) findViewById(R.id.buttonImage1);
ImageView gridButton2 = (ImageView) findViewById(R.id.buttonImage2);
gridButton1.setOnClickListener(this);
gridButton2.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.buttonImage1:
Intent oneA = new Intent(IdentificationActivity.this, MainActivity.class);
oneA.putExtra("gridPressed", "one");
startActivity(oneA);
break;
case R.id.buttonImage2:
Intent oneB = new Intent(IdentificationActivity.this, MainActivity.class);
oneB.putExtra("gridPressed", "two");
startActivity(oneB);
break;
}
}
Additional Info
Layouts for these classes are setup correctly, they work fine. It only force closes when I try to use Intents.
All help is appreciated! Thanks!
The received intent did not contain any extra values. Make sure the code sending the intent had added to the intent an extra String value with the name "gridPressed". You can use Intent.putExtra(String, String) for this purpose.
Intent.getExtras() will return null if there are no extra values. In addition, Bundle.getString(String key) will return null if the Bundle does not contain the key.
Finally, be sure to check that MainActivity is not invocable in any other way (like when the app icon is clicked). If that is possible, then this version of onCreate() will work better:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView spotlightOne = (ImageView) findViewById(R.id.variousOne);
// Set image based on which button was pressed, if any
String gridPressed = getIntent().getStringExtra("gridPressed");
if ("one".equals(gridPressed)) {
spotlightOne.setImageResource(R.drawable.one);
} else if ("two".equals(gridPressed)) {
spotlightOne.setImageResource(R.drawable.two);
} else {
spotlightOne.setImageResource(R.drawable.one);
}
ImageView buttonGrid = (ImageView) findViewById(R.id.phoneReference);
buttonGrid.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(
"com.crackedporcelain.crackedcalibration.IDENTIFICATION"));
}
});
}
You're getting a NPE because you are not sending a bundle from the sender activity rather just a string extra. So, to access the String extra in the receiver activity, use :
getIntent().getStringExtra("gridPressed");
To send bundles, you need to first create a bundle, then add extras to that bundle and then pass the bundle with the intent :
Bundle bundle = new Bundle();
bundle.putString("KEY", "VALUE");
intent.putExtras(bundle);
If you send a bundle as mentioned in 2, your existing code in the receiving activity will work, otherwise you can change the code in the receiving activity as mentioned in 1.
I am trying to make a button in one activity (SetupMenu) that, when pressed, puts an int into the intent and carries that over to the next activity (IntroActivity) where a textView will retrieve the int and display it.
Problem is, when the app runs and I get to the activity and press the button, the app crashes and my emulator tells me that "Unfortunately [my app] has stopped working."
I feel like I've tested every possible angle to get this to work. I should note that the button has worked fine, the textview has worked fine, everything else is working smoothly - I only run into issues when I try retrieving the intent and displaying it in textView. I tried passing through a String instead of an Int and also had issues (my string would not appear). Any pointers?
SetupMenu activity (here I put an int into my intent):
public class SetupMenu extends Activity {
public final static String extra_progress_key = "com.example.angelsanddemons.track_players";
public int track_players = 0;
public void to_intro(View view) {
Intent intent = new Intent(this, IntroActivity.class);
intent.putExtra(extra_progress_key, track_players);
startActivity(intent);
}
IntroActivity activity (here I try to retrieve the int from the intent):
public class IntroActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
int temp = intent.getIntExtra(SetupMenu.extra_progress_key, 0 );
TextView textView = new TextView(this);
textView.setText(temp);
setContentView(textView);
}
}
One problem is that you can't set a TextView's text to an int; you'll need to first convert it to an string. It's also not a good idea to be manipulating views before you've inflated them, so perhaps your onCreate() should be:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
int temp = intent.getIntExtra(SetupMenu.extra_progress_key, 0 );
TextView textView = new TextView(this);
setContentView(textView);
textView.setText(String.valueof(temp));
}
I see nothing that ensure that SetupMenu activity is created and in memory when IntroActivity is launched. To make sure, don't pass the variable, but the string itself and check if it work:
int temp = intent.getIntExtra("com.example.angelsanddemons.track_players", 0 );
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.