In one of my activities I have a button, when pressed it stores a string value inside a bundle that I want to send to another activity and display in a TextView.
Code for when the bundle is created:
public void enemy_seen(View view){
Intent send_enemy = new Intent(rear_gunner.this, pilot.class);
String sight = "ENEMY SPOTTED";
Bundle spotted = new Bundle();
spotted.putString("TAG",sight);
send_enemy.putExtras(spotted);
}
This code hapens on the button clicked and so far, from what I can tell this works....I believe.
When the bundle is called in second activity:
public class pilot extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pilot);
//sets screen orientation on created
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
Bundle bundle = getIntent().getExtras();
String something = bundle.getString("TAG");
TextView enemy = (TextView) findViewById(R.id.enemy_spotted);
enemy.setText(something);
}
}
The activity loads and crashes. So it must be something to do with when using the bundle I believe?
I don't see you starting the activity from the intent you set the bundle.
The activity will only receive the bundle you put in an intent if you fire that activity with that intent.
You should do a startActivity(send_enemy) after setting the bundle to the intent.
Related
I am trying to pass a value to a text view into a different activity but only when the button in the activity is clicked.
Here is the activity I am trying to set the text...
public static Button yes;
public static final String TEST_KEY = "test";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_question19);
yes = (Button) findViewById(R.id.finalYes);
yes.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent myIntent = new Intent(v.getContext(), Question13Activity.class);
startActivity(myIntent);
Intent i = new Intent(Question12Activity.this, HighRiskActivity.class);
i.putExtra(TEST_KEY, "SOME STRINGSSS");
}
});
Here is the HighRiskActivity that is the destination for updating the textview's value...
TextView t;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.high_risk);
t = (TextView) findViewById(R.id.abusedOrNah);
Bundle extras = getIntent().getExtras();
if(extras != null)
{
String value = extras.getString(TEST_KEY);
t.setText(value);
}
My problem is that no text is printing at the desired activity, am I passing in the wrong data?? Any help would be amazing, this is the last step to the app that I am creating :D
UPDATE
I do not want the Question12Activity to be directed to the HighRiskActivity when the button is clicked. I want it to go to the next activity but still be able to pass the text onto the HighRiskActivity once the button is clicked. Sorry for the confusion, hopefully that makes more sense :)
Try to pass data this way:
Intent i = new Intent(this, SecondActivity.class);
Bundle bundle = new Bundle();
//Add your data to bundle
bundle.putString(“test”, "Data you want to pass");
//Add the bundle to the intent
i.putExtras(bundle);
startActivity(i);
Now in your SecondActivity class you retrieve data as below:
Bundle bundle = getIntent().getExtras();
//get the data…
String stuff = bundle.getString(“test”);
I don't know what exactly you are trying to achieve by starting the first activity with the statements below
Intent myIntent = new Intent(v.getContext(), Question13Activity.class);
startActivity(myIntent);
But if the Activity you want to start is the HighRiskActivity then the following should fix your issue:
Get rid of the statements related to the Question13Activity mentioned above.
and add the call to start the actual HighRiskActivity like below:
Intent i = new Intent(Question12Activity.this, HighRiskActivity.class);
i.putExtra(TEST_KEY, "SOME STRINGSSS");
startActivity(i); //This line is important to start the new Activity.
So, I guess your major issue here is you didn't start the activity with the startActivity(i); call.
In Activity A you are using the internal intent bundle that is not public to you and private to the intent. In the Activity B you are asking the intent to instead look for a bundle that you provided yourself.
Try something like this in your Activity A
Intent intent = new Intent(context, YourActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelable("EXTRA1", "string data");
intent.putExtra(TEST_KEY, bundle);
startIntent(intent);
Here is a good writeup on the subject (see answer) Advantages of using Bundle instead of direct Intent putExtra() in Android
You dont need to define two intents in order to connect two Activities. Put the following inside onClick method
Intent i = new Intent(Question12Activity.this, HighRiskActivity.class);
i.putExtra(TEST_KEY, "SOME STRINGSSS");
startActivity(i);
The application aims to write a message at the first activity, and after clicking on a button, this message appears in the second activity.
But After installing the application on smartphone, when i click on the button, a message appears saying " application is stopped". Help me please." i USED ECLIPSE for developping this application"
The first activity code:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void login (View view) {
EditText user = (EditText) findViewById(R.id.user);
EditText password = (EditText) findViewById(R.id.password);
Intent myintent= new Intent (getApplicationContext(),Main2Activity.class); //permet de basculer de cette interface à la 2éme aprés avoir éxécuter "startActivity(myintent)";
Bundle b=new Bundle();
b.putString("username", user.getText().toString());
b.putString("password", password.getText().toString());
myintent.putExtras(b);
startActivity(myintent);
}
The second activity:
public class Main2Activity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
TextView mes=(TextView)findViewById(R.id.mes);
Bundle b=getIntent().getExtras();
string user=(string) b.get("username");
string pass=(string) b.get("password");
mes.setText("user:"+user +"password:"+pass);
}
Try a simpler way to pass data as a String,without using bundle.
MainActivity.java
Intent i= new Intent(MainActivity.this,Main2Activity .class);
i.putExtra("username",user.getText().toString());
i.putExtra("password",password.getText().toString());
startActivity(i);
and in Main2Activity .java
String name=getIntent().getExtras().getString("username");
txtView.setText(name);
String pass=getIntent().getExtras().getString("password");
txtView2.setText(pass);
Change this line
Intent myintent= new Intent (getApplicationContext(), Main2Activity.class)
to
Intent myintent= new Intent (this, Main2Activity.class)
And see if it solves the problem :)
On Your Second Activity(Main2Activity) File:
The Line After
Bundle b=getIntent().getExtras();
you have written (string) instead of (String).
So please change it...if u have written it mistakenly.
and #AugustoCarmo answer is also correct.
Try That Also.
I am new to Android apps. I'm doing JSON parsing for my listview. When I click on my list Image I need to pass that Image to second activity. I'm trying to pass it using intent and bundles. But I have image URL so I don't know how to pass it. I searched everywhere others are passing bitmaps or id.
Class- FirstActivity
Bundle bundle= new Bundle();
bundle.putString("imageUrl",<url for image>);
Intent i= new Intent(FirstActivity.this,SecondActivity.this);
i.putExtras(bundle);
statrActivity(i)
Class- SecondActivity
Bundle bundle = getIntent().getExtras();
String image_url =bundle.getString("imageUrl");
your first activity
Intent intent = new Intent(MainActivity.this,AnotherActivity.class);
intent.putExtras("IMAGE_URL",your_image_url);
startActivity(intent);
in Second activity where you want data
Bundle bundle = getIntent().getExtras();
String image_url =bundle.getString("IMAGE_URL");
pass image_url from one activity to another activity, and then use picasso lib to show image.
In FirstActivity, in the onClickListener, add this
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtras(SecondActivity.KEY_IMAGE_URL, image_url);
startActivity(intent);
And in SecondActivity
public static final String KEY_IMAGE_URL = "image_url";
#Override
protected void onCreate(Bundle savedInstanceState) {
...
if (getIntent().hasExtra(KEY_IMAGE_URL)) {
String imageUrl = getIntent().getStringExtra(KEY_IMAGE_URL);
}
}
I am having some trouble with my simple app. The app starts in the MainActivity where you can press a camera icon. This opens an implicit intent for taking a photo. When the photo is taken, another activity DisplayImageActivity is opened. This activity consists of two fragments: one that holds an ImageView for displaying the photo and another one that holds some TextViews that displays some information about the photo (filename, size, location etc.). I use a ViewPager for having horizontal swipe capabilities.
Now to the problem. I should note that this is not a consistent problem. Sometimes the app crashes, sometimes it works just fine. The problem lies in getting the image path from the onActivityResult in MainActivity to the two fragments so I can get the image and info. Here is my onActivityResult method:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 42 && resultCode == RESULT_OK) {
Log.d(TAG, "MainActivity onActivityResult method called.");
Intent intentShowPicture = new Intent(this, DisplayImageActivity.class);
intentShowPicture.putExtra(PICTURE_KEY, imgPath);
startActivity(intentShowPicture);
}
}
So I just put the image path I get from taking the picture in the bundle and start the DisplayImageActivity. Then in my DisplayImageActivity, I do this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_image);
Log.d(MainActivity.TAG, "DisplayImageActivity onCreate called.");
imgPath = getIntent().getExtras().getString(MainActivity.PICTURE_KEY);
mAdapter = new FragmentAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mAdapter);
Then I have a method that just return the straing imgPath:
public String getImgPath() {
return imgPath;
}
Then inside the fragment (PictureFragment) I try to retrieve the imgPath like this:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
DisplayImageActivity activity = (DisplayImageActivity) getActivity();
imgPath = activity.getImgPath();
But as I mentioned earlier, sometimes the getImgPath method just returns null and the app crashes when I try to retrieve the photo. But sometimes it works fine. I am kinda lost as to why this is. Is it because the fragment is sometimes constructed before the imgPath variable is assigned in the DisplayImageActivity, so the variable is just null?
I am kinda new to android, so this might not be the best approach. I just did it from the top of my head. Any ideas why this is happening?
If you want to pass data from an Activity to a Fragment, you could use this approach:
In the Activity:
Bundle bundle = new Bundle();
String imgPath = "path/to/my/image";
bundle.putString("imgPath", imgPath );
PictureFragment frag = new PictureFragment();
frag.setArguments(bundle);
transaction.replace(R.id.fragment_single, frag);
transaction.commit();
Fragment:
Reading the value in fragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
String myValue = this.getArguments().getString("imgPath");
...
...
...
}
For more information, take a look at this question How to pass a variable from Activity to Fragment, and pass it back?
I want to pass data from login page to main activity which contains my menus which works fine. But the problem is that suppose a user clicks on a particular menu and open another activity lets say activity A. when the user press the go back button to switch to main activity the bundle gives a null pointer exception. Here is my login intent
Bundle m=new Bundle();
m.putString("userid",userid);
Intent intent=new Intent(login.this,main.class);
intent.putExtras(m);
startActivity(intent);
///////////////
main
String userid;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bundle b=getIntent().getExtras();
}
Above code works fine but the problem arise when main activity creates another activity A. when Activity A want to go back to parent activity (main) activity gives a null pointer exception.
Intent inte=new Intent(A.this,main.class);
startActivity(inte);
Why not store the userid in SharedPreferences.
Setter:
SharedPreferences pref = getApplicationContext().getSharedPreferences("UserAccount", 0);
Editor editor = pref.edit();
editor.putString("userid", userid);
editor.commit();
Getter:
pref.getString("userid", "default_value");
Clear:
editor.remove("userid");
editor.commit();
You are trying access bundle when returning to Main Activity, but you dont create bundle in your intent. Check for it as follows,
if(getIntent().getExtras() != null)
{
Bundle b=getIntent().getExtras();
}
If you want the bundle in activity A then send the bundle to Activity A and when you are coming back from Activity A then call the intent with bundle to main activity again.
Use this:
Intent i = new Intent(this, main.class);
i.putExtra("Key1", "ABC");
i.putExtra("Key2", "123");
// Starts main.class
startActivity(i);
Now in main.class
Bundle extras = getIntent().getExtras();
if(extras!=null){
String a = extras.getString("Key1");
String b = extras.getString("Key2");
}
Hope this will work..