空指针异常的原因是什么

时间:2013-10-28 14:12:32

标签: java android button

大家好我尝试做计算器,我写下面的代码,当我尝试运行它我有一个空指针异常我不知道是什么错误,我发现我的代码是好的,没有错误函数在布局出现之前关闭并且onclickerlistener里面的所有东西都是如此,所以至少我必须强行关闭当我点击任何按钮时如果有什么不对请帮帮我 这是我的代码:

    package com.example.calculato;

    import android.os.Bundle;
    import android.app.Activity;
    import android.util.Log;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;

    public class MainActivity extends Activity implements OnClickListener{

         EditText edt;

            double  num1=0,num2=0,result=0;
            double fact=1;

            String a="";
            char  op;
            boolean b=false;
            Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17,b18,b19,b20,b21;  

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Log.d("inside on create ", "okzzzzzzzzzzzzzzzzz");
            edt=(EditText)findViewById(R.id.editText1);
             b1=(Button)findViewById(R.id.button1);
             b2=(Button)findViewById(R.id.button2);
             b3=(Button)findViewById(R.id.button3);
             b4=(Button)findViewById(R.id.button4);
             b5=(Button)findViewById(R.id.button5);
             b6=(Button)findViewById(R.id.button6);
             b7=(Button)findViewById(R.id.button7);
             b8=(Button)findViewById(R.id.button8);
             b9=(Button)findViewById(R.id.button9);
             b10=(Button)findViewById(R.id.button10);
             b11=(Button)findViewById(R.id.button11);
             b12=(Button)findViewById(R.id.button12);
             b13=(Button)findViewById(R.id.button13);
             b14=(Button)findViewById(R.id.button14);
             b15=(Button)findViewById(R.id.button15);
             b16=(Button)findViewById(R.id.button16);
             Log.d("after def  ", "okzzzzzzzzzzzzzzzzz");
             b1.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        a=edt.getText().toString();
                          edt.setText(a+ "1");

                    }
                });

              b2.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        a=edt.getText().toString();
                          edt.setText(a+ "2");

                    }
                });

              b3.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        a=edt.getText().toString();
                          edt.setText(a+ "3");

                    }
                });

              b4.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        a=edt.getText().toString();
                          edt.setText(a+ "4");  
                    }
                });
              b5.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        a=edt.getText().toString();
                          edt.setText(a+ "5");

                    }
                });
              b6.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        a=edt.getText().toString();
                          edt.setText(a+ "6");

                    }
                });

              b7.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        a=edt.getText().toString();
                          edt.setText(a+ "7");

                    }
                });

              b8.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        a=edt.getText().toString();
                          edt.setText(a+ "8");


                    }
                });
              b9.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        a=edt.getText().toString();
                          edt.setText(a+ "9");

                    }
                });
              b10.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        a=edt.getText().toString();
                          edt.setText(a+ "0");  
                    }
                });

              b13.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        num1=Double.valueOf(edt.getText().toString());
                        op='+';
                        edt.setText("");




                    }
                });
              b14.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                         num1=Double.valueOf(edt.getText().toString());
                        op='-';
                        edt.setText("");


                    }
                });
              b15.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                         num1=Double.valueOf(edt.getText().toString());
                        op='*';
                        edt.setText("");


                    }
                });
              b16.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                         num1=Double.valueOf(edt.getText().toString());
                        op='/';
                        edt.setText("");


                    }
                });
              b11.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub

                        edt.setText(" ");




                    }
                });



                 b12.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        // TODO Auto-generated method stub

                        try {
                            String Value = edt.getText().toString();
                            if(Value.length()==1)
                                edt.setText("");
                            else 
                            {
                            Value = Value.substring(0,Value.length()-1);
                            edt.setText(Value);
                            }
                        } catch (Exception e) {

                        }

                    }
                });
                 b17.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            // TODO Auto-generated method stub
                             num2=Double.valueOf(edt.getText().toString());

                            if(op=='+')
                            {
                                result=num1+num2;
                                 edt.setText(String.valueOf(result));

                            }
                            if(op=='-')
                            {
                                result=num1-num2;
                                 edt.setText(String.valueOf(result));

                            }
                            if(op=='*')
                            {
                                result=num1*num2;
                                 edt.setText(String.valueOf(result));

                            }
                            if(op=='/')
                                {

                                if(num2!=0)
                                {
                                    result=num1/num2;
                                     edt.setText(String.valueOf(result));
                                    }
                                else
                                    edt.setText("Math Error");
                                }

                        }
                    });

        }

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

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

        }

    }

这就是logcat:

            10-28 14:16:22.488: E/AndroidRuntime(1049): FATAL EXCEPTION: main
    10-28 14:16:22.488: E/AndroidRuntime(1049): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.calculato/com.example.calculato.MainActivity}: java.lang.NullPointerException
    10-28 14:16:22.488: E/AndroidRuntime(1049):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
    10-28 14:16:22.488: E/AndroidRuntime(1049):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
    10-28 14:16:22.488: E/AndroidRuntime(1049):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
    10-28 14:16:22.488: E/AndroidRuntime(1049):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
    10-28 14:16:22.488: E/AndroidRuntime(1049):     at android.os.Handler.dispatchMessage(Handler.java:99)
    10-28 14:16:22.488: E/AndroidRuntime(1049):     at android.os.Looper.loop(Looper.java:137)
    10-28 14:16:22.488: E/AndroidRuntime(1049):     at android.app.ActivityThread.main(ActivityThread.java:5039)
    10-28 14:16:22.488: E/AndroidRuntime(1049):     at java.lang.reflect.Method.invokeNative(Native Method)
    10-28 14:16:22.488: E/AndroidRuntime(1049):     at java.lang.reflect.Method.invoke(Method.java:511)
    10-28 14:16:22.488: E/AndroidRuntime(1049):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    10-28 14:16:22.488: E/AndroidRuntime(1049):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    10-28 14:16:22.488: E/AndroidRuntime(1049):     at dalvik.system.NativeStart.main(Native Method)
    10-28 14:16:22.488: E/AndroidRuntime(1049): Caused by: java.lang.NullPointerException
    10-28 14:16:22.488: E/AndroidRuntime(1049):     at com.example.calculato.MainActivity.onCreate(MainActivity.java:254)
    10-28 14:16:22.488: E/AndroidRuntime(1049):     at android.app.Activity.performCreate(Activity.java:5104)
    10-28 14:16:22.488: E/AndroidRuntime(1049):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
    10-28 14:16:22.488: E/AndroidRuntime(1049):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)

4 个答案:

答案 0 :(得分:1)

希望这可以帮到你:b17未初始化

答案 1 :(得分:1)

下面:

b17.setOnClickListener(

b17为空,因此您获得NullPointerException。请务必仔细阅读您的LogCat,它就在那里。

答案 2 :(得分:1)

首先像这样初始化按钮17

 b17=(Button)findViewById(R.id.button17);

然后设置Click listener。 像

b17.setOnClickListener(new View.OnClickListener() {
 ---
 ---
 ---
 }

...享受

答案 3 :(得分:0)

你没有初始化Button b17 ...... 按照你的逻辑,你应该写b17=(Button)findViewById(R.id.button17);