单击按钮时我的程序崩溃了..为什么?

时间:2012-04-03 21:05:28

标签: java android

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

这是我的java代码:

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);

            }
}
}});}
}

这里是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>

如果它有帮助,我认为错误可能与我正在使用的资源(八个jpgs)有关,但我不确定。程序运行干净,直到激活OnClickListener。感谢您的帮助!

3 个答案:

答案 0 :(得分:5)

在这里(以及其他类似的行):

tv20.setText(twenty);

twenty是int,应该是String(我想你想在textview中显示数字):

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

编辑:

所有这些变量都有相同的错误

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

答案 1 :(得分:1)

你有一个小而重的错误:

如果您尝试将数字/整数放入TextView,则需要先将其强制转换为String。如果您不这样做,那么您引用的内容ID为R.string.mynumber的Android内容并不是您想要的。

答案 2 :(得分:0)

尝试在Button solve = (Button)findViewById(R.id.bsolve);下方和solve.setOnClickListener(new OnClickListener()

之前移动以下两行
EditText tdet = (EditText)findViewById(R.id.etdollars);
EditText tcet = (EditText)findViewById(R.id.etcents);