2

これがJavaファイルです。使用されているのは1つだけです...エラーの意味がわかっていれば、自分でこれを行うことができます

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) findViewById(R.id.percentText);
final EditText myEditField2 = (EditText) findViewById(R.id.billText);

float percentage = 0;
float percentageInp = 0;
float billAmount = 0;
double output = 0; 
String output1 = "";
Button clearButton = (Button) findViewById(R.id.clearButton);

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);


    enter.setOnClickListener(new OnClickListener() {


        public void onClick(View v) {
             TextView textView;
             TextView errors;
             textView = (TextView) findViewById(R.id.textView1);
             errors = (TextView) findViewById(R.id.errorText);             
             for(;;){

             if(myEditField == null){
                 break;
             }

             if(myEditField2 == null){
                 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;

             textView = (TextView) findViewById(R.id.textView1);

            textView.setText("");

            percentageInp = 0;
            billAmount = 0;

            myEditField.setText("");
            myEditField2.setText("");


        }


    });
}
}

これは logcat です。エラーの意味や対処方法がわかりません... 注: これらはアプリを開いたときにのみ生成され、その後クラッシュします

08-01 15:37:43.934: E/Trace(671): error opening trace file: No such file or directory (2)
08-01 15:37:44.704: E/AndroidRuntime(671): FATAL EXCEPTION: main
08-01 15:37:44.704: E/AndroidRuntime(671): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{tip.calculator/tip.calculator.TipCalculator}: java.lang.NullPointerException
08-01 15:37:44.704: E/AndroidRuntime(671):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
08-01 15:37:44.704: E/AndroidRuntime(671):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
08-01 15:37:44.704: E/AndroidRuntime(671):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
08-01 15:37:44.704: E/AndroidRuntime(671):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
08-01 15:37:44.704: E/AndroidRuntime(671):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-01 15:37:44.704: E/AndroidRuntime(671):  at android.os.Looper.loop(Looper.java:137)
08-01 15:37:44.704: E/AndroidRuntime(671):  at android.app.ActivityThread.main(ActivityThread.java:4745)
08-01 15:37:44.704: E/AndroidRuntime(671):  at java.lang.reflect.Method.invokeNative(Native Method)
08-01 15:37:44.704: E/AndroidRuntime(671):  at java.lang.reflect.Method.invoke(Method.java:511)
08-01 15:37:44.704: E/AndroidRuntime(671):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-01 15:37:44.704: E/AndroidRuntime(671):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-01 15:37:44.704: E/AndroidRuntime(671):  at dalvik.system.NativeStart.main(Native Method)
08-01 15:37:44.704: E/AndroidRuntime(671): Caused by: java.lang.NullPointerException
08-01 15:37:44.704: E/AndroidRuntime(671):  at android.app.Activity.findViewById(Activity.java:1825)
08-01 15:37:44.704: E/AndroidRuntime(671):  at tip.calculator.TipCalculator.<init>(TipCalculator.java:19)
08-01 15:37:44.704: E/AndroidRuntime(671):  at java.lang.Class.newInstanceImpl(Native Method)
08-01 15:37:44.704: E/AndroidRuntime(671):  at java.lang.Class.newInstance(Class.java:1319)
08-01 15:37:44.704: E/AndroidRuntime(671):  at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
08-01 15:37:44.704: E/AndroidRuntime(671):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
08-01 15:37:44.704: E/AndroidRuntime(671):  ... 11 more
08-01 15:38:44.993: E/Trace(741): error opening trace file: No such file or directory (2)
08-01 15:38:45.123: E/AndroidRuntime(741): FATAL EXCEPTION: main
08-01 15:38:45.123: E/AndroidRuntime(741): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{tip.calculator/tip.calculator.TipCalculator}: java.lang.NullPointerException
08-01 15:38:45.123: E/AndroidRuntime(741):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
08-01 15:38:45.123: E/AndroidRuntime(741):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
08-01 15:38:45.123: E/AndroidRuntime(741):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
08-01 15:38:45.123: E/AndroidRuntime(741):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
08-01 15:38:45.123: E/AndroidRuntime(741):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-01 15:38:45.123: E/AndroidRuntime(741):  at android.os.Looper.loop(Looper.java:137)
08-01 15:38:45.123: E/AndroidRuntime(741):  at android.app.ActivityThread.main(ActivityThread.java:4745)
08-01 15:38:45.123: E/AndroidRuntime(741):  at java.lang.reflect.Method.invokeNative(Native Method)
08-01 15:38:45.123: E/AndroidRuntime(741):  at java.lang.reflect.Method.invoke(Method.java:511)
08-01 15:38:45.123: E/AndroidRuntime(741):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-01 15:38:45.123: E/AndroidRuntime(741):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-01 15:38:45.123: E/AndroidRuntime(741):  at dalvik.system.NativeStart.main(Native Method)
08-01 15:38:45.123: E/AndroidRuntime(741): Caused by: java.lang.NullPointerException
08-01 15:38:45.123: E/AndroidRuntime(741):  at android.app.Activity.findViewById(Activity.java:1825)
08-01 15:38:45.123: E/AndroidRuntime(741):  at tip.calculator.TipCalculator.<init>(TipCalculator.java:19)
08-01 15:38:45.123: E/AndroidRuntime(741):  at java.lang.Class.newInstanceImpl(Native Method)
08-01 15:38:45.123: E/AndroidRuntime(741):  at java.lang.Class.newInstance(Class.java:1319)
08-01 15:38:45.123: E/AndroidRuntime(741):  at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
08-01 15:38:45.123: E/AndroidRuntime(741):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
08-01 15:38:45.123: E/AndroidRuntime(741):  ... 11 more
4

1 に答える 1

6

TipCalculator問題は、クラスのメンバー フィールドを初期化するために使用している次の行です。

EditText myEditField = (EditText) findViewById(R.id.percentText);
final EditText myEditField2 = (EditText) findViewById(R.id.billText);
Button clearButton = (Button) findViewById(R.id.clearButton);

findViewByIdを呼び出す前に使用することはできませんsetContentView。上記を次のように置き換えます。

EditText myEditField;
EditText myEditField2;
Button clearButton;

そして、これをonCreate呼び出した後にどこかに追加しsetContentViewます:

myEditField = (EditText) findViewById(R.id.percentText);
myEditField2 = (EditText) findViewById(R.id.billText);
clearButton = (Button) findViewById(R.id.clearButton);

PS logcat の出力を解釈する方法は、最後の "Caused by:" 行を探して、そのスタック トレースを読み取ることです。その前のすべてのものは、この種の診断タスクには関係ありません。

于 2012-08-01T22:47:46.280 に答える