1

谁能告诉我这些错误的含义,实际上我是android编码的新手,无法弄清楚这些错误,请帮忙。

06-11 11:56:08.118: W/dalvikvm(593): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
06-11 11:56:08.128: E/AndroidRuntime(593): FATAL EXCEPTION: main
06-11 11:56:08.128: E/AndroidRuntime(593): java.lang.RuntimeException: Unable to start activity ComponentInfo{gaurav.android/gaurav.android.CalcActivity}: java.lang.NullPointerException
06-11 11:56:08.128: E/AndroidRuntime(593):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
06-11 11:56:08.128: E/AndroidRuntime(593):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
06-11 11:56:08.128: E/AndroidRuntime(593):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
06-11 11:56:08.128: E/AndroidRuntime(593):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
06-11 11:56:08.128: E/AndroidRuntime(593):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-11 11:56:08.128: E/AndroidRuntime(593):  at android.os.Looper.loop(Looper.java:137)
06-11 11:56:08.128: E/AndroidRuntime(593):  at android.app.ActivityThread.main(ActivityThread.java:4424)
06-11 11:56:08.128: E/AndroidRuntime(593):  at java.lang.reflect.Method.invokeNative(Native Method)
06-11 11:56:08.128: E/AndroidRuntime(593):  at java.lang.reflect.Method.invoke(Method.java:511)
06-11 11:56:08.128: E/AndroidRuntime(593):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-11 11:56:08.128: E/AndroidRuntime(593):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-11 11:56:08.128: E/AndroidRuntime(593):  at dalvik.system.NativeStart.main(Native Method)
06-11 11:56:08.128: E/AndroidRuntime(593): Caused by: java.lang.NullPointerException
06-11 11:56:08.128: E/AndroidRuntime(593):  at gaurav.android.CalcActivity.reset(CalcActivity.java:341)
06-11 11:56:08.128: E/AndroidRuntime(593):  at gaurav.android.CalcActivity.onCreate(CalcActivity.java:58)
06-11 11:56:08.128: E/AndroidRuntime(593):  at android.app.Activity.performCreate(Activity.java:4465)
06-11 11:56:08.128: E/AndroidRuntime(593):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
06-11 11:56:08.128: E/AndroidRuntime(593):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)

这是我的代码,它实际上是一个简单的计算器。

包gaurav.android;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.Button;
import android.widget.EditText;


public class CalcActivity extends Activity {
    EditText t=null;
    private final int MENUITEM_CLOSE = 300;
    private boolean hasChanged;
    private boolean readyToClear;

    public Button b1=null;
    public Button b2=null;
    public Button b3=null;
    public Button b4=null;
    public Button b5=null;
    public Button b6=null;
    public Button b7=null;
    public Button b8=null;
    public Button b9=null;
    public Button b10=null;
    public Button b11=null;
    public Button b12=null;
    public Button b13=null;
    public Button b14=null;
    public Button b15=null;
    public Button b16=null;
    public Button b17=null;
    public Button b18=null;
    public Button b19=null;
    public Button b20=null;
    public Button b21=null;
    public Button b22=null;
    public Button b23=null;
    public Button b24=null;
    public Button b25=null;
    public Button b26=null;
    public Button b27=null;
    public Button b28=null;

    private double memNum=0;
    private int operator=1;
    private double num=0;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setTheme(android.R.style.Theme_Black);
        setContentView(R.layout.main);
        reset();
        t=(EditText)findViewById(R.id.editText1);
        b1=(Button)findViewById(R.id.Button17);
        b2=(Button)findViewById(R.id.Button18);
        b3=(Button)findViewById(R.id.Button19);
        b4=(Button)findViewById(R.id.Button12);
        b5=(Button)findViewById(R.id.Button16);
        b6=(Button)findViewById(R.id.Button15);
        b7=(Button)findViewById(R.id.Button06);
        b8=(Button)findViewById(R.id.Button05);
        b9=(Button)findViewById(R.id.Button09);
        b10=(Button)findViewById(R.id.button7);
        b11=(Button)findViewById(R.id.button1);
        b12=(Button)findViewById(R.id.button2);
        b13=(Button)findViewById(R.id.button3);
        b14=(Button)findViewById(R.id.Button11);
        b15=(Button)findViewById(R.id.Button07);
        b16=(Button)findViewById(R.id.Button01);
        b17=(Button)findViewById(R.id.Button02);
        b18=(Button)findViewById(R.id.Button03);
        b19=(Button)findViewById(R.id.Button14);
        b20=(Button)findViewById(R.id.button4);
        b21=(Button)findViewById(R.id.Button10);
        b22=(Button)findViewById(R.id.Button20);
        b23=(Button)findViewById(R.id.button6);
        b24=(Button)findViewById(R.id.button9);
        b25=(Button)findViewById(R.id.button8);
        b26=(Button)findViewById(R.id.Button13);
        b27=(Button)findViewById(R.id.Button21);
        b28=(Button)findViewById(R.id.button30);

        b1.setOnClickListener(new Button.OnClickListener(){public void onClick(View v){handleNumber(1);}});
        b2.setOnClickListener(new Button.OnClickListener(){public void onClick(View v){handleNumber(2);}});
        b3.setOnClickListener(new Button.OnClickListener(){public void onClick(View v){handleNumber(3);}});   
        b4.setOnClickListener(new Button.OnClickListener(){public void onClick(View v){handleNumber(4);}});
        b5.setOnClickListener(new Button.OnClickListener(){public void onClick(View v){handleNumber(5);}});
        b6.setOnClickListener(new Button.OnClickListener(){public void onClick(View v){handleNumber(6);}});
        b7.setOnClickListener(new Button.OnClickListener(){public void onClick(View v){handleNumber(7);}});   
        b8.setOnClickListener(new Button.OnClickListener(){public void onClick(View v){handleNumber(8);}});
        b9.setOnClickListener(new Button.OnClickListener(){public void onClick(View v){handleNumber(9);}});
        b10.setOnClickListener(new Button.OnClickListener(){public void onClick(View v){handleNumber(0);}});
        b11.setOnClickListener(new Button.OnClickListener(){public void onClick(View v){memNum=0;}});   
        b12.setOnClickListener(new Button.OnClickListener(){public void onClick(View v){setValue(Double.toString(memNum));}});
        b13.setOnClickListener(new Button.OnClickListener(){public void onClick(View v)
        {}});
        b14.setOnClickListener(new Button.OnClickListener(){public void onClick(View v){
            memNum = memNum+ Double.parseDouble(t.getText().toString());
            operator = 0;
        }});
        b15.setOnClickListener(new Button.OnClickListener(){public void onClick(View v){
            memNum = memNum- Double.parseDouble(t.getText().toString());
            operator = 0;
        }});   
        b16.setOnClickListener(new Button.OnClickListener(){public void onClick(View v){handleBackspace();}});
        b17.setOnClickListener(new Button.OnClickListener(){public void onClick(View v){}});
        b18.setOnClickListener(new Button.OnClickListener(){public void onClick(View v){reset();}});
        b19.setOnClickListener(new Button.OnClickListener(){public void onClick(View v){handlePlusMinus();}});   
        b20.setOnClickListener(new Button.OnClickListener(){public void onClick(View v){
            setValue(Double.toString(Math.sqrt(Double.parseDouble(t.getText().toString()))));
        }});
        b21.setOnClickListener(new Button.OnClickListener(){public void onClick(View v){handleEquals(4);}});
        b22.setOnClickListener(new Button.OnClickListener(){public void onClick(View v){handleEquals(3);}});
        b23.setOnClickListener(new Button.OnClickListener(){public void onClick(View v){handleEquals(2);}});   
        b24.setOnClickListener(new Button.OnClickListener(){public void onClick(View v){handleEquals(1);}});
        b25.setOnClickListener(new Button.OnClickListener(){public void onClick(View v){handleDecimal();}});
        b26.setOnClickListener(new Button.OnClickListener(){public void onClick(View v){
            setValue(Double.toString(num*(0.01 * Double.parseDouble(t.getText().toString()))));
        }});
        b27.setOnClickListener(new Button.OnClickListener(){public void onClick(View v){}});   
        b28.setOnClickListener(new Button.OnClickListener(){public void onClick(View v){handleEquals(0);}});

        t.setOnKeyListener(new OnKeyListener()
        {
            public boolean onKey(View v,int i,android.view.KeyEvent e)
            {
                if (e.getAction() == KeyEvent.ACTION_DOWN) 
                {
                    int keyCode=e.getKeyCode();

                    switch (keyCode) {
                     case KeyEvent.KEYCODE_0:
                      handleNumber(0);
                      break;

                     case KeyEvent.KEYCODE_1:
                      handleNumber(1);
                      break;

                     case KeyEvent.KEYCODE_2:
                      handleNumber(2);
                      break;

                     case KeyEvent.KEYCODE_3:
                      handleNumber(3);
                      break;

                     case KeyEvent.KEYCODE_4:
                      handleNumber(4);
                      break;

                     case KeyEvent.KEYCODE_5:
                      handleNumber(5);
                      break;

                     case KeyEvent.KEYCODE_6:
                      handleNumber(6);
                      break;

                     case KeyEvent.KEYCODE_7:
                      handleNumber(7);
                      break;

                     case KeyEvent.KEYCODE_8:
                      handleNumber(8);
                      break;

                     case KeyEvent.KEYCODE_9:
                      handleNumber(9);
                      break;

                     case 43:
                      handleEquals(1);
                      break;

                     case KeyEvent.KEYCODE_EQUALS:
                      handleEquals(0);
                      break;

                     case KeyEvent.KEYCODE_MINUS:
                      handleEquals(2);
                      break;

                     case KeyEvent.KEYCODE_PERIOD:
                      handleDecimal();
                      break;

                     case KeyEvent.KEYCODE_C:
                      reset();
                      break;

                     case KeyEvent.KEYCODE_SLASH:
                      handleEquals(4);
                      break;

                     case KeyEvent.KEYCODE_DPAD_DOWN:
                      return false;
                     }
                    }

                    return true;
                   }
                  });
                 }

                 @Override
                 public boolean onCreateOptionsMenu(Menu menu) {
                  menu.add(0,1, MENUITEM_CLOSE, "Close");

                  return super.onCreateOptionsMenu(menu);
                 }

                 @Override
                 public boolean onOptionsItemSelected(MenuItem item) {
                  switch (item.getItemId()) {
                  case MENUITEM_CLOSE:
                   finish();
                   break;
                  }

                  return super.onOptionsItemSelected(item);
                 }

                 private void handleEquals(int newOperator) {
                  if (hasChanged) {
                   switch (operator) {
                   case 1:
                    num = num + Double.parseDouble(t.getText().toString());
                    break;
                   case 2:
                    num = num - Double.parseDouble(t.getText().toString());
                    break;
                   case 3:
                    num = num * Double.parseDouble(t.getText().toString());
                    break;
                   case 4:
                    num = num / Double.parseDouble(t.getText().toString());
                    break;
                   }

                   String txt = Double.toString(num);
                   t.setText(txt);
                   t.setSelection(txt.length());

                   readyToClear = true;
                   hasChanged = false;
                  }

                  operator = newOperator;
                 }

                 private void handleNumber(int num) {
                  if (operator == 0)
                   reset();

                  String txt = t.getText().toString();
                  if (readyToClear) {
                   txt = "";
                   readyToClear = false;
                  } else if (txt.equals("0"))
                   txt = "";

                  txt = txt + Integer.toString(num);

                  t.setText(txt);
                  t.setSelection(txt.length());

                  hasChanged = true;
                 }

                 private void setValue(String value) {
                  if (operator == 0)
                   reset();

                  if (readyToClear) {
                   readyToClear = false;
                  }

                  t.setText(value);
                  t.setSelection(value.length());

                  hasChanged = true;
                 }

                 private void handleDecimal() {
                  if (operator == 0)
                   reset();

                  if (readyToClear) {
                   t.setText("0.");
                   t.setSelection(2);
                   readyToClear = false;
                   hasChanged = true;
                  } else {
                   String txt = t.getText().toString();

                   if (!txt.contains(".")) {
                    t.append(".");
                    hasChanged = true;
                   }
                  }
                 }

                 private void handleBackspace() {
                  if (!readyToClear) {
                   String txt = t.getText().toString();
                   if (txt.length() > 0) {
                    txt = txt.substring(0, txt.length() - 1);
                    if (txt.equals(""))
                     txt = "0";

                    t.setText(txt);
                    t.setSelection(txt.length());
                   }
                  }
                 }

                 private void handlePlusMinus() {
                  if (!readyToClear) {
                   String txt = t.getText().toString();
                   if (!txt.equals("0")) {
                    if (txt.charAt(0) == '-')
                     txt = txt.substring(1, txt.length());
                    else
                     txt = "-" + txt;

                    t.setText(txt);
                    t.setSelection(txt.length());
                   }
                  }
                 }

                 private void reset() {
                  num = 0;
                  t.setText("0");
                  t.setSelection(1);
                  operator = 1;
                 }


                    }
4

4 回答 4

2

您在第 341 行有一个空指针异常。

CalcActivity.reset(CalcActivity.java:341)

如果您在 LogCat 中双击这些行,它将带您到包含错误的行。

06-11 11:56:08.128: E/AndroidRuntime(593):  at gaurav.android.CalcActivity.reset(CalcActivity.java:341)
06-11 11:56:08.128: E/AndroidRuntime(593):  at gaurav.android.CalcActivity.onCreate(CalcActivity.java:58)
于 2012-06-11T06:54:32.333 回答
0

您在 handleEquals 方法中得到 NullPointerException

从你的代码中我看到的是

switch (operator)
            {
            case 1:
                num = num + Double.parseDouble(t.getText().toString());
                break;
            case 2:
                num = num - Double.parseDouble(t.getText().toString());
                break;
            case 3:
                num = num * Double.parseDouble(t.getText().toString());
                break;
            case 4:
                num = num / Double.parseDouble(t.getText().toString());
                break;
            }

你还没有初始化你的 TextViews。如果没有 Initiliaing t,您将无法使用以下功能

t.getText().toString()
于 2012-06-11T07:10:54.230 回答
0

TextView在初始化之前使用的是您面临的原因null pointer expection

    t=(EditText)findViewById(R.id.editText1);
    b1=(Button)findViewById(R.id.Button17);
    b2=(Button)findViewById(R.id.Button18);
    b3=(Button)findViewById(R.id.Button19);
    b4=(Button)findViewById(R.id.Button12);
    b5=(Button)findViewById(R.id.Button16);
    b6=(Button)findViewById(R.id.Button15);
    b7=(Button)findViewById(R.id.Button06);
    b8=(Button)findViewById(R.id.Button05);
    b9=(Button)findViewById(R.id.Button09);
    b10=(Button)findViewById(R.id.button7);
    b11=(Button)findViewById(R.id.button1);
    b12=(Button)findViewById(R.id.button2);
    b13=(Button)findViewById(R.id.button3);
    b14=(Button)findViewById(R.id.Button11);
    b15=(Button)findViewById(R.id.Button07);
    b16=(Button)findViewById(R.id.Button01);
    b17=(Button)findViewById(R.id.Button02);
    b18=(Button)findViewById(R.id.Button03);
    b19=(Button)findViewById(R.id.Button14);
    b20=(Button)findViewById(R.id.button4);
    b21=(Button)findViewById(R.id.Button10);
    b22=(Button)findViewById(R.id.Button20);
    b23=(Button)findViewById(R.id.button6);
    b24=(Button)findViewById(R.id.button9);
    b25=(Button)findViewById(R.id.button8);
    b26=(Button)findViewById(R.id.Button13);
    b27=(Button)findViewById(R.id.Button21);
    b28=(Button)findViewById(R.id.button30);

reset();

reset()所以在初始化所有之后写你的方法view

于 2012-06-11T07:12:49.160 回答
0

您正在调用reset()并且您正在使用该函数内的 edittext 变量t。但是您t只有在调用reset(). 交换这些行reset(); t=(EditText)findViewById(R.id.editText1);OnCreate()您的程序应该可以正常运行。

于 2012-06-11T07:18:01.267 回答