Android第一个应用程序不会运行,许多logcat错误

时间:2013-12-02 20:37:02

标签: java android eclipse logcat

嘿,我在eclipse中制作了一个小型计算器应用程序。然而,当我去测试它时,我会在我的日志中遇到一堆错误,我将包含这些错误。我不确定这里有什么问题,我是android和amp;编程的新手。 java的。任何帮助将非常感激。下面我将展示我的mainActivity类。

public class MainActivity extends Activity {
    final EditText num1Text = (EditText)findViewById(R.id.num1Text);
    final EditText num2Text = (EditText)findViewById(R.id.num2Text);
    final TextView answer = (TextView)findViewById(R.id.answer);

    final RadioGroup radioG = (RadioGroup)findViewById(R.id.radioOperation);
    final Button btnCalc = (Button)findViewById(R.id.buttonCalc);


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnCalc.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0){

                if(num1Text.getText().toString().isEmpty() || num2Text.getText().toString().isEmpty())
                {
                    Toast msg = Toast.makeText(getBaseContext(),"Please enter a Number", Toast.LENGTH_LONG);
                    msg.show();
                }

                else
                {
                    double num1 = Double.parseDouble(num1Text.getText().toString());
                    double num2 = Double.parseDouble(num2Text.getText().toString());

                    int selectedOperation = radioG.getCheckedRadioButtonId();

                    final RadioButton option = (RadioButton)findViewById(selectedOperation);

                    String Option = new String(option.getText().toString());

                    if(Option.equals("Add"))
                    {
                     answer.setText(String.valueOf(num1+num2));
                    }

                    else if(Option.equals("Subtract"))
                    {
                        answer.setText(String.valueOf(num1-num2));
                    }

                    else if(Option.equals("Divide"))
                    {
                        if(num2==0.0)
                        {
                            Toast msg = Toast.makeText(getBaseContext(),"Cannot Divide By Zero",Toast.LENGTH_LONG);
                            msg.show();
                        }
                        else
                        {
                            answer.setText(String.valueOf(num1/num2));
                        }
                    }

                    else if(Option.equals("Multiply"))
                    {
                        answer.setText(String.valueOf(num1*num2));
                    }

                }
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;

    }
}

现在这是我的logcat

12-02 15:27:10.485: D/AndroidRuntime(2677): Shutting down VM
12-02 15:27:10.485: W/dalvikvm(2677): threadid=1: thread exiting with uncaught exception (group=0x41a31c50)
12-02 15:27:10.485: E/AndroidRuntime(2677): FATAL EXCEPTION: main
12-02 15:27:10.485: E/AndroidRuntime(2677): Process: com.example.codyscalc, PID: 2677
12-02 15:27:10.485: E/AndroidRuntime(2677): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.codyscalc/com.example.codyscalc.MainActivity}: java.lang.NullPointerException
12-02 15:27:10.485: E/AndroidRuntime(2677):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
12-02 15:27:10.485: E/AndroidRuntime(2677):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2246)
12-02 15:27:10.485: E/AndroidRuntime(2677):     at android.app.ActivityThread.access$700(ActivityThread.java:145)
12-02 15:27:10.485: E/AndroidRuntime(2677):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1407)
12-02 15:27:10.485: E/AndroidRuntime(2677):     at android.os.Handler.dispatchMessage(Handler.java:102)
12-02 15:27:10.485: E/AndroidRuntime(2677):     at android.os.Looper.loop(Looper.java:137)
12-02 15:27:10.485: E/AndroidRuntime(2677):     at android.app.ActivityThread.main(ActivityThread.java:5062)
12-02 15:27:10.485: E/AndroidRuntime(2677):     at java.lang.reflect.Method.invokeNative(Native Method)
12-02 15:27:10.485: E/AndroidRuntime(2677):     at java.lang.reflect.Method.invoke(Method.java:515)
12-02 15:27:10.485: E/AndroidRuntime(2677):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
12-02 15:27:10.485: E/AndroidRuntime(2677):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
12-02 15:27:10.485: E/AndroidRuntime(2677):     at dalvik.system.NativeStart.main(Native Method)
12-02 15:27:10.485: E/AndroidRuntime(2677): Caused by: java.lang.NullPointerException
12-02 15:27:10.485: E/AndroidRuntime(2677):     at android.app.Activity.findViewById(Activity.java:1883)
12-02 15:27:10.485: E/AndroidRuntime(2677):     at com.example.codyscalc.MainActivity.<init>(MainActivity.java:16)
12-02 15:27:10.485: E/AndroidRuntime(2677):     at java.lang.Class.newInstanceImpl(Native Method)
12-02 15:27:10.485: E/AndroidRuntime(2677):     at java.lang.Class.newInstance(Class.java:1208)
12-02 15:27:10.485: E/AndroidRuntime(2677):     at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
12-02 15:27:10.485: E/AndroidRuntime(2677):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2103)
12-02 15:27:10.485: E/AndroidRuntime(2677):     ... 11 more

2 个答案:

答案 0 :(得分:2)

(EditText) findViewById(R.id.num1Text);

在致电setContentView之前,您无法做到这一点。

答案 1 :(得分:0)

尝试喜欢

private EditText num1Text;
private EditText num2Text;
private TextView answer;

private RadioGroup radioG;
private Button btnCalc ;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    num1Text = (EditText)findViewById(R.id.num1Text);
    num2Text = (EditText)findViewById(R.id.num2Text);
    answer = (TextView)findViewById(R.id.answer);

    radioG = (RadioGroup)findViewById(R.id.radioOperation);
    btnCalc = (Button)findViewById(R.id.buttonCalc);

    btnCalc.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View arg0){

            if(num1Text.getText().toString().isEmpty() || num2Text.getText().toString().isEmpty())
            {
                Toast msg = Toast.makeText(getBaseContext(),"Please enter a Number", Toast.LENGTH_LONG);
                msg.show();
            }

            else
            {
                double num1 = Double.parseDouble(num1Text.getText().toString());
                double num2 = Double.parseDouble(num2Text.getText().toString());

                int selectedOperation = radioG.getCheckedRadioButtonId();

                final RadioButton option = (RadioButton)findViewById(selectedOperation);

                String Option = new String(option.getText().toString());

                if(Option.equals("Add"))
                {
                 answer.setText(String.valueOf(num1+num2));
                }

                else if(Option.equals("Subtract"))
                {
                    answer.setText(String.valueOf(num1-num2));
                }

                else if(Option.equals("Divide"))
                {
                    if(num2==0.0)
                    {
                        Toast msg = Toast.makeText(getBaseContext(),"Cannot Divide By Zero",Toast.LENGTH_LONG);
                        msg.show();
                    }
                    else
                    {
                        answer.setText(String.valueOf(num1/num2));
                    }
                }

                else if(Option.equals("Multiply"))
                {
                    answer.setText(String.valueOf(num1*num2));
                }

            }
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;

}