2012-08-02 97 views
0

這裏是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 
+0

你有沒有在清單文件中包含TipCalculator,並宣佈它發射其它按鈕一起設置clearButton。 – Gogu 2012-08-02 01:22:43

+1

哪一個是82線? – 0gravity 2012-08-02 01:27:36

+0

clearButton.setOnClickListener(新OnClickListener(){ – PrommeringsDisplay 2012-08-02 01:33:02

回答

0
Caused by: java.lang.NullPointerException 
    at tip.calculator.TipCalculator.onCreate(TipCalculator.java:82) 

你永遠不會初始化clearButton。從評論

clearButton.setOnClickListener(new OnClickListener() {...} 

另外,你需要移動這樣的:

clearButton = (Button) findViewById(R.id.clearButton); 

到這裏:如果單擊回車鍵

myEditField = (EditText) findViewById(R.id.percentText); 
enter = (Button)findViewById(R.id.button1); 
myEditField2 = (EditText) findViewById(R.id.billText); 
clearButton = (Button) findViewById(R.id.clearButton); 
+0

沒有這clearButton.setOnClickListener(新OnClickListener(){ – PrommeringsDisplay 2012-08-02 01:31:25

+0

哦,我曾經有過在onclick的進入,這就是一個問題吧? – PrommeringsDisplay 2012-08-02 01:35:19

+1

這就是你引用它,你需要在正確的範圍內初始化clearButton' onClick()方法不會執行,直到按鈕被按下... – Sam 2012-08-02 01:35:34

0

clearButton時,才設置。如果您在輸入按鈕之前單擊clearButton,則會引發錯誤。

您可以使用findViewById

相關問題