Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8842135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:53:21+00:00 2026-06-14T10:53:21+00:00

I’m trying to create an application that figures planetary weight. In the first view

  • 0

I’m trying to create an application that figures planetary weight. In the first view the user inputs their weight and it has an enter button, in the second view is a list of radio buttons and the select button. I want it to multiply their weight by the planet’s force. To do this I needed the weight variable for both activity_main.xml and planets.xml. At first I had it in only planets, but now that it’s in main as well, the if else statements are erroring.

activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
    android:id="@+id/askwtTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="17dp"
    android:layout_marginTop="19dp"
    android:text="@string/askwt" />

<EditText
    android:id="@+id/inputwtEditText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/askwtTextView"
    android:layout_below="@+id/askwtTextView"
    android:layout_marginTop="26dp"
    android:ems="10"
    android:inputType="numberDecimal" />

<Button
    android:id="@+id/enterButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/inputwtEditText"
    android:layout_below="@+id/inputwtEditText"
    android:layout_marginTop="38dp"
    android:onClick="buttonclick"
    android:text="@string/enter" />

</RelativeLayout>

planets.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/planetTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/planet" />

<TextView
    android:id="@+id/textViewform2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="" />

<RadioGroup
    android:id="@+id/radioGroup1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <RadioButton
        android:id="@+id/mercuryRadio"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="@string/mercury" />

    <RadioButton
        android:id="@+id/venusRadio"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/venus" />

    <RadioButton
        android:id="@+id/earthRadio"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/earth" />

    <RadioButton
        android:id="@+id/marsRadio"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="@string/mars" />

    <RadioButton
        android:id="@+id/jupiterRadio"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/jupiter" />

    <RadioButton
        android:id="@+id/saturnRadio"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/saturn" />

    <RadioButton
        android:id="@+id/uranusRadio"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="@string/uranus" />

    <RadioButton
        android:id="@+id/neptuneRadio"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/neptune" />

    <RadioButton
        android:id="@+id/plutoRadio"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/pluto" />
</RadioGroup>


<Button
    android:id="@+id/selectButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="buttonclick2"
    android:text="@string/select" />

<TextView
    android:id="@+id/textViewform2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
     />

</LinearLayout>

JAVA:

package com.deitel.planetaryweight;

import android.os.Bundle;
import android.app.Activity;
import android.widget.*;
import android.view.View;
import android.view.Menu;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.EditText;
import android.widget.TextView;
import java.text.DecimalFormat;

public class MainActivity extends Activity {

//Global variable
double weight;
private Button enter;  // creates a button 


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    enter = (Button) findViewById(R.id.enterButton);

    //Start with first screen
    setContentView(R.layout.activity_main);
}

//buttonclick for form 1
public void buttonclick(View view){



    //creates an editext and assigns the resource id of the xml edittext.
    EditText wtentry = (EditText)findViewById(R.id.inputwtEditText);

    //Receives the input from the edittext, converts it to a double (number).
    weight = Double.parseDouble(wtentry.getText().toString());


      //switch views to screen 2
       setContentView(R.layout.planets);

       //change the value of the textview on screen 2 to the calculation value
       TextView t2 = (TextView)findViewById(R.id.textViewform2);
       t2.setText(Double.toString(weight));

        }

        //buttonclick for form 2!
        public void buttonclick2(View view){
            setContentView(R.layout.planets);

            RadioButton mercury = (RadioButton) findViewById(R.id.mercuryRadio);
            RadioButton venus = (RadioButton) findViewById(R.id.venusRadio);
            RadioButton earth = (RadioButton) findViewById(R.id.earthRadio);            
            RadioButton mars = (RadioButton) findViewById(R.id.marsRadio);
            RadioButton jupiter = (RadioButton) findViewById(R.id.jupiterRadio);
            RadioButton saturn = (RadioButton) findViewById(R.id.saturnRadio);
            RadioButton uranus = (RadioButton) findViewById(R.id.uranusRadio);
            RadioButton neptune = (RadioButton) findViewById(R.id.neptuneRadio);
            RadioButton pluto = (RadioButton) findViewById(R.id.plutoRadio);

            //Makes a variable for the entered amount
            Double mercurypf;
            Double venuspf;
            Double earthpf;
            Double marspf;
            Double jupiterpf;
            Double saturnpf;
            Double uranuspf;
            Double neptunepf;
            Double plutopf;
            Double weight;

            // constants
            final double mercuryforce = 0.38; 
            final double venusforce = 0.91; 
            final double earthforce = 1.00; 
            final double marsforce = 0.38; 
            final double jupiterforce = 2.34; 
            final double saturnforce = 1.06; 
            final double uranusforce = .92;
            final double neptuneforce = 1.19;
            final double plutoforce = 0.06;



            // Code used to determine which planet RadioButton is checked:

            if(mercury.isChecked())
            {
                mercurypf = mercuryforce * weight;
            }
            else
            {
                mercurypf = 0.00;
            }


            if(venus.isChecked())
            {
                venuspf = venusforce * weight;
            }
            else
            {
                venuspf = 0.00;

            }
            if(earth.isChecked())
            {
                earthpf = earthforce * weight;
            }
            else
            {
                earthpf = 0.00;
            }
            if(mars.isChecked())
            {
                marspf = marsforce * weight;
            }
            else
            {
                marspf = 0.00;
            }
            if(jupiter.isChecked())
            {
                jupiterpf =jupiterforce * weight;
            }
            else
            {
                jupiterpf = 0.00;
            }
            if(saturn.isChecked())
            {
                saturnpf = saturnforce * weight;
            }
            else
            {
                saturnpf = 0.00;
            }

            if(uranus.isChecked())
            {
                uranuspf = uranusforce * weight;
            }
            else
            {
                uranuspf = 0.00;
            }

            if(neptune.isChecked())
            {
                neptunepf = neptuneforce * weight;
            }
            else
            {
                neptunepf = 0.00;
            }

            if(pluto.isChecked())
            {
                plutopf = plutoforce * weight;
            }
            else
            {
                plutopf = 0.00;
            }

        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T10:53:22+00:00Added an answer on June 14, 2026 at 10:53 am

    In addition to the rest, there’s some structural and design issues you need to resolve. Take a look at the docs for information on passing information back and forth to and from Activities. setContentView() shouldn’t be called more than once in an Activity unless you have a good reason to.

    public class MainActivity extends Activity {
        /* You should get used to declaring everything with the correct visibility.  Good practice is to make everything private and use public mutator methods */
        //Global variable
        private double weight;
        private Button enter;  // creates a button 
    
        // Views
        private EditText wtEntry;
        private TextView txtForm2;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            //Start with first screen
            setContentView(R.layout.activity_main);
    
            enter = (Button) findViewById(R.id.enterButton);
    
            //creates an editext and assigns the resource id of the xml edittext.
            wtEntry = (EditText)findViewById(R.id.inputwtEditText);
            txtForm2 = (TextView)findViewById(R.id.textViewform2);
        }
        // Button clicks shouldn't do anything but perform clicky actions. Leave field initialization, view creation, etc to the Activity.
        //buttonclick for form 1
        public void buttonclick(View view){
            //Receives the input from the edittext, converts it to a double (number).
            weight = Double.parseDouble(wtEntry.getText().toString());
    
            //change the value of the textview on screen 2 to the calculation value
           t2.setText(Double.toString(weight));
    
           // If you want a new layout, it's best to start a new activity.
           // It looks like you want to get information back, so use startActivityForResult().
           // setContentView(R.layout.planets);
           Intent dataIntent = new Intent(this, PlanetChooser.class);
           dataIntent.putExtra("com.yourpackage.identifier.DATA_WEIGHT", weight);
           startActivityForResult(dataIntent, Activity.RESULT_OK);
        }
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
    
            // Check that the resultCode is the same as we started the activity with
            if(resultCode == Activity.RESULT_OK){
                // get the double from the Intent, using the same string name (package prefixed)
                // or a default value if it didn't get set.
                double resultWeight = data.getDoubleExtra("com.yourpackage.identifier.RESULT_WEIGHT", 0.0);
    
                // Now do something with resultWeight
            }
        }
    }
    // PlanetChooser.class
    public class PlanetChooser extends Activity {
        // constants, usually denoted by uppercase and declared static and final
        public static final double MERCURYFORCE = 0.38; 
        public static final double VENUSFORCE = 0.91; 
        public static final double EARTHFORCE = 1.00; 
        public static final double MARSFORCE = 0.38; 
        public static final double JUPITERFORCE = 2.34; 
        public static final double SATURNFORCE = 1.06; 
        public static final double URANUSFORCE = 0.92;
        public static final double NEPTUNEFORCE = 1.19;
        public static final double PLUTOFORCE = 0.06;
    
        private RadioButton mercury, venus, earth, mars, jupiter, saturn, uranus, neptune, pluto;
    
        // No need to use the Double object as opposed to the primitive unless you have good reason
        private double mercurypf, venuspf, earthpf, marspf, jupiterpf, saturnpf, uranuspf, neptunepf, plutopf, weight;
    
        // One variable will suffice, it seems.
        private double resultForce;
    
        public void onCreate(Bundle s){
            super.onCreate(s);
            setContentView(R.layout.planets);
    
            mercury = (RadioButton) findViewById(R.id.mercuryRadio);
            venus = (RadioButton) findViewById(R.id.venusRadio);
            earth = (RadioButton) findViewById(R.id.earthRadio);            
            mars = (RadioButton) findViewById(R.id.marsRadio);
            jupiter = (RadioButton) findViewById(R.id.jupiterRadio);
            saturn = (RadioButton) findViewById(R.id.saturnRadio);
            uranus = (RadioButton) findViewById(R.id.uranusRadio);
            neptune = (RadioButton) findViewById(R.id.neptuneRadio);
            pluto = (RadioButton) findViewById(R.id.plutoRadio);
        }
        public void buttonclick2(View view){
            /*
            It looks to me here you're looking to see which box is checked, and set a value based on 
            that planet.  Since instance variables (in this case mercurypf, jupiterpf, etc) are initialized
            to the default value (0), there's no need to set them manually.
            */
    
            // Code used to determine which planet RadioButton is checked:
            if(mercury.isChecked())    {
                resultForce = MERCURYFORCE * weight;
            }
            if(venus.isChecked()){
                resultForce = VENUSFORCE * weight;
            }
            if(earth.isChecked()){
                resultForce = EARTHFORCE * weight;
            }
            if(mars.isChecked()){
                resultForce = MARSFORCE * weight;
            }
            if(jupiter.isChecked()){
                resultForce =JUPITERFORCE * weight;
            }
            if(saturn.isChecked()){
                resultForce = SATURNFORCE * weight;
            }
            if(uranus.isChecked()){
                resultForce = URANUSFORCE * weight;
            }
            if(neptune.isChecked()){
                resultForce = NEPTUNEFORCE * weight;
            }
            if(pluto.isChecked()){
                resultForce = PLUTOFORCE * weight;
            }
            // Create a new data Intent to pass back to the calling activity, set the result code, 
            // and manually finish() this activity.
            Intent dataIntent = new Intent(this);
            dataIntent.putDoubleExtra("com.yourpackage.identifier.RESULT_DATA", resultForce);
            setResult(Activity.RESULT_OK, dataIntent);
            finish();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.