Int崩溃我的应用程序

时间:2015-05-26 14:49:20

标签: android math int

我想知道下面一行是否有任何问题?

  int b1 = Integer.valueOf(selection.getText().toString());
  int b12 =  (b1*100);
  result.setText(Integer.toString(b12));

我已将该行隔离到以下行:

int b1 = Integer.valueOf(selection.getText().toString());

我打算使用b1 int值来做一个简单的数学运算,如下表所示:

int b12 =  (b1*100);

然后将值输出到TextView名称结果

result.setText(Integer.toString(b12));

我找到了一些答案并在stackoverflow中尝试但是没有看到 救命。能有什么样的灵魂启发我吗?

以下是我更改K Neeraj Lal给出的代码后的日志

05-26 23:11:29.542: E/AndroidRuntime(7862): FATAL EXCEPTION: main
05-26 23:11:29.542: E/AndroidRuntime(7862): Process: com.lockon.smdresistor2, PID: 7862
05-26 23:11:29.542: E/AndroidRuntime(7862): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lockon.smdresistor2/com.lockon.smdresistor2.MainActivity}: java.lang.NumberFormatException: Invalid int: "TextView"
05-26 23:11:29.542: E/AndroidRuntime(7862):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2215)
05-26 23:11:29.542: E/AndroidRuntime(7862):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2264)
05-26 23:11:29.542: E/AndroidRuntime(7862):     at android.app.ActivityThread.access$800(ActivityThread.java:144)
05-26 23:11:29.542: E/AndroidRuntime(7862):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
05-26 23:11:29.542: E/AndroidRuntime(7862):     at android.os.Handler.dispatchMessage(Handler.java:102)
05-26 23:11:29.542: E/AndroidRuntime(7862):     at android.os.Looper.loop(Looper.java:136)
05-26 23:11:29.542: E/AndroidRuntime(7862):     at android.app.ActivityThread.main(ActivityThread.java:5139)
05-26 23:11:29.542: E/AndroidRuntime(7862):     at java.lang.reflect.Method.invokeNative(Native Method)
05-26 23:11:29.542: E/AndroidRuntime(7862):     at java.lang.reflect.Method.invoke(Method.java:515)
05-26 23:11:29.542: E/AndroidRuntime(7862):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
05-26 23:11:29.542: E/AndroidRuntime(7862):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
05-26 23:11:29.542: E/AndroidRuntime(7862):     at dalvik.system.NativeStart.main(Native Method)
05-26 23:11:29.542: E/AndroidRuntime(7862): Caused by: java.lang.NumberFormatException: Invalid int: "TextView"
05-26 23:11:29.542: E/AndroidRuntime(7862):     at java.lang.Integer.invalidInt(Integer.java:137)
05-26 23:11:29.542: E/AndroidRuntime(7862):     at java.lang.Integer.parse(Integer.java:374)
05-26 23:11:29.542: E/AndroidRuntime(7862):     at java.lang.Integer.parseInt(Integer.java:365)
05-26 23:11:29.542: E/AndroidRuntime(7862):     at java.lang.Integer.parseInt(Integer.java:331)
05-26 23:11:29.542: E/AndroidRuntime(7862):     at java.lang.Integer.valueOf(Integer.java:489)
05-26 23:11:29.542: E/AndroidRuntime(7862):     at com.lockon.smdresistor2.MainActivity.onCreate(MainActivity.java:52)
05-26 23:11:29.542: E/AndroidRuntime(7862):     at android.app.Activity.performCreate(Activity.java:5231)
05-26 23:11:29.542: E/AndroidRuntime(7862):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-26 23:11:29.542: E/AndroidRuntime(7862):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
05-26 23:11:29.542: E/AndroidRuntime(7862):     ... 11 more

K Neeraj Lah和伙计们,这可能有助于升技

selection=(TextView)findViewById(R.id.text0);  
  Spinner spin=(Spinner)findViewById(R.id.spinner0);  
  result=(TextView)findViewById(R.id.result);

  spin.setOnItemSelectedListener(this);

  ArrayAdapter<String> aa=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, items);

  aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  spin.setAdapter(aa);

3 个答案:

答案 0 :(得分:0)

试试这个,

try{
    String text = selection.getText().toString();
    int b1 = 0;
    if(!text.equals("") || isIntegerParseInt(text)){
        b1  = Integer.valueOf();
        int b12 =  (b1*100);
        result.setText(Integer.toString(b12));
    } else {
        Toast.makeText(getApplicationContext(), "please input a number!", Toast.LENGTH_SHORT);
    }
} catch(Exception e) {
    e.printStackTrace();
    Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT);
}
....

public static boolean isIntegerParseInt(String str) {
        try {
            Integer.parseInt(str);
            return true;
        } catch (NumberFormatException nfe) {}
        return false;
    }

答案 1 :(得分:0)

如果你确定在选择文本视图中获得了一个int值,你可以试试这个:

int b1 = Integer.parseInt(selection.getText().toString());
int b12 = b1 * 100;
result.setText(String.valueOf(b12));

答案 2 :(得分:0)

有两种方式

line-ending

result.setText(String.valueOf(b12));

在我的案例中看到我的R文件的问题是选择textview有一些错误,所以我对我的项目做了一个干净并删除R并再次重新创建R文件