2

这是java文件,唯一使用的...如果我知道错误意味着我可以自己做

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 回答