新手提问:为什么在模拟器运行会报错,stop exception?

张安国 发布于 2012/02/29 10:38
阅读 852
收藏 0

Activity——03Rep.java

package anvien.Activity_03Rep;

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 Activity_03RepActivity extends Activity {
    /** Called when the activity is first created. */
    private EditText factorOne,factorTwo,result;
    private TextView symbol;
    private Button calculate;
 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //根据控件id取得控件对象
        factorOne = (EditText)findViewById(R.id.factorOne);
        factorTwo = (EditText)findViewById(R.id.factorTwo);
        symbol = (TextView)findViewById(R.id.symbol);
        calculate = (Button)findViewById(R.id.calculate);
        result = (EditText)findViewById(R.id.result);
      //为symbol和calculate设置显示的值
        //symbol.setText("乘以");
        //calculate.setText("计算");
        symbol.setText(R.string.symbol);
        calculate.setText(R.string.calculate);
        calculate.setOnClickListener(new CalculateListener());
    }
 class CalculateListener implements OnClickListener{

  public void onClick(View v) {
   // TODO Auto-generated method stub
   String factorOneStr = factorOne.getText().toString();
   String factorTwoStr = factorTwo.getText().toString();
   int factorOneInt = Integer.parseInt(factorOneStr);
   int factorTwoInt = Integer.parseInt(factorTwoStr);
   int resultInt = factorOneInt*factorTwoInt;
   result.setText(resultInt);
  }
  
 }
}

R.java:

/* AUTO-GENERATED FILE.  DO NOT MODIFY.
 *
 * This class was automatically generated by the
 * aapt tool from the resource data it found.  It
 * should not be modified by hand.
 */

package anvien.Activity_03Rep;

public final class R {
    public static final class attr {
    }
    public static final class drawable {
        public static final int app_icon=0x7f020000;
        public static final int battery_high_bkg=0x7f020001;
        public static final int ic_launcher=0x7f020002;
    }
    public static final class id {
        public static final int calculate=0x7f050003;
        public static final int factorOne=0x7f050000;
        public static final int factorTwo=0x7f050002;
        public static final int result=0x7f050004;
        public static final int symbol=0x7f050001;
    }
    public static final class layout {
        public static final int main=0x7f030000;
    }
    public static final class string {
        public static final int app_name=0x7f040001;
        public static final int calculate=0x7f040003;
        public static final int hello=0x7f040000;
        public static final int symbol=0x7f040002;
    }
}

string.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="hello">Hello World, Activity_03RepActivity!</string>
    <string name="app_name">Activity_03Rep</string>
    <string name="symbol">乘积</string>
    <string name="calculate">计算</string>

</resources>

main.xml:

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

    <EditText
        android:id="@+id/factorOne"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />
    <TextView
        android:id="@+id/symbol"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
     <EditText
        android:id="@+id/factorTwo"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />
     <Button 
         android:id="@+id/calculate"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         />
     <EditText 
         android:id="@+id/result"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         />
</LinearLayout>

各位大神帮忙看一下:这几个文件中错误的地方到底在哪里?为什么模拟器上一点那个calculate按钮,模拟器就报错呢?要我try again,强制关闭?

加载中
0
鉴客
鉴客
你在模拟器执行可以通过 logcat 看到异常信息的
0
迷途d书童
迷途d书童
你怎么不用logcat去看看异常信息呢,
public void onClick(View v) {
   // TODO Auto-generated method stub
   String factorOneStr = factorOne.getText().toString();
   String factorTwoStr = factorTwo.getText().toString();
   int factorOneInt = Integer.parseInt(factorOneStr);
   int factorTwoInt = Integer.parseInt(factorTwoStr);
   int resultInt = factorOneInt*factorTwoInt;
   result.setText(resultInt);
  }

这段代码你加try-catch捕获异常看看,肯能是字符串转整形时出错了Integer.parseInt

张安国
张安国
回复 @张安国 : String resultStr = resultInt.toString(); result.setText(resultStr);
张安国
张安国
谢谢啊!不愧大神!的确是这里错了,可是我要怎么改才行呢?
0
迷途d书童
迷途d书童
int resultInt = 0;
try {
    int factorOneInt = Integer.parseInt(factorOneStr);
    int factorTwoInt = Integer.parseInt(factorTwoStr);
    resultInt = factorOneInt*factorTwoInt;
} catch (Exception e) {
    e.printStackTrace();
}
result.setText(resultInt.toString());

你没把异常信息贴出来,result.setText()你不能传一个整形值给它,它会认为是R.string.**字符串的引用。

返回顶部
顶部