-1

我在 android 中创建了一个应用程序,它有两个用于获取数字的编辑文本和几个用于运算符的单选按钮,如减法、加法、乘法和除法,以及一个用于在文本视图中计算结果的按钮,我的问题是什么时候在编辑文本中只输入一个数字,然后单击计算按钮我的模拟器关闭,怎么办?

07-24 19:04:40.250: I/Choreographer(1672): Skipped 44 frames!  The application may be doing too much work on its main thread.
07-24 19:04:40.802: I/Choreographer(1672): Skipped 44 frames!  The application may be doing too much work on its main thread.
07-24 19:04:42.522: I/Choreographer(1672): Skipped 45 frames!  The application may be doing too much work on its main thread.
07-24 19:04:43.070: I/Choreographer(1672): Skipped 57 frames!  The application may be doing too much work on its main thread.
07-24 19:04:43.320: D/AndroidRuntime(1672): Shutting down VM
07-24 19:04:43.320: W/dalvikvm(1672): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
07-24 19:04:43.439: E/AndroidRuntime(1672):     FATAL EXCEPTION: main
07-24 19:04:43.439: E/AndroidRuntime(1672):     java.lang.NumberFormatException: Invalid int: ""
07-24 19:04:43.439: E/AndroidRuntime(1672):     at java.lang.Integer.invalidInt(Integer.java:138)
07-24 19:04:43.439: E/AndroidRuntime(1672):     at java.lang.Integer.parseInt(Integer.java:359)
07-24 19:04:43.439: E/AndroidRuntime(1672):     at java.lang.Integer.parseInt(Integer.java:332)
07-24 19:04:43.439: E/AndroidRuntime(1672):     at com.appwacky.calcumachine.MainActivity$1.onClick(MainActivity.java:61)

07-24 19:04:43.439: E/AndroidRuntime(1672):     atAndroid.view.View.performClick(View.java:4204)
07-24 19:04:43.439: E/AndroidRuntime(1672):     atAndroid.view.View$PerformClick.run(View.java:17355)
07-24 19:04:43.439: E/AndroidRuntime(1672):     atAndroid.os.Handler.handleCallback(Handler.java:725)
07-24 19:04:43.439: E/AndroidRuntime(1672):     atAndroid.os.Handler.dispatchMessage(Handler.java:92)
07-24 19:04:43.439: E/AndroidRuntime(1672):     atAndroid.os.Looper.loop(Looper.java:137)
07-24 19:04:43.439: E/AndroidRuntime(1672): Android.app.ActivityThread.main(ActivityThread.java:5041)

07-24 19:04:43.439: E/AndroidRuntime(1672):     at java.lang.reflect.Method.invokeNative(Native Method)

07-24 19:04:43.439: E/AndroidRuntime(1672):     at java.lang.reflect.Method.invoke(Method.java:511)
07-24 19:04:43.439: E/AndroidRuntime(1672):at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)

07-24 19:04:43.439: E/AndroidRuntime(1672): at      com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)

07-24 19:04:43.439: E/AndroidRuntime(1672):     at dalvik.system.NativeStart.main(Native Method)

_ __ _ __ _ _ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ _ _ __ _ _

package com.appwacky.calcumachine;

import org.w3c.dom.Text;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity 
{

    int i,j,res;

    EditText edt1,edt2;
    TextView tv1;
    RadioButton add,sub,mul,div;
    Button b1;



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

        edt1=(EditText)findViewById(R.id.insert1);
        edt2=(EditText)findViewById(R.id.insert2);

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

        add=(RadioButton)findViewById(R.id.radio0);
        sub=(RadioButton)findViewById(R.id.radio1);
        mul=(RadioButton)findViewById(R.id.radio2);
        div=(RadioButton)findViewById(R.id.radio3);

        b1=(Button)findViewById(R.id.button1);

        edt1.getText().toString();

        edt1.requestFocus();            
        edt2.getText().toString();



        b1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v)

            {

                if(edt1.length()==0 && edt2.length()==0)
                {
                    Toast.makeText(getApplicationContext(), "Please enter some number", Toast.LENGTH_LONG).show();
                }


                 i=Integer.parseInt(edt1.getText().toString());
                   j=Integer.parseInt(edt2.getText().toString());

                if (add.isChecked()) 
                {

                    res=i+j;

                    //Integer.toString(res);

                     tv1.setText(Integer.toString(res));

                     System.out.println(tv1);


                }

                if (sub.isChecked())
                {
                    res=i-j;

                    tv1.setText(Integer.toString(res));
                    System.out.println(tv1);

                }




                if(mul.isChecked())
                {

                    res=i*j;

                    tv1.setText(Integer.toString(res));
                    System.out.println(tv1);

                }

            try{

                if(div.isChecked())
                {
                    tv1.setText("");
                    res=i/j;

                    tv1.setText(Integer.toString(res));
                    System.out.println(tv1);
                }

            }   
            catch(ArithmeticException ae)
            {
                System.out.println("Its an arithmatic error");
                Toast.makeText(getApplicationContext(), "This is against the arithmatic law", Toast.LENGTH_LONG).show();
            }


            }
        });






    }


}
4

1 回答 1

-2

Unfortunately your application has stopped- 消息就像世界上第一次哭泣的孩子。您的应用程序在哭泣并想从您那里查看它。:)

嬉戏

请查看使用EditText

第 1 步:像第 2 步一样创建您的实例Edittext ed=(Edittext)findViewById(R.id.editetext1) :然后您可以使用ed.getText();

请注意,从onClick();方法中使用它,如果您只创建对象的引用EditText但没有对象,那么您将得到null pointer exception.

于 2013-07-24T19:08:06.240 回答