【问题标题】:Force Close on Button Pressed按下按钮时强制关闭
【发布时间】:2012-03-27 19:40:15
【问题描述】:

我对 android 有点陌生,但我有一些 java 经验。所以我决定开始做一个“物理计算器”,看看我在网上看了一堆教程后能不能做一个应用程序。我已经按照我的意愿设置了我的 xml 文件,但一切都不是“向上和向上”。我已经尝试了很多东西,但我对自己正在做的事情有些迷茫。基本上,我想用这段代码做的是获取适当的编辑文本字段中的内容,并在按下适当的按钮时将它们转换为整数,但是每当我按下按钮时,应用程序总是强制关闭我(但我能够“看到”所有图形,例如按钮等)。 (例如,对于速度,它只会拉时间和距离,因为这是用户提供的所有内容)。有人知道我在这里做错了什么吗?

package t.t.slash25;

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 Physics extends Activity implements OnClickListener
{
int v1 , d , t;
EditText velocity, distance , time;
Button calcVel, calcDis, calcTime;
TextView formula, result;

@Override
protected void onCreate(Bundle savedInstanceState) 
{
        super.onCreate(savedInstanceState);
        setContentView(R.layout.physics);
        initialize();               
}

private void initialize() 
{
    formula = (TextView) findViewById(R.id.tvFormula);
    velocity = (EditText) findViewById(R.id.etVelocity);
    distance = (EditText) findViewById(R.id.etDistance);
    time = (EditText) findViewById(R.id.etTime);
    calcVel = (Button) findViewById(R.id.bVel);
    calcVel.setOnClickListener(this);
    calcDis = (Button) findViewById(R.id.bDis);
    calcDis.setOnClickListener(this);
    calcTime = (Button) findViewById(R.id.bTime);
    calcTime.setOnClickListener(this);
    result = (TextView) findViewById(R.id.tvResult);
}

public void onClick(View v) 
{

    switch (v.getId()) 
    {
         case R.id.bVel:   //when the button is pressed, convert the editText's to integers, do the formula, and set the text
        d = Integer.getInteger(distance.getText().toString());
        t = Integer.getInteger(time.getText().toString());
        v1 = (d/t);
        result.setText("Velocity = " + v1);
          break;

    }
  }
}

我真的对这一切感到迷茫,如果有人能给予我任何帮助,我将不胜感激。

附:这是 logcat 错误消息:

03-27 15:26:58.959: E/AndroidRuntime(1084): FATAL EXCEPTION: main
03-27 15:26:58.959: E/AndroidRuntime(1084): java.lang.NullPointerException
03-27 15:26:58.959: E/AndroidRuntime(1084):     at t.t.slash25.Physics.onClick(Physics.java:47)
03-27 15:26:58.959: E/AndroidRuntime(1084):     at android.view.View.performClick(View.java:2532)
03-27 15:26:58.959: E/AndroidRuntime(1084):     at android.view.View$PerformClick.run(View.java:9277)
03-27 15:26:58.959: E/AndroidRuntime(1084):     at android.os.Handler.handleCallback(Handler.java:587)
03-27 15:26:58.959: E/AndroidRuntime(1084):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-27 15:26:58.959: E/AndroidRuntime(1084):     at android.os.Looper.loop(Looper.java:143)
03-27 15:26:58.959: E/AndroidRuntime(1084):     at android.app.ActivityThread.main(ActivityThread.java:4196)
03-27 15:26:58.959: E/AndroidRuntime(1084):     at java.lang.reflect.Method.invokeNative(Native Method)
03-27 15:26:58.959: E/AndroidRuntime(1084):     at java.lang.reflect.Method.invoke(Method.java:507)
03-27 15:26:58.959: E/AndroidRuntime(1084):     at      com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-27 15:26:58.959: E/AndroidRuntime(1084):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-27 15:26:58.959: E/AndroidRuntime(1084):     at dalvik.system.NativeStart.main(Native Method)

非常感谢!

【问题讨论】:

标签: android button


【解决方案1】:

问题可能出在这两行:

d = Integer.getInteger(distance.getText().toString());
t = Integer.getInteger(time.getText().toString());

Integer.getInteger() 不会将字符串转换为整数,您应该改用Integer.parseInt(),如下所示:

d = Integer.parseInt(distance.getText().toString());
t = Integer.parseInt(time.getText().toString());

更多详情请查看此答案:https://stackoverflow.com/a/3123350/284618

【讨论】:

  • 不敢相信我犯了这样的愚蠢错误。完美修复它。非常感谢您的帮助!
【解决方案2】:

将上面获取 d 和 t 的代码更改为:

d = Integer.parseInt(distance.getText().toString().trim());
 t = Integer.parseInt(time.getText().toString().trim());
    Btw dont use int for storing v1, you may get 0 as the answer even if it is 0.37 or something like that.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多