• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps New to Android and don't know why I'm getting an error

I am trying to just create a basic program to add up money and return the total amount. For some reason I keep getting an error towards the beginning at setContentView(R.layout.main); under the R which then has something to do with the rest of the errors in the program i believe. I have tried "cleaning" the file which is what I have found some forums saying to do but that does not do anything. I am sure its going to be something really stupid and simple to fix but like I said this is my first program hehe. Thanks for the any help.

Here is my code and if anyone could look at it and give me some ideas on how to fix it and make it run correctly.

package com.utilities.moneycounter;



//import android.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.widget.EditText;
//import com.utilities.moneycounter.R;

public class moneycounter extends Activity {

public double penny;
public EditText nickel;
public EditText dime;
public EditText quarter;
public EditText dollar;
public EditText dollar5;
public EditText dollarmisc;
public EditText amount;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

EditText penny = (EditText) findViewById(R.id.penny);
String stringnumber = penny.getText().toString();
double pennyy = Double.parseDouble(stringnumber);
//penny = (double) findViewById(R.id.text1);
nickel = (EditText) findViewById(R.id.textView3);
dime = (EditText) findViewById(R.id.textView4);
quarter = (EditText) findViewById(R.id.textView5);
dollar = (EditText) findViewById(R.id.textView9);
dollar5 = (EditText) findViewById(R.id.textView10);
dollarmisc = (EditText) findViewById(R.id.textView11);
amount = (EditText) findViewById(R.id.textView12);

penny.setOnFocusChangeListener(new OnFocusChangeListener(){
public void OnFocusChange(View v, boolean hasFocus){
total();
}

private void total() {
// TODO Auto-generated method stub
double result = new Double(penny.getText().toString())/100
+ new Double(nickel.getText().toString())/20
+ new Double(dime.getText().toString())/10
+ new Double(quarter.getText().toString())/4
+ new Double(dollar.getText().toString())
+ new Double(dollar5.getText().toString())*5
+ new Double(dollarmisc.getText().toString());
amount.setText(Double.toString(result));
}

@Override
public void onFocusChange(View arg0, boolean arg1) {
// TODO Auto-generated method stub

}
});

}


}
 
- Do you have any errors in XML files? Check your error console, which should tell you if you do.
- Try refreshing the project by Right-Clicking it and select "Refresh"
 
Your problem lies in your declaring variables. Look at "public double penny;" it should read "public EditText penny;" You then convert, after the user inputs a value and is optained, the String from EditText to a double with "double penny = Double.valueOf(penny.getText().toString());"

You will need to to this for each value obtained.
 
Your problem lies in your declaring variables. Look at "public double penny;" it should read "public EditText penny;"
That first line where I wrote public double penny that was just when I was trying to figure out possibly the correct way to write it. The whole block
setContentView(R.layout.main);

EditText penny = (EditText) findViewById(R.id.penny);
String stringnumber = penny.getText().toString();
double pennyy = Double.parseDouble(stringnumber);
//penny = (double) findViewById(R.id.text1);
nickel = (EditText) findViewById(R.id.textView3);
dime = (EditText) findViewById(R.id.textView4);
quarter = (EditText) findViewById(R.id.textView5);
dollar = (EditText) findViewById(R.id.textView9);
dollar5 = (EditText) findViewById(R.id.textView10);
dollarmisc = (EditText) findViewById(R.id.textView11);
amount = (EditText) findViewById(R.id.textView12);
has an error saying that "main cannot or is not a field" for the first line and "id cannot be resolved or is not a field" for the rest. Thanks for any other ideas you may have.
 
will, would you be able to send me a zip of your project file? It would be much easier for me to help you with where you're going wrong if I can see it all, or maybe also post your .xml's too
 
Here is my main.xml file

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

android:layout_width="match_parent"
android:id="@+id/RelativeLayout1"
android:layout_height="50dip">
<ImageView
android:id="@+id/penny"
android:src="@drawable/ic_penny"
android:layout_height="match_parent"
android:layout_width="65dip"
android:layout_alignParentTop="true">
</ImageView>
<TextView
android:id="@+id/textView2"
android:text="TextView"
android:layout_width="wrap_content"
android:editable="true"
android:layout_alignParentRight="true"
android:layout_height="match_parent">
</TextView>
</RelativeLayout>
<RelativeLayout //nickel
android:layout_width="match_parent"
android:id="@+id/RelativeLayout2"
android:layout_below="@+id/RelativeLayout1"
android:layout_height="50dip">
<TextView
android:text="TextView"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:id="@+id/textView3"
android:editable="true"
android:layout_alignParentRight="true">
</TextView>
<ImageView
android:layout_height="wrap_content"
android:layout_width="65dip"
android:id="@+id/nickel"
android:src="@drawable/ic_nickel"
android:layout_alignParentTop="true">
</ImageView>
</RelativeLayout>
<RelativeLayout //dime
android:layout_width="match_parent"
android:id="@+id/RelativeLayout3"
android:layout_below="@+id/RelativeLayout2"
android:layout_height="50dip">
<TextView
android:text="TextView"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/textView4"
android:editable="true"
android:layout_alignParentRight="true">
</TextView>
<ImageView
android:layout_height="wrap_content"
android:layout_width="65dip"
android:id="@+id/imageView1"
android:src="@drawable/ic_dime"
android:layout_alignParentTop="true">
</ImageView>
</RelativeLayout>
<RelativeLayout //quarter
android:layout_width="match_parent"
android:id="@+id/RelativeLayout4"
android:layout_below="@+id/RelativeLayout3"
android:layout_height="50dip">
<ImageView
android:layout_width="65dip"
android:layout_height="48dip"
android:id="@+id/quarter"
android:src="@drawable/ic_quarter">
</ImageView>
<TextView
android:text="TextView"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/textView5"
android:editable="true"
android:layout_alignParentRight="true">
</TextView>
</RelativeLayout>
<RelativeLayout //dollar
android:layout_width="match_parent"
android:id="@+id/RelativeLayout5"
android:layout_below="@+id/RelativeLayout4"
android:layout_height="50dip">
<TextView
android:gravity="center_horizontal"
android:textSize="35dip"
android:text="1$"
android:layout_height="48dip"
android:layout_width="65dip"
android:id="@+id/dollar"
android:editable="false">
</TextView>
<TextView
android:text="TextView"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/textView9"
android:editable="true"
android:layout_alignParentRight="true">
</TextView>
</RelativeLayout>
<RelativeLayout //dollar5
android:layout_height="wrap_content"
android:id="@+id/RelativeLayout6"
android:layout_below="@+id/RelativeLayout5"
android:layout_width="match_parent">
<TextView
android:text="5$"
android:gravity="center_horizontal"
android:id="@+id/dollar5"
android:layout_width="65dip"
android:layout_height="48dip"
android:textSize="35dip"
android:editable="false">
</TextView>
<TextView
android:text="TextView"
android:id="@+id/textView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:editable="true"
android:layout_alignParentRight="true">
</TextView>
</RelativeLayout>
<RelativeLayout //dollarmisc
android:layout_height="wrap_content"
android:id="@+id/RelativeLayout7"
android:layout_below="@+id/RelativeLayout6"
android:layout_width="match_parent">
<TextView
android:text="Misc"
android:gravity="center_horizontal"
android:id="@+id/dollarmisc"
android:layout_width="65dip"
android:layout_height="48dip"
android:textSize="30dip"
android:editable="false">
</TextView>
<TextView
android:editable="true"
android:text="TextView"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/textView11"
android:gravity="center_vertical"
android:layout_alignParentRight="true">
</TextView>
</RelativeLayout>
<RelativeLayout //total
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="@+id/relativeLayout8"
android:layout_below="@+id/RelativeLayout7">
<TextView
android:text="Total"
android:id="@+id/textView1"
android:layout_alignParentLeft="true"
android:layout_height="fill_parent"
android:layout_width="wrap_content"
android:textSize="35dip">
</TextView>
<TextView
android:text="0.00"
android:id="@+id/textView12"
android:layout_alignParentRight="true"
android:layout_width="150dip"
android:layout_height="fill_parent"
android:textSize="35dip"
android:background="#bebebe"
android:textColor="#aa000000"
android:gravity="right"
android:digits=".0123456789">
</TextView>
</RelativeLayout>

</RelativeLayout>




Here is my whole java file (the lines with a *** in the front are the ones with errors that say they cannot be resolved or are not a field)

package com.program.changecounter;


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.widget.EditText;
import com.program.changecounter.R;

public class changecounter extends Activity {
EditText penny;

public EditText nickel;
public EditText dime;
public EditText quarter;
public EditText dollar;
public EditText dollar5;
public EditText dollarmisc;
public EditText amount;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

*** setContentView(R.layout.main);

*** penny = (EditText)findViewById(R.id.editText1);
*** nickel = (EditText) findViewById(R.id.textView3);
*** dime = (EditText) findViewById(R.id.textView4);
*** quarter = (EditText) findViewById(R.id.textView5);
*** dollar = (EditText) findViewById(R.id.textView9);
*** dollar5 = (EditText) findViewById(R.id.textView10);
*** dollarmisc = (EditText) findViewById(R.id.textView11);
*** amount = (EditText) findViewById(R.id.textView12);

penny.setOnFocusChangeListener(new OnFocusChangeListener(){
public void OnFocusChange(View v, boolean hasFocus){
total();
}

private void total() {
// TODO Auto-generated method stub
double result = new Double(penny.getText().toString())/100
+ new Double(nickel.getText().toString())/20
+ new Double(dime.getText().toString())/10
+ new Double(quarter.getText().toString())/4
+ new Double(dollar.getText().toString())
+ new Double(dollar5.getText().toString())*5
+ new Double(dollarmisc.getText().toString());
amount.setText(Double.toString(result));
}

@Override
public void onFocusChange(View arg0, boolean arg1) {
// TODO Auto-generated method stub

}
});

}


}
 
you can't call penny = (EditText) findViewById(R.id.TextView)

you have to change in your xml, these textviews to edit texts (which are the input boxes, textview just shows text)

changing it to findViewById(R.id.EditText01)
 
Back
Top Bottom