Passing one String from Activity A to Activity B

Put your problem here if it does not fit any of the other categories.

Passing one String from Activity A to Activity B

Postby Marbert » Wed Feb 15, 2012 12:45 am

Sorry for that (stupid? :oops: ) question, I've read a lot about it, but always get the same error...

I have a string in Activity A that should be passed to Activity B and displayed in a TextView.

This is the code in Activity A:

Code: Select all
String sToSend = "pass this string...";
       Intent i = new Intent(this, ActivityB.class);
       i.putExtra("justTesting", sToSend);
       startActivity(i);


And this is the code in Activity B (a button is pressed (this code is in the onClick() of the button), then the TextView should display the string from the first Activity):

Code: Select all
try
            {
                 //getting String from other Activity
            Intent i = getIntent();
            String sGetIt = iTestIntent.getExtras().getString("justTesting");
            tvDisplayString.setText("tvOffsetValueFromOtherActivity: "+sGetIt);
            }
            catch (Exception e)
            {
               e.printStackTrace();
               Toast ng = Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT);
               ng.show();
            }


I get a NullPointerException at the line that says "String sGetIt=...".

Thanks in advance,
Marbert
Marbert
Developer
Developer
 
Posts: 30
Joined: Sat Feb 04, 2012 7:31 pm

Top

Re: Passing one String from Activity A to Activity B

Postby Phyll » Wed Feb 15, 2012 5:14 am

Hi Marbert,

Why isn't that something like this in your code:

Intent startingIntent = getIntent();
path = startingIntent.getStringExtra("path");

You see here in your code:

Intent i = getIntent();
String sGetIt = iTestIntent.getExtras().getString("justTesting");

You make the intent i but then use the intent iTestIntent. Where does that come from? But more importantly, what is it initialized from? Maybe something with no extras?

Hope this helps.

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Re: Passing one String from Activity A to Activity B

Postby Marbert » Wed Feb 15, 2012 1:05 pm

Hi Phyll,

Sorry for the wrong Intent-name. I posted my code here, but then fixed it in Eclipse a few minutes later but not here in the forum.
I'm now using .getStringExtras.

After a little bit of trying, Activity A:

Code: Select all
String sToSend = "pass this string...";
      Intent i = new Intent(this, ActivityB.class);
      i.putExtra("justTesting", sToSend);
      startActivity(i);


And Activity B:

Code: Select all
Intent i = getIntent();
            String sGetIt = i.getStringExtra("justTesting");
            tvDisplayString.setText("tvDisplayString: "+sGetIt);


No Exception is used anymore (thanks :)), but it just displays 'null' for sGetIt.
What is path in your code? A String?
So you see I'm not very familiar with intents. Basically I know what they do, but they won't work in my application. :wink:

Thanks in advance,
Marbert
Marbert
Developer
Developer
 
Posts: 30
Joined: Sat Feb 04, 2012 7:31 pm

Re: Passing one String from Activity A to Activity B

Postby Phyll » Wed Feb 15, 2012 1:38 pm

Hi Marbert,

Here's a whole project with activities A and B passing a string.

Hope this helps.

Phyll
Attachments
AppA.zip
Example Project
(37.36 KiB) Downloaded 33 times
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Re: Passing one String from Activity A to Activity B

Postby Marbert » Wed Feb 15, 2012 2:00 pm

Hi Phyll,

FIrst I have to say thanks for your help here, really great!

But your app changes the activity after pressing the button.

I want this: in Activity A there's a EditText. The user can write a value into it and the value is needed for calculating a result in Activity B (B is the main part of my app).

For testing I want the value from A and display it with a TextView in B. I don't want to switch Activities, the user shouldn't recognize any change of the Activities.

I hope I'm not too annoying. ;)

Thanks,
Marbert
Marbert
Developer
Developer
 
Posts: 30
Joined: Sat Feb 04, 2012 7:31 pm

Re: Passing one String from Activity A to Activity B

Postby Phyll » Wed Feb 15, 2012 9:41 pm

Hi Marbert,

As far as I know, only one Activity is "active" at a time, hence the name Activity. In Android, Activity equates to screen or window. That being said, when and how does the user get to activity B after they have entered something into the EditText? Is activity B not really another activity?

I'm not annoyed just confused I guess.

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Top

Re: Passing one String from Activity A to Activity B

Postby Marbert » Wed Feb 15, 2012 10:50 pm

Activty A is the Settings-Activity. The user can set somethings which are important for Activity B (B is my Main-Activity).
For example, one thing is the maximum value for the seekbar. The seekbar is in Activity B, the EditText with the maximum value in Activity A. Activity A is a *.java-file (MainPart.java), Activity B is another *.java-file (Settings.java).
Marbert
Developer
Developer
 
Posts: 30
Joined: Sat Feb 04, 2012 7:31 pm

Re: Passing one String from Activity A to Activity B

Postby Phyll » Wed Feb 15, 2012 10:56 pm

Hi Marbert,

Activity A and B aren't extended from Activity?

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Re: Passing one String from Activity A to Activity B

Postby Marbert » Wed Feb 15, 2012 11:04 pm

Both of them are extendet from Activity.
I thought this would be the best possibility to do this. But if you show me an other way, I'd be happy. :)
Marbert
Developer
Developer
 
Posts: 30
Joined: Sat Feb 04, 2012 7:31 pm

Re: Passing one String from Activity A to Activity B

Postby Phyll » Thu Feb 16, 2012 12:31 am

Hi Marbert,

Well I must be dense or something but can you send me any more code then you already have? I mean, I sent you code that correctly did what your code in the first post tried to do. Then I sent you a project with two activities that passed a string from the first to the second. But that still isn't somehow what you want to do. So I'm just not understanding what it is you want to do I guess.

I'm sorry,

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Re: Passing one String from Activity A to Activity B

Postby Marbert » Thu Feb 16, 2012 1:11 am

I'm very sorry... :O

Here's the important part.

MainActivity.java:
- Button btnGetString
- TextView tvMain

Settings.java:
- TextView txtValue with text in it
- String sToSend = text from txtValue

What I want to do:
After pressing btnGetString the String sToSend should be shown in the TextView tvMain.
I don't want to change the activity, the user just should insert a value into txtValue and then the user should see this value in tvMain.

I hope I explained it well enough. :)
Attachments
Activity String.zip
(23.3 KiB) Downloaded 18 times
Marbert
Developer
Developer
 
Posts: 30
Joined: Sat Feb 04, 2012 7:31 pm

Re: Passing one String from Activity A to Activity B

Postby Phyll » Thu Feb 16, 2012 3:19 am

Hi Marbert,

I remember now, you wanted a dialog.

So here it is.

Hope this helps.

Phyll
Attachments
StringDialog.zip
String Dialog Example
(60.06 KiB) Downloaded 23 times
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Re: Passing one String from Activity A to Activity B

Postby Marbert » Thu Feb 16, 2012 2:36 pm

Great, absolutely great, thanks! That's exactly the way I wanted to have the dialog! :)


(Hopefully) my last question(s):

I modified the whole thing a little bit.

Now we have these things:

Choose.java:
First thing the user sees when he opens the app. There are to buttons. Each button goes to another activity.

Settings.java:
Here the user enters text.

MainActivity.java:
After pressing the button the user should see the text he has entered in Settings.java. No popup-window required, just the text should be shown.


How to accomplish this?

Thanks Phyll for your help!! Great! :)
Attachments
ActivityString2.rar
(62.83 KiB) Downloaded 25 times
Marbert
Developer
Developer
 
Posts: 30
Joined: Sat Feb 04, 2012 7:31 pm

Re: Passing one String from Activity A to Activity B

Postby Phyll » Thu Feb 16, 2012 6:09 pm

Hi Marbert,

I'm still confused. What happened to the dialog? What happened to the ok and cancel buttons?

Now there is just a floating TextEdit without any instructions or anything that looks familiar to an Android user.

I'm only guessing but I think you want to click the settings button on the choose activity and goto the settings activity. But clicking on the other button just takes you to the main activity. How are you going to return from the settings activity. Do you want buttons as there were in the example? Is It no longer important that it be a dialog?

Is it that you just can't figure out how to take the example from my last post and make it work in your app because I think any and all answers to your questions so far are in there.

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Re: Passing one String from Activity A to Activity B

Postby Marbert » Thu Feb 16, 2012 6:38 pm

Hi Phyll,
the Dialog is a completely different thing (it was another topic). ;)
My last version I uploaded here is just the core problem, all the other things are solved.

My app has 3 activities:

The user opens the app and sees the Choose-Actitivy. In my 'real' app, there's a spinner and you can choose which activity you want to see. Here there a just two buttons (to keep things simple).

So the user goes to the Settings and enters a string into the EditText. Then he goes back to Choose and presses the other button -> he comes to the MainActivity. Here he presses the button and the content of the EditText (which is in Settings) should be shown here.

It has this sense: in my 'real' app I have a SeekBar which is important for calculating. In the Settings the user can enter a maxValue for the SeekBar. Then I want to read and save files from the sd (reading and saving is working), but the app should get the names from the Settings.java.
I have one Activity for calculating, an other activity for set-up things.

This should be 'simulated' here with this example.

Thanks
Marbert
Developer
Developer
 
Posts: 30
Joined: Sat Feb 04, 2012 7:31 pm

Top
Next

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 18 guests