Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Android Simple Converter Program

I have a program nearing completion but I cant figure out how to implemnet the code for the onclick method. everytime I uncomment the sections that are comment the program gives an error and closes

Below is the code

package edu.witc.temperatureconvertermenu;


import android.R.*;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

    private static final String TEMPF = "TEMPF";
    private static final String TEMPC = "TEMPC";

    private double enterTempF;
    private double enterTempC;
    private EditText fahrenheitEditText;
    private EditText celsiusEditText;
    private EditText convertedTempC;
    private EditText convertedTempF;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home);
        if ( savedInstanceState == null )
        {
            enterTempF = 0.0;
            enterTempC = 0.0;
        }
        else
        {
            enterTempF = savedInstanceState.getDouble(TEMPF);
            enterTempC = savedInstanceState.getDouble(TEMPC);
        }
        fahrenheitEditText = (EditText) findViewById(R.id.fInput);
        celsiusEditText = (EditText) findViewById(R.id.cInput);
        convertedTempC = (EditText) findViewById(R.id.cOutput);
        convertedTempF = (EditText) findViewById(R.id.fOutput);
        Button convertCelsiusButton = (Button) findViewById(R.id.convertToCelsius);
        //convertCelsiusButton.setOnClickListener(convertCelsiusButtonListener);
        Button convertFahrenheitButton = (Button) findViewById(R.id.convertToCelsius);
        //convertFahrenheitButton.setOnClickListener(convertFahrenheitButtonListener);



    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    protected void onSaveInstanceState(Bundle outState)
    {
        super.onSaveInstanceState(outState);

        outState.putDouble(TEMPF, enterTempF);
        outState.putDouble(TEMPC, enterTempC);
    }

//  public OnClickListener convertCelsiusButtonListener = new OnClickListener()
//  {
//      @Override
//      public void onClick(View v)
//      {
//          try
//          {
//              enterTempC = Double.parseDouble(celsiusEditText.getText().toString());
//          }
//          catch (NumberFormatException e)
//          {
//              enterTempC = 9999;
//          }
//          
//          if ( enterTempC == 9999)
    //convertedTempF.setText("none");
//else
//{
//  double newTempF = (9 * enterTempC / 5 ) + 32;
//  convertedTempF.setText(String.format("%.1f", newTempF));
//}
//          
//      
//      }
//  };
//  
//  public OnClickListener convertFahrenheitButtonListener = new OnClickListener()
//  {
//      @Override
//      public void onClick(View v)
//      {
//          try
//          {
//              enterTempF = Double.parseDouble(fahrenheitEditText.getText().toString());
//          }
//          catch (NumberFormatException e)
//          {
//              enterTempF = 9999;
//          }
//          
    //if ( enterTempF == 9999)
        //convertedTempC.setText("none");
    //else
    //{
    //  double newTempC = 5 * ( enterTempF - 32 ) / 9;
    //  convertedTempC.setText(String.format("%.1f", newTempC));
    //}
//      
//          
//      }
//  };


    public void Home(MenuItem item){
        setContentView(R.layout.home);
    }

    public void About(MenuItem item){
        setContentView(R.layout.about);
    }

    public void Quit(MenuItem item){
        Toast.makeText(MainActivity.this, "Thank You For Using Temperature Converter!!!", Toast.LENGTH_SHORT).show();
        finish();
    }

    public void ConvertFahrenheit(MenuItem item){
        setContentView(R.layout.f_conversion);


    }

    public void ConvertCelsius(MenuItem item){

        setContentView(R.layout.c_conversion);


    }

}

If needed I can upload this via google drive

1 Answer

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

Hmmm...looks okay...if you want, zip up your project directory and email us at help@teamtreehouse.com. Ask that it be forwarded to me and I can take a look at your whole project.