這裏是java ...有人可以告訴我這裏有什麼錯嗎?錯誤可能是在這裏,我認爲logcat(下面)中的主要錯誤是一個空指針...我可能是錯誤的,但...提前謝謝大家!啓動我的應用程序時出現問題?
package tip.calculator;
import android.os.Bundle;
import android.app.Activity;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
import android.view.View;
import android.widget.Button;
public class TipCalculator extends Activity
{
private Button enter;
EditText myEditField ;
EditText myEditField2;
float percentage = 0;
float percentageInp = 0;
float billAmount = 0;
double output = 0;
String output1 = "";
Button clearButton ;
TextView textView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myEditField = (EditText) findViewById(R.id.percentText);
enter = (Button)findViewById(R.id.button1);
myEditField2 = (EditText) findViewById(R.id.billText);
enter.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
TextView errors;
textView = (TextView) findViewById(R.id.textView1);
errors = (TextView) findViewById(R.id.errorText);
clearButton = (Button) findViewById(R.id.clearButton);
for(;;){
if(myEditField == null){
errors.setText("Percent must be filled in");
break;
}
if(myEditField2 == null){
errors.setText("Bill Amount must be filled in");
break;
}
}
percentageInp = Float.parseFloat(myEditField.getText().toString());
billAmount = Float.parseFloat(myEditField2.getText().toString());
percentage = ((float)percentageInp /100);
output = (double)(billAmount * percentage);
double result = output * 100;
result = Math.round(result);
result = result/100;
output1 = Double.toString(result);
textView.setText(output1 + " $");
}
});
clearButton.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
percentage = 0;
output = 0;
output1 = "";
TextView textView = null;
textView.setText("");
percentageInp = 0;
billAmount = 0;
myEditField.setText("");
myEditField2.setText("");
}
});
}
}
這裏是logcat的......我不知道從哪裏開始對這個..
08-01 18:13:41.915: E/AndroidRuntime(30328): FATAL EXCEPTION: main
08-01 18:13:41.915: E/AndroidRuntime(30328): java.lang.RuntimeException: Unable to start activity ComponentInfo{tip.calculator/tip.calculator.TipCalculator}: java.lang.NullPointerException
08-01 18:13:41.915: E/AndroidRuntime(30328): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1968)
08-01 18:13:41.915: E/AndroidRuntime(30328): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1993)
08-01 18:13:41.915: E/AndroidRuntime(30328): at android.app.ActivityThread.access$600(ActivityThread.java:127)
08-01 18:13:41.915: E/AndroidRuntime(30328): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1159)
08-01 18:13:41.915: E/AndroidRuntime(30328): at android.os.Handler.dispatchMessage(Handler.java:99)
08-01 18:13:41.915: E/AndroidRuntime(30328): at android.os.Looper.loop(Looper.java:137)
08-01 18:13:41.915: E/AndroidRuntime(30328): at android.app.ActivityThread.main(ActivityThread.java:4507)
08-01 18:13:41.915: E/AndroidRuntime(30328): at java.lang.reflect.Method.invokeNative(Native Method)
08-01 18:13:41.915: E/AndroidRuntime(30328): at java.lang.reflect.Method.invoke(Method.java:511)
08-01 18:13:41.915: E/AndroidRuntime(30328): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
08-01 18:13:41.915: E/AndroidRuntime(30328): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
08-01 18:13:41.915: E/AndroidRuntime(30328): at dalvik.system.NativeStart.main(Native Method)
08-01 18:13:41.915: E/AndroidRuntime(30328): Caused by: java.lang.NullPointerException
08-01 18:13:41.915: E/AndroidRuntime(30328): at tip.calculator.TipCalculator.onCreate(TipCalculator.java:82)
08-01 18:13:41.915: E/AndroidRuntime(30328): at android.app.Activity.performCreate(Activity.java:4465)
08-01 18:13:41.915: E/AndroidRuntime(30328): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
08-01 18:13:41.915: E/AndroidRuntime(30328): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1932)
08-01 18:13:41.915: E/AndroidRuntime(30328): ... 11 more
你有沒有在清單文件中包含TipCalculator,並宣佈它發射其它按鈕一起設置clearButton。 – Gogu 2012-08-02 01:22:43
哪一個是82線? – 0gravity 2012-08-02 01:27:36
clearButton.setOnClickListener(新OnClickListener(){ – PrommeringsDisplay 2012-08-02 01:33:02