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
4

2 に答える 2

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);
于 2012-08-02T01:26:21.883 に答える
0

clearButtonは、Enterボタンがクリックされた場合にのみ設定されます。Enterボタンの前にclearButtonをクリックすると、エラーがスローされます。

clearButtonは、他のボタンと一緒に使用して設定できます。findViewById

于 2012-08-02T01:36:30.253 に答える