My program crashes when the button is clicked.. why?

Go To StackoverFlow.com

-2

LOGCAT

04-03 20:59:46.189: E/AndroidRuntime(362): android.content.res.Resources$NotFoundException: String resource ID #0x0 
04-03 20:59:46.189: E/AndroidRuntime(362): at android.content.res.Resources.getText(Resources.java:201) 
04-03 20:59:46.189: E/AndroidRuntime(362): at android.widget.TextView.setText(TextView.java:2857) 
04-03 20:59:46.189: E/AndroidRuntime(362): at coin.calc.wilson.CoinCalculatorActivity$1.onClick(CoinCalculatorActivity.java:65) 
04-03 20:59:46.189: E/AndroidRuntime(362): at android.view.View.performClick(View.java:2485) 
04-03 20:59:46.189: E/AndroidRuntime(362): at android.view.View$PerformClick.run(View.java:9080) 
04-03 20:59:46.189: E/AndroidRuntime(362): at android.os.Handler.handleCallback(Handler.java:587) 
04-03 20:59:46.189: E/AndroidRuntime(362): at android.os.Handler.dispatchMessage(Handler.java:92) 
04-03 20:59:46.189: E/AndroidRuntime(362): at android.os.Looper.loop(Looper.java:123) 
04-03 20:59:46.189: E/AndroidRuntime(362): at android.app.ActivityThread.main(ActivityThread.java:3683) 
04-03 20:59:46.189: E/AndroidRuntime(362): at java.lang.reflect.Method.invokeNative(Native Method) 
04-03 20:59:46.189: E/AndroidRuntime(362): at java.lang.reflect.Method.invoke(Method.java:507) 
04-03 20:59:46.189: E/AndroidRuntime(362): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
04-03 20:59:46.189: E/AndroidRuntime(362): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
04-03 20:59:46.189: E/AndroidRuntime(362): at dalvik.system.NativeStart.main(Native Method) 
04-03 20:59:49.720: I/Process(362): Sending signal. PID: 362 SIG: 9

Here is my java code:

package coin.calc.wilson;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

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

    Button solve = (Button)findViewById(R.id.bsolve);
    solve.setOnClickListener(new OnClickListener() {

        public void onClick(View v) { 
    int td, tc, twenty, ten, five, one, quarter, dime, nickel, penny;
    EditText tdet = (EditText)findViewById(R.id.etdollars);
    EditText tcet = (EditText)findViewById(R.id.etcents);
        try{
            td = Integer.parseInt(tdet.getText().toString());
            tc = Integer.parseInt(tdet.getText().toString());
        }
        catch (NumberFormatException e) {
            td = tc = 0;
        }

    if (td < 0 || tc < 0){
        /* ERROR */
    }
    else{
        if( td == 0 ){
            twenty = ten = five = one = 0;
        }
        else{
            one = td%5;

            if(td >= 20){
                twenty = (td/20)-((td%20)/20);
            }
            else{
                twenty = 0;
            }

            int tda20 = td-(20*twenty);
            if (tda20 >= 10){
                ten = (tda20/10)-((tda20%10)/10);
            }
            else{
                ten = 0;
            }
            int tda10 = tda20-(10*ten);
            if(tda10>=5){
                five = (tda10/5)-((tda10%5)/5);
            }
            else{
                five = 0;
            }
            TextView tv20 = (TextView)findViewById(R.id.tvtwenty);
            tv20.setText(twenty);
            TextView tv10 = (TextView)findViewById(R.id.tvten);
            tv10.setText(ten);
            TextView tv5 = (TextView)findViewById(R.id.tvfive);
            tv5.setText(five);
            TextView tv1 = (TextView)findViewById(R.id.tvone);
            tv1.setText(one);

            }


        if( tc == 0 ){
            quarter = dime = nickel = penny = 0;
        }
        else{
            penny = tc%5;

            if(tc >= 25){
                quarter = (tc/25)-((td%25)/25);
            }
            else{
                quarter = 0;
            }

            int tcaq = tc-(25*quarter);
            if (tcaq >= 10){
                dime = (tcaq/10)-((tcaq%10)/10);
            }
            else{
                dime = 0;
            }
            int tcad = tcaq-(10*ten);
            if(tcad>=5){
                nickel = (tcad/5)-((tcad%5)/5);
            }
            else{
                nickel = 0;
            }
            TextView tvq = (TextView)findViewById(R.id.tvquarter);
            tvq.setText(quarter);
            TextView tvd = (TextView)findViewById(R.id.tvdime);
            tvd.setText(dime);
            TextView tvn = (TextView)findViewById(R.id.tvnickel);
            tvn.setText(nickel);
            TextView tvp = (TextView)findViewById(R.id.tvpenny);
            tvp.setText(penny);

            }
}
}});}
}

and here is the xml:

<TableRow
    android:id="@+id/tableRow1"
    android:layout_height="wrap_content"
    android:gravity="center" >

    <EditText
        android:id="@+id/etdollars"
        android:layout_width="156dp"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="number" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="dollars"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</TableRow>

<TableRow
    android:id="@+id/tableRow2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center" >

    <EditText
        android:id="@+id/etcents"
        android:layout_width="156dp"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="number" />
     <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="cents"
        android:textAppearance="?android:attr/textAppearanceLarge" />
</TableRow>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="60dp"
    android:gravity="center" >

    <Button
        android:id="@+id/bsolve"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Solve!" />

</LinearLayout>


<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="80dp" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="100dp"
        android:layout_height="80dp"
        android:layout_weight="0.00"
        android:layout_marginLeft="8.75dp"
        android:layout_marginRight="8.75dp"
        android:src="@drawable/twenty" />

    <TextView
        android:id="@+id/tvtwenty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.00"
        android:layout_gravity="center_vertical"
        android:text="0"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="61dp"
        android:layout_height="80dp"
        android:layout_marginLeft="43dp"
        android:layout_marginRight="13.5dp"
        android:src="@drawable/cquarter" />
    <TextView
        android:id="@+id/tvquarter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.00"
        android:layout_gravity="center_vertical"
        android:text="0"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="80dp" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="100dp"
        android:layout_height="80dp"
        android:layout_weight="0.00"
        android:layout_marginLeft="8.75dp"
        android:layout_marginRight="8.75dp"
        android:src="@drawable/ten" />

    <TextView
        android:id="@+id/tvten"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.00"
        android:layout_gravity="center_vertical"
        android:text="0"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="61dp"
        android:layout_height="80dp"
        android:layout_marginLeft="43dp"
        android:layout_marginRight="13.5dp"
        android:src="@drawable/cdime" />
    <TextView
        android:id="@+id/tvdime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.00"
        android:layout_gravity="center_vertical"
        android:text="0"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="80dp" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="100dp"
        android:layout_height="80dp"
        android:layout_weight="0.00"
        android:layout_marginLeft="8.75dp"
        android:layout_marginRight="8.75dp"
        android:src="@drawable/five" />

    <TextView
        android:id="@+id/tvfive"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.00"
        android:layout_gravity="center_vertical"
        android:text="0"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="61dp"
        android:layout_height="80dp"
        android:layout_marginLeft="43dp"
        android:layout_marginRight="13.5dp"
        android:src="@drawable/cnickel" />
    <TextView
        android:id="@+id/tvnickel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.00"
        android:layout_gravity="center_vertical"
        android:text="0"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="80dp" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="100dp"
        android:layout_height="80dp"
        android:layout_weight="0.00"
        android:layout_marginLeft="8.75dp"
        android:layout_marginRight="8.75dp"
        android:src="@drawable/one" />

    <TextView
        android:id="@+id/tvone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.00"
        android:layout_gravity="center_vertical"
        android:text="0"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="61dp"
        android:layout_height="80dp"
        android:layout_marginLeft="43dp"
        android:layout_marginRight="13.5dp"
        android:src="@drawable/cpenny" />
    <TextView
        android:id="@+id/tvpenny"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.00"
        android:layout_gravity="center_vertical"
        android:text="0"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

</TableLayout>

If it helps, i think the error MAY have something to do with resources i am using (eight jpgs), but i'm not sure. The program runs cleanly up until the OnClickListener is activated. Thanks for any and all help!

2012-04-03 21:05
by Wilson
Sorry, posting ALL code here, logcat somewhere else just to point you on the line 65 is a waste of our time! Please reduce the code, read the stacktrace and try to solve it before posting everything here... at least the next time.. - WarrenFaith 2012-04-03 21:11
The debugger is your friend. Use it to debug your code - Brian Roach 2012-04-03 21:13
@WarrenFaith I will, sorry about that - Wilson 2012-04-03 21:15


5

Is here (and in another lines like that):

tv20.setText(twenty);

twenty is int and should be String (I suppose that you want to show the number in the textview):

tv20.setText(String.valueOf(twenty));

EDIT:

All this variables have the same error

int td, tc, twenty, ten, five, one, quarter, dime, nickel, penny;

2012-04-03 21:09
by Enrique Marcos
Makes sense, thanks! I'll try it - Wilson 2012-04-03 21:11
String.valueOf(twenty) is kinda ugly. Use ""+twenty instead : - Tiago Almeida 2012-04-03 21:11
@TiagoAlmeida well : - Enrique Marcos 2012-04-03 21:13
Veto! If you use + you create an empty string, concatening the interger to it and store it as a new String while throwing away the empty one. Waste of object creation and memory.. - WarrenFaith 2012-04-03 21:18


1

You have a small but heavy mistake:

If you try to put a number/integer into a TextView, you need to cast it to a String first. If you don't do that, Android things you reference to an internal id like R.string.mynumber and that is not what you want.

2012-04-03 21:16
by WarrenFaith


0

try moving the two following lines right underneath Button solve = (Button)findViewById(R.id.bsolve); and before solve.setOnClickListener(new OnClickListener()

EditText tdet = (EditText)findViewById(R.id.etdollars);
EditText tcet = (EditText)findViewById(R.id.etcents);
2012-04-03 21:14
by Win Myo Htet