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

  • Home
  • SEARCH
  • 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 8574179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:24:59+00:00 2026-06-11T19:24:59+00:00

This is my code for my android app which isn’t complete package com.example; import

  • 0

This is my code for my android app which isn’t complete

package com.example;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

public class MainActivity extends Activity {
    private EditText text;
    private EditText text2;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text = (EditText) findViewById(R.id.editText1);
        text2=(EditText)findViewById(R.id.result);


    }

    // This method is called at button click because we assigned the name to the
    // "OnClick property" of the button
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.button1:
                RadioButton celsiusButton = (RadioButton) findViewById(R.id.radio0);
                RadioButton fahrenheitButton = (RadioButton) findViewById(R.id.radio1);
                RadioButton KelvinButton=(RadioButton) findViewById(R.id.radio2);
                if (text.getText().length() == 0) {
                    Toast.makeText(this, "Please enter a valid number",
                            Toast.LENGTH_LONG).show();
                    return;
                }

                float inputValue = Float.parseFloat(text.getText().toString());
                if (celsiusButton.isChecked()) {
                    text2.setText(String
                            .valueOf(convertFahrenheitToCelsius(inputValue)));
                    celsiusButton.setChecked(true);
                } else {
                    text2.setText(String
                            .valueOf(convertCelsiusToFahrenheit(inputValue)));
                    fahrenheitButton.setChecked(false);
                    celsiusButton.setChecked(true);
                    if(KelvinButton.isChecked())
                    {
                        text2.setText(String.valueOf(convertoKelvin(inputValue)));
                    }
                }
                break;
            case R.id.button2:
                RadioButton celsiusButton = (RadioButton) findViewById(R.id.radio0);
                RadioButton fahrenheitButton = (RadioButton) findViewById(R.id.radio1);
                RadioButton KelvinButton=(RadioButton) findViewById(R.id.radio2);
                float inputValue = Float.parseFloat(text.getText().toString());

                if (text.getText().length() == 0) {
                    Toast.makeText(this, "Please enter a valid number",
                            Toast.LENGTH_LONG).show();
                    return;
                }

                if (fahrenheitButton.isChecked()) {
                    text2.setText(String
                            .valueOf(convertFahrenheitToCelsius(inputValue)));
                    celsiusButton.setChecked(true);
                } else {
                    text2.setText(String
                            .valueOf(convertCelsiusToFahrenheit(inputValue)));
                    fahrenheitButton.setChecked(true);
                    if(KelvinButton.isChecked())
                    {
                        text2.setText(String.valueOf(convertoKelvin(inputValue)));
                    }
                }
                break;
            case R.id.button3:
                RadioButton celsiusButton = (RadioButton) findViewById(R.id.radio0);
                RadioButton fahrenheitButton = (RadioButton) findViewById(R.id.radio1);
                RadioButton KelvinButton=(RadioButton) findViewById(R.id.radio2);
                float inputValue = Float.parseFloat(text.getText().toString());
                if (text.getText().length() == 0) {
                    Toast.makeText(this, "Please enter a valid number",
                            Toast.LENGTH_LONG).show();
                    return;
                }

                if (KelvinButton.isChecked()) {
                    text2.setText(String
                            .valueOf(convertFahrenheitToCelsius(inputValue)));
                    celsiusButton.setChecked(true);
                } else {
                    text2.setText(String
                            .valueOf(convertCelsiusToFahrenheit(inputValue)));
                    fahrenheitButton.setChecked(false);
                    celsiusButton.setChecked(true);
                    if(KelvinButton.isChecked())
                    {
                        text2.setText(String.valueOf(convertoKelvin(inputValue)));
                    }
                }
                break;

        }
    }

    // Converts to celsius
    private float convertFahrenheitToCelsius(float fahrenheit) {
        return ((fahrenheit - 32) * 5 / 9);
    }

    // Converts to fahrenheit
    private float convertCelsiusToFahrenheit(float celsius) {
        return ((celsius * 9) / 5) + 32;
    }

    private float convertoKelvin(float celsius)
    {
        return ((celsius+273)) ;
    }


}

As you can see i have defined my variables each time in very case.If i don’t do this i get the error “The value might not have been initialised.What am i doing wrong.

If you were wondering,My code isn’t complete.I am suck at this error and will continue once this is fixed

  • 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-11T19:25:00+00:00Added an answer on June 11, 2026 at 7:25 pm

    If I understood your question correctly If you assign values to celsiusButton, fahrenheitButton ,KelvinButtonvariables in only one case then you get error.

    Reason why this happens is scope of the object initialization. Below is the sample which illustrates what is happening.

    public void example() {
        String str;
        switch (1) {
        case 1:
            str = "test";// If I initalize here then there is problem since
                            // scope is limited to only this case
            str.toString();
            break;
    
        default:
            str.toString();// Compilation error here.
            break;
        }
    }
    
    public void example() {
        String str = "test";
        switch (1) {
        case 1:
            str.toString();
            break;
    
        default:
            str.toString();// No error because scope of initialisation is whole
                            // method
            break;
        }
    }
    

    So you need to define your variables before switch (view.getId()) {

    And resultant declaration will look like below

    public void onClick(View view) {
    RadioButton celsiusButton = (RadioButton) findViewById(R.id.radio0);
    RadioButton fahrenheitButton = (RadioButton) findViewById(R.id.radio1);
    RadioButton KelvinButton=(RadioButton) findViewById(R.id.radio2);
    switch (view.getId()) {
        case R.id.button1:
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code: package com.powergroupbd.timer; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.os.CountDownTimer;
This is my MyTasteActivity code: package MyTaste; import android.app.Activity; import android.content.Context; import android.os.Bundle; import
I have some simple code which should create a database. package com.webfoo.android.databaseExample; import android.app.Activity;
i have this code: import android.app.Activity; import android.app.ListActivity; import android.os.Bundle; import android.widget.AdapterView; import android.widget.ArrayAdapter;
I'm working on an Android app which has an activity and a widget. This
When I add this code to my Android 2.1 Java app, it fails: db=SQLiteDatabase.openOrCreateDatabase(Locations,
I'm following this tutorial: http://developer.android.com/training/basics/firstapp/running-app.html#Emulator In it there is a simple code for a
Please, help me. I dont know what is wrong with this code: import android.appwidget.AppWidgetProvider;
I used this code in my Android application: import org.apache.http.client.methods.HttpPost; ... HttpClient httpclient =
I've used this code https://github.com/johannilsson/android-pulltorefresh It all works perfectly apart from one thing, when

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.