我有點新的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;
}
}
}
我真的失去了所有這一切,我會很感激任何人都可以給我的幫助。
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)
感謝一堆!
請張貼您的logcat。如果你不知道如何去做:http://developer.android.com/guide/developing/tools/logcat.html – EGHDK 2012-03-27 19:41:35