开发者

What is wrong with my android simple calculator app?

I'm new to android and this is my first application, it seems fine to me but ev开发者_如何学编程ery time I pressed the calculate button it seems to stop unexpectedly and force close.

package com.test.simplecalc;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final Button myButton = (Button) findViewById(R.id.myButton);
    final EditText firstNum = (EditText)findViewById(R.id.firstNum);
    final EditText secondNum = (EditText)findViewById(R.id.secondNum);
    final EditText finalNum = (EditText)findViewById(R.id.finalNum);

    myButton.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            int num1 = 0;

            try {
                num1 = Integer.parseInt(firstNum.getText().toString());
            } catch(NumberFormatException nfe) {
               Toast.makeText(MainActivity.this, "Could not parse" + nfe, Toast.LENGTH_SHORT).show();
            }

            int num2 = 0;

            try {
                num2 = Integer.parseInt(secondNum.getText().toString());
            } catch(NumberFormatException nfe) {
               Toast.makeText(MainActivity.this, "Could not parse" + nfe, Toast.LENGTH_SHORT).show();
            }
            int num3 = num1 + num2;
            finalNum.setText(num3);
        }
    });
}

}


It's likely that one of the two blocks is throwing something other than NumberFormatException. (My guess would be a NullPointerException in the call to toString().)

Try changing the caught exception in each case to Exception and see if this reveals the problem.


just try

num3.toString() 

while printing the answer, and better to use textview for showing answer

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜