Results 1 to 2 of 2
  1. #1
    Join Date
    Jun 2013
    Posts
    7
    Rep Power
    0

    Post -- Temperature Conversion code, please help :-(

    For my homework this week I was assigned to make a Temperature Conversion App for android, the layout is not important but my code has to work, I have my code in xml and in java but I don't understand the error that I have in xml, it's not giving me any specific's as to what the error is, please help
    the error is as follows: error:Error:String types not allowed(at 'id' with value 'button1').
    Here's the xml code:
    Java Code:
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/button01"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".MainActivity" >
    
        <RadioButton
            android:id="@+id/fahrenheitButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/celsiusButton"
            android:layout_below="@+id/celsiusButton"
            android:layout_marginTop="30dp"
            android:text="Fahrenheit" />
    
        <Button
            android:id="@+id/button01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/celsiusButton"
            android:layout_alignParentBottom="false"
            android:layout_alignParentLeft="false"
            android:layout_alignParentRight="false"
            android:layout_alignParentTop="false"
            android:layout_alignWithParentIfMissing="false"
            android:layout_below="@+id/fahrenheitButton"
            android:layout_centerHorizontal="false"
            android:layout_centerInParent="false"
            android:layout_centerVertical="false"
            android:layout_marginTop="30dp"
            android:text="Enter" />
    
        <EditText
            android:id="@+id/textview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/celsiusButton"
            android:layout_alignParentTop="true"
            android:layout_marginTop="28dp"
            android:ems="10"
            android:inputType="number" >
    
            <requestFocus />
        </EditText>
    
        <RadioButton
            android:id="@+id/celsiusButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/textview"
            android:layout_marginLeft="35dp"
            android:layout_marginTop="19dp"
            android:text="Celsius" />
    
    </RelativeLayout>

    this the code I have in Java:
    Java Code:
    package com.example.w7h2;
    
    import android.R;
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    //import android.view.TextView;
    //import android.widget.EditText;
    import android.widget.RadioButton;
    import android.widget.TextView;
    import android.widget.Toast;
    
    public class MainActivity extends Activity {
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_list_item);
    	}	
    	@SuppressWarnings("null")
    	public void myClickHandler(TextView v){
    		TextView textView = null;
    		TextView view = null;
    		switch (view.getId()){
    		case R.id.button1:
    			RadioButton celsiusButton = (RadioButton) findViewById(R.id.button1);
    			RadioButton fahrenheitButton =  (RadioButton) findViewById(R.id.button2);
    			if (textView.getText().length()== 0) {
    				Toast.makeText(
    						this,
    						"Please enter a valid number", Toast.LENGTH_LONG) .show();
    				return;
    			}			
    			float inputValue = Float.parseFloat(textView.getText().toString());
    			if (celsiusButton.isChecked()) {
    				textView.setText(String
    						.valueOf(convertCelsiusToFahrenheit(inputValue)));
    			} else {
    				textView.setText(String
    						.valueOf(convertCelsiusToFahrenheit(inputValue)));
    			}
    			//Switch to the other button
    			if (fahrenheitButton.isChecked()){
    				fahrenheitButton.setChecked(false);
    				celsiusButton.setChecked(true);
    			} else {
    				fahrenheitButton.setChecked(true);
    				celsiusButton.setChecked(false);
    			}
    			break;
    		}
    	}
    	
    	private float convertFahrenheitToCelcius(float fahrenheit) {
    		return ((fahrenheit - 32) * 5 / 9);
    	}
    	
    
    	private float convertCelsiusToFahrenheit(float celsius) {
    		return ((celsius * 9) / 5) * 32;
    	}
    
    	@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;
    	}
    
    }
    Last edited by nu2programming; 09-02-2013 at 06:07 AM.

  2. #2
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: -- Temperature Conversion code, please help :-(

    There is nothing with the id 'button1' in your XML. However, there are two things with the id 'button01'... one of which is not even a button.
    Get in the habit of using standard Java naming conventions!

Similar Threads

  1. Temperature Conversion coding issue
    By snobbysteven in forum New To Java
    Replies: 9
    Last Post: 03-13-2013, 05:08 PM
  2. Replies: 17
    Last Post: 09-15-2012, 03:50 AM
  3. Replies: 1
    Last Post: 08-30-2012, 12:07 AM
  4. Need help modifying code for hex conversion
    By gamer765 in forum New To Java
    Replies: 14
    Last Post: 10-25-2010, 06:59 AM
  5. image to byte code file conversion
    By nupurashi in forum Java Tip
    Replies: 0
    Last Post: 01-28-2009, 10:25 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •