• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

2 screens, one activity  RSS feed

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can I use 2 screens with one activity in android studio?

I want to add a main screen which asks the user questions and then saves their input into variables. (I can do this part).
I then want the user to click the "Submit" button on the main screen which then saves the input into variables (I can do this part too)

Then when the user clicks the button i want to kill the main screen and go on to the second screen which the user will use 99% of the time.

I tried added a second xml file in my layouts folder and I set the content view in the onCreate method to the main screen but when I launch my app it opens for about a second and then closes down immediately.
Why is this happening?
 
Saloon Keeper
Posts: 5048
135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please get in the habit of posting Android questions in Android forum, not any of the Java forums. We have moved your various posts to that forum, but that should really not be necessary :-)

closes down immediately


That generally means there was an unhandled exception, and the full stack trace of the exception -including the line number of the offending statement- will be written to the logcat. That should be your first point of debugging. Post the full stack trace here, along with relevant excerpts of the source code, and indicate which line of code caused the exception.

How can I use 2 screens with one activity


The ViewFlipper component is an easy way to have more than one screen in an activity.
 
Ranch Hand
Posts: 151
1
Android Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jacques van Tonder wrote:  .....but when I launch my app it opens for about a second and then closes down immediately.
Why is this happening?


Learn to read stacktrace.It would help us to help you if you provide more info.
I guese you miss to add second activity in manifest ?
 
Jacques van Tonder
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help,

ill be sure to post my findings here and sorry about posting in the wrong forum.

@Randy Tong, I dont want a second activity, just a second screen.
 
Ranch Hand
Posts: 31
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jacques van Tonder wrote:

@Randy Tong, I dont want a second activity, just a second screen.



Activities are just that, you could call them screens or windows but in android terminology they are called activities. So, you want the first activity to call the second one using an Intent.
 
Jacques van Tonder
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That would work, so i am going to have to activities, each with their own xml file?

That will work and I considered it already but i then need to somehow pass information from one activity to the other.

In the main activity i am going to store user input into variables.
Then kill the activity but those variables containing the user input must be available in my second activity.
 
Adrian Grabowski
Ranch Hand
Posts: 31
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jacques van Tonder wrote:That would work, so i am going to have to activities, each with their own xml file?

That will work and I considered it already but i then need to somehow pass information from one activity to the other.



Yes, first create two separate activities, each with its own xml file, in Android Studio just use "new Activity" to create it so it will create both java and xml files for you.

this is how you switch between activities: starting activity

So basically you create a new Intent and pass the name of the second activity so it knows which activity you want

Now, to pass data you use putExtra() method, in the above example they just send a simple String but it can be anything that implements Serializable (or Parcelable)
 
Jacques van Tonder
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good lord!,

for a noob like me this is quite overwhelming.
Let me see if I can implement this.

Thanks for all the help everyone.
Will keep you posted.
 
Tim Moores
Saloon Keeper
Posts: 5048
135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Activities are just that, you could call them screens or windows but in android terminology they are called activities.


I beg to differ: It is not at all unusual to use more than one layout per activity. That's precisely what components like ViewFlipper and ViewPager are there for. No second activity and no Intent are necessary in that case.
 
Jacques van Tonder
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok so here's what i did:

On one activity I stored user input into a string variable.
I then converted that variable to an int.
Then I passed the variable to my other activity.

On my other activity i retrieved the variable and assigned it to the correct variable which is present in the rest of my code.

But it keeps saying that the variable i want to assign it to is an int(which it is) and the variable from the other activity is a string even thought I converted that variable to an int before passing it.

Activity from which I pass the variable:



Activity in which I retrieve the variable:



Please help!




 
Tim Moores
Saloon Keeper
Posts: 5048
135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, but you're calling "getString".
 
Jacques van Tonder
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Managed to sort it out by parsing the variable to int while assigning it to another int variable.

No i have another problem.

When i click the button which is supposed to launch the second activity my app closes down.
In the code above the activity which launches first is the first part of the code.

Then when the button is clicked it is supposed to save the input from one of the text boxes and pass that info to the new activity when i click the button.

The second part of code is the activity which is launched and which receives the info from the other activity and stores it in a variable upon the button click
 
Randy Tong
Ranch Hand
Posts: 151
1
Android Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

When i click the button which is supposed to launch the second activity my app closes down.


Again, please post stacktrace to help us to help you.
 
Jacques van Tonder
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does this help?

This is the errors i get when the app crashes.

09-21 10:22:21.379 23540-23540/? E/AndroidRuntime: FATAL EXCEPTION: main
   Process: com.floatr.cypher, PID: 23540
   java.lang.RuntimeException: Unable to start activity ComponentInfo{com.floatr.cypher/com.floatr.cypher.MainActivity}: java.lang.NumberFormatException: s == null
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3194)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3302)
       at android.app.ActivityThread.-wrap12(Unknown Source:0)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1891)
       at android.os.Handler.dispatchMessage(Handler.java:108)
       at android.os.Looper.loop(Looper.java:166)
       at android.app.ActivityThread.main(ActivityThread.java:7425)
       at java.lang.reflect.Method.invoke(Native Method)
       at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)
    Caused by: java.lang.NumberFormatException: s == null
       at java.lang.Integer.parseInt(Integer.java:570)
       at java.lang.Integer.parseInt(Integer.java:643)
       at com.floatr.cypher.MainActivity.onCreate(MainActivity.java:72)
       at android.app.Activity.performCreate(Activity.java:7372)
       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1218)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3147)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3302) 
       at android.app.ActivityThread.-wrap12(Unknown Source:0) 
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1891) 
       at android.os.Handler.dispatchMessage(Handler.java:108) 
       at android.os.Looper.loop(Looper.java:166) 
       at android.app.ActivityThread.main(ActivityThread.java:7425) 
       at java.lang.reflect.Method.invoke(Native Method) 
       at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245) 
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921) 
 
Tim Moores
Saloon Keeper
Posts: 5048
135
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the important part:

Caused by: java.lang.NumberFormatException: s == null
       at java.lang.Integer.parseInt(Integer.java:570)
       at java.lang.Integer.parseInt(Integer.java:643)
       at com.floatr.cypher.MainActivity.onCreate(MainActivity.java:72)



The line number and class name don't match the code you posted earlier, so we can't pinpoint the issue. But it's in line 72 of your MainActivity class. The bottom line is: don't pass null arguments to Integer.parseInt - check the values before using them.
 
Randy Tong
Ranch Hand
Posts: 151
1
Android Java Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In first activity, you assigned R1input to keyBut in second activity, you retrieved R1input using R1input
This is wrong ! Make sure the key is matched with first activity
Also, it should be extras.getInt("key"), not getString. Otherwise you will get null value.
 
Jacques van Tonder
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay thanks, so now i know what is causing the issue but how can I assign R1inputValue to totals1 with parsing R1inputValue to int as it is retrieved from as a string from the other activity and I dont know why because i parsed it to int before passing it on to my main activity.
 
Jacques van Tonder
Greenhorn
Posts: 29
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh wow, that solved it.

Thank you so much guys, especially Tim Moores and Randy Tong.

It now works as expected!

I apologize if i ask stupid questions but i am still learning and you guys have taught me a lot over the last coupe of posts.
Thanks for bearing with me, most other forums like stackOverflow would've told me to get lost a long time ago.

You guys are awesome!
 
Randy Tong
Ranch Hand
Posts: 151
1
Android Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jacques van Tonder wrote:...how can I assign R1inputValue to totals1 with parsing R1inputValue to int as it is retrieved from as a string from the other activity....


The answer is in last line of my previous answer.
 
Jacques van Tonder
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone please explain to me what is wrong here?
app closes down again when clicking button

Logcat says it has an issue on line 46 but i dont see the issue:

09-21 13:41:34.433 7010-7010/com.floatr.cypher E/AndroidRuntime: FATAL EXCEPTION: main
   Process: com.floatr.cypher, PID: 7010
   java.lang.NumberFormatException: For input string: ""
       at java.lang.Integer.parseInt(Integer.java:620)
       at java.lang.Integer.parseInt(Integer.java:643)
       at com.floatr.cypher.MainActivity2$1.onClick(MainActivity2.java:44)
       at android.view.View.performClick(View.java:6291)
       at android.view.View$PerformClick.run(View.java:24931)
       at android.os.Handler.handleCallback(Handler.java:808)
       at android.os.Handler.dispatchMessage(Handler.java:101)
       at android.os.Looper.loop(Looper.java:166)
       at android.app.ActivityThread.main(ActivityThread.java:7425)
       at java.lang.reflect.Method.invoke(Native Method)
       at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)
09-21 13:41:34.446 7010-7010/com.floatr.cypher I/Process: Sending signal. PID: 7010 SIG: 9

My code:



 
Jacques van Tonder
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Logcat has an issue on line 44 nt 46
 
Tim Moores
Saloon Keeper
Posts: 5048
135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Moores wrote:The bottom line is: don't pass null arguments to Integer.parseInt - check the values before using them.


Or in case of "java.lang.NumberFormatException: For input string: "" " : don't pass empty strings to Integer.parseInt.
 
Jacques van Tonder
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hahahahaha I am such a noob!

I presses the button without entering anything into the EditText boxes!

You must be laughing so hard at me right now. lol.

Thanks for the help.
 
Jacques van Tonder
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can I post a video here or somewhere so you guys can see what you helped build?
 
Tim Moores
Saloon Keeper
Posts: 5048
135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not here. But you can embed YouTube and Vimeo videos in posts.
 
Jacques van Tonder
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how about i upload it to my Google drive and post the link here?
 
Jacques van Tonder
Greenhorn
Posts: 29
 
Jacques van Tonder
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The UI still needs some work but the app works.

Any suggestions or potential bugs that you think might occur?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!