按下按钮强制关闭

时间:2012-03-27 19:40:16

标签: android button

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

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;

    }
  }
}

我真的迷失了所有这一切,我很感激任何人都可以给我的帮助。

P.S。这是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)

非常感谢!

2 个答案:

答案 0 :(得分:2)

问题可能在于以下两行:

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

答案 1 :(得分:0)

更改以上代码以获取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.